[Notes] [Git][BuildStream/buildstream][richardmaw/subprocess-PWD] subprocesses: Ensure PWD is set in process environment



Title: GitLab

richardmaw-codethink pushed to branch richardmaw/subprocess-PWD at BuildStream / buildstream

Commits:

3 changed files:

Changes:

  • buildstream/sandbox/_sandboxbwrap.py
    ... ... @@ -69,6 +69,15 @@ class SandboxBwrap(Sandbox):
    69 69
             if env is None:
    
    70 70
                 env = self._get_environment()
    
    71 71
     
    
    72
    +        if cwd is None:
    
    73
    +            cwd = '/'
    
    74
    +
    
    75
    +        # Naive getcwd implementations can break when bind-mounts to different
    
    76
    +        # paths on the same filesystem are present. Letting the command know
    
    77
    +        # what directory it is in makes it unnecessary to call the faulty
    
    78
    +        # getcwd.
    
    79
    +        env['PWD'] = cwd
    
    80
    +
    
    72 81
             if not self._has_command(command[0], env):
    
    73 82
                 raise SandboxError("Staged artifacts do not provide command "
    
    74 83
                                    "'{}'".format(command[0]),
    
    ... ... @@ -83,9 +92,6 @@ class SandboxBwrap(Sandbox):
    83 92
             mount_map = MountMap(self, flags & SandboxFlags.ROOT_READ_ONLY)
    
    84 93
             root_mount_source = mount_map.get_mount_source('/')
    
    85 94
     
    
    86
    -        if cwd is None:
    
    87
    -            cwd = '/'
    
    88
    -
    
    89 95
             # Grab the full path of the bwrap binary
    
    90 96
             bwrap_command = [utils.get_host_tool('bwrap')]
    
    91 97
     
    

  • buildstream/sandbox/_sandboxchroot.py
    ... ... @@ -58,6 +58,12 @@ class SandboxChroot(Sandbox):
    58 58
             if env is None:
    
    59 59
                 env = self._get_environment()
    
    60 60
     
    
    61
    +        # Naive getcwd implementations can break when bind-mounts to different
    
    62
    +        # paths on the same filesystem are present. Letting the command know
    
    63
    +        # what directory it is in makes it unnecessary to call the faulty
    
    64
    +        # getcwd.
    
    65
    +        env['PWD'] = cwd
    
    66
    +
    
    61 67
             if not self._has_command(command[0], env):
    
    62 68
                 raise SandboxError("Staged artifacts do not provide command "
    
    63 69
                                    "'{}'".format(command[0]),
    

  • tests/testutils/repo/git.py
    ... ... @@ -26,24 +26,30 @@ class Git(Repo):
    26 26
     
    
    27 27
             super(Git, self).__init__(directory, subdir)
    
    28 28
     
    
    29
    +    def _run_git(self, *args, **kwargs):
    
    30
    +        argv = ['git']
    
    31
    +        argv.extend(args)
    
    32
    +        if 'env' not in kwargs:
    
    33
    +            kwargs['env'] = dict(GIT_ENV, PWD=self.repo)
    
    34
    +        kwargs.setdefault('cwd', self.repo)
    
    35
    +        kwargs.setdefault('check', True)
    
    36
    +        return subprocess.run(argv, **kwargs)
    
    37
    +
    
    29 38
         def create(self, directory):
    
    30 39
             self.copy_directory(directory, self.repo)
    
    31
    -        subprocess.call(['git', 'init', '.'], env=GIT_ENV, cwd=self.repo)
    
    32
    -        subprocess.call(['git', 'add', '.'], env=GIT_ENV, cwd=self.repo)
    
    33
    -        subprocess.call(['git', 'commit', '-m', 'Initial commit'], env=GIT_ENV, cwd=self.repo)
    
    40
    +        self._run_git('init', '.')
    
    41
    +        self._run_git('add', '.')
    
    42
    +        self._run_git('commit', '-m', 'Initial commit')
    
    34 43
             return self.latest_commit()
    
    35 44
     
    
    36 45
         def add_commit(self):
    
    37
    -        subprocess.call(['git', 'commit', '--allow-empty', '-m', 'Additional commit'],
    
    38
    -                        env=GIT_ENV, cwd=self.repo)
    
    46
    +        self._run_git('commit', '--allow-empty', '-m', 'Additional commit')
    
    39 47
             return self.latest_commit()
    
    40 48
     
    
    41 49
         def add_file(self, filename):
    
    42 50
             shutil.copy(filename, self.repo)
    
    43
    -        subprocess.call(['git', 'add', os.path.basename(filename)], env=GIT_ENV, cwd=self.repo)
    
    44
    -        subprocess.call([
    
    45
    -            'git', 'commit', '-m', 'Added {}'.format(os.path.basename(filename))
    
    46
    -        ], env=GIT_ENV, cwd=self.repo)
    
    51
    +        self._run_git('add', os.path.basename(filename))
    
    52
    +        self._run_git('commit', '-m', 'Added {}'.format(os.path.basename(filename)))
    
    47 53
             return self.latest_commit()
    
    48 54
     
    
    49 55
         def add_submodule(self, subdir, url=None, checkout=None):
    
    ... ... @@ -53,8 +59,8 @@ class Git(Repo):
    53 59
             if url is not None:
    
    54 60
                 submodule['url'] = url
    
    55 61
             self.submodules[subdir] = submodule
    
    56
    -        subprocess.call(['git', 'submodule', 'add', url, subdir], env=GIT_ENV, cwd=self.repo)
    
    57
    -        subprocess.call(['git', 'commit', '-m', 'Added the submodule'], env=GIT_ENV, cwd=self.repo)
    
    62
    +        self._run_git('submodule', 'add', url, subdir)
    
    63
    +        self._run_git('commit', '-m', 'Added the submodule')
    
    58 64
             return self.latest_commit()
    
    59 65
     
    
    60 66
         def source_config(self, ref=None, checkout_submodules=None):
    
    ... ... @@ -74,10 +80,8 @@ class Git(Repo):
    74 80
             return config
    
    75 81
     
    
    76 82
         def latest_commit(self):
    
    77
    -        output = subprocess.check_output([
    
    78
    -            'git', 'rev-parse', 'master'
    
    79
    -        ], env=GIT_ENV, cwd=self.repo)
    
    83
    +        output = self._run_git('rev-parse', 'master', stdout=subprocess.PIPE).stdout
    
    80 84
             return output.decode('UTF-8').strip()
    
    81 85
     
    
    82 86
         def branch(self, branch_name):
    
    83
    -        subprocess.call(['git', 'checkout', '-b', branch_name], env=GIT_ENV, cwd=self.repo)
    87
    +        self._run_git('checkout', '-b', branch_name)



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]