Fixed a "git-sh-setup: file not found" error when installing project dependencies from Git VCS // Resolve #3740

This commit is contained in:
Ivan Kravets
2020-11-30 20:23:30 +02:00
parent b6128aeaa1
commit c38b9a4144
4 changed files with 44 additions and 11 deletions

View File

@ -14,6 +14,7 @@ PlatformIO Core 5
- Added "Core" suffix when showing PlatformIO Core version using ``pio --version`` command - Added "Core" suffix when showing PlatformIO Core version using ``pio --version`` command
- Improved ``.ccls`` configuration file for Emacs, Vim, and Sublime Text integrations - Improved ``.ccls`` configuration file for Emacs, Vim, and Sublime Text integrations
- Do not provide "intelliSenseMode" option when generating configuration for VSCode C/C++ extension - Do not provide "intelliSenseMode" option when generating configuration for VSCode C/C++ extension
- Fixed a "git-sh-setup: file not found" error when installing project dependencies from Git VCS (`issue #3740 <https://github.com/platformio/platformio-core/issues/3740>`_)
5.0.3 (2020-11-12) 5.0.3 (2020-11-12)
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

View File

@ -153,7 +153,7 @@ class PackageManagerInstallMixin(object):
finally: finally:
if os.path.isdir(tmp_dir): if os.path.isdir(tmp_dir):
try: try:
shutil.rmtree(tmp_dir) fs.rmtree(tmp_dir)
except: # pylint: disable=bare-except except: # pylint: disable=bare-except
pass pass

View File

@ -12,17 +12,17 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import re import re
from os.path import join import subprocess
from subprocess import CalledProcessError, check_call import sys
from sys import modules
from platformio import proc
from platformio.package.exception import ( from platformio.package.exception import (
PackageException, PackageException,
PlatformioException, PlatformioException,
UserSideException, UserSideException,
) )
from platformio.proc import exec_command
try: try:
from urllib.parse import urlparse from urllib.parse import urlparse
@ -51,7 +51,7 @@ class VCSClientFactory(object):
if not type_: if not type_:
raise VCSBaseException("VCS: Unknown repository type %s" % remote_url) raise VCSBaseException("VCS: Unknown repository type %s" % remote_url)
try: try:
obj = getattr(modules[__name__], "%sClient" % type_.title())( obj = getattr(sys.modules[__name__], "%sClient" % type_.title())(
src_dir, remote_url, tag, silent src_dir, remote_url, tag, silent
) )
assert isinstance(obj, VCSClientBase) assert isinstance(obj, VCSClientBase)
@ -86,7 +86,7 @@ class VCSClientBase(object):
@property @property
def storage_dir(self): def storage_dir(self):
return join(self.src_dir, "." + self.command) return os.path.join(self.src_dir, "." + self.command)
def export(self): def export(self):
raise NotImplementedError raise NotImplementedError
@ -108,17 +108,19 @@ class VCSClientBase(object):
args = [self.command] + args args = [self.command] + args
if "cwd" not in kwargs: if "cwd" not in kwargs:
kwargs["cwd"] = self.src_dir kwargs["cwd"] = self.src_dir
if "env" not in kwargs:
kwargs["env"] = os.environ
try: try:
check_call(args, **kwargs) subprocess.check_call(args, **kwargs)
return True return True
except CalledProcessError as e: except subprocess.CalledProcessError as e:
raise VCSBaseException("VCS: Could not process command %s" % e.cmd) raise VCSBaseException("VCS: Could not process command %s" % e.cmd)
def get_cmd_output(self, args, **kwargs): def get_cmd_output(self, args, **kwargs):
args = [self.command] + args args = [self.command] + args
if "cwd" not in kwargs: if "cwd" not in kwargs:
kwargs["cwd"] = self.src_dir kwargs["cwd"] = self.src_dir
result = exec_command(args, **kwargs) result = proc.exec_command(args, **kwargs)
if result["returncode"] == 0: if result["returncode"] == 0:
return result["out"].strip() return result["out"].strip()
raise VCSBaseException( raise VCSBaseException(
@ -129,6 +131,28 @@ class VCSClientBase(object):
class GitClient(VCSClientBase): class GitClient(VCSClientBase):
command = "git" command = "git"
_configured = False
def __init__(self, *args, **kwargs):
self.configure()
super(GitClient, self).__init__(*args, **kwargs)
@classmethod
def configure(cls):
if cls._configured:
return True
cls._configured = True
try:
result = proc.exec_command([cls.command, "--exec-path"])
if result["returncode"] != 0:
return False
path = result["out"].strip()
if path:
proc.append_env_path("PATH", path)
return True
except subprocess.CalledProcessError:
pass
return False
def check_client(self): def check_client(self):
try: try:
@ -173,7 +197,7 @@ class GitClient(VCSClientBase):
if self.tag: if self.tag:
args += ["--branch", self.tag] args += ["--branch", self.tag]
args += [self.remote_url, self.src_dir] args += [self.remote_url, self.src_dir]
assert self.run_cmd(args) assert self.run_cmd(args, cwd=os.getcwd())
if is_commit: if is_commit:
assert self.run_cmd(["reset", "--hard", self.tag]) assert self.run_cmd(["reset", "--hard", self.tag])
return self.run_cmd( return self.run_cmd(

View File

@ -203,3 +203,11 @@ def where_is_program(program, envpath=None):
return os.path.join(bin_dir, "%s.exe" % program) return os.path.join(bin_dir, "%s.exe" % program)
return program return program
def append_env_path(name, value):
cur_value = os.environ.get(name) or ""
if cur_value and value in cur_value.split(os.pathsep):
return cur_value
os.environ[name] = os.pathsep.join([cur_value, value])
return os.environ[name]