Skip to contentSkip to conversation

bitbake-setup: Convert string commands to lists where possible

PATCH New Richard Purdie 1 participant 6 messages May 31

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 bitbake-setup: Convert string commands to lists where possible

String commands have to be processed by subprocess using shell=True which is suboptimal for all kinds of reasons, including quoting. Convert to use a list where possible to improve efficiency and the robustness of code. It also means copy and paste will be more likely to use the preferred form.

---
 bin/bitbake-setup | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
bin/bitbake-setup +11 -11
index 220540f7f9c..69c9882f4c4 100755
@@ -128,8 +128,8 @@ def write_sources_fixed_revisions(config_dir, layer_dir, config_data):
         os.symlink(os.path.relpath(json_path ,layer_dir), json_link)
 
 def commit_config(config_dir):
-    bb.process.run("git -C {} add .".format(config_dir))
-    bb.process.run("git -C {} commit --allow-empty --no-verify -a -m 'Configuration at {}'".format(config_dir, time.asctime()))
+    bb.process.run(['git', '-C', config_dir, 'add', '.'])
+    bb.process.run(['git', '-C', config_dir, 'commit', '--allow-empty', '--no-verify', '-a', '-m', 'Configuration at ' + time.asctime()])
 
 def _write_layer_list(dest, repodirs):
     layers = []
@@ -357,7 +357,7 @@ def setup_bitbake_build(bitbake_config, layerdir, setupdir, thisdir, update_bb_c
         _setup_build_conf(layers, filerelative_layers, bitbake_confdir)
 
     if template:
-        bb.process.run("{} setup -c {} -b {} --no-shell".format(oesetupbuild, template, bitbake_builddir))
+        bb.process.run([oesetupbuild, "setup", "-c", template, "-b", bitbake_builddir, "--no-shell"])
     else:
         oeinitbuildenvdir = os.path.join(layerdir, 'oe-init-build-env-dir')
         if not os.path.exists(os.path.join(oeinitbuildenvdir, "oe-init-build-env")):
@@ -857,11 +857,11 @@ def init_config(top_dir, settings, args):
     os.makedirs(confdir)
     os.makedirs(layerdir)
 
-    bb.process.run("git -C {} init -b main".format(confdir))
+    bb.process.run(["git", "-C", confdir, "init", "-b", "main"])
     # Make sure commiting doesn't fail if no default git user is configured on the machine
-    bb.process.run("git -C {} config user.name bitbake-setup".format(confdir))
-    bb.process.run("git -C {} config user.email [email protected]".format(confdir))
-    bb.process.run("git -C {} commit --no-verify --allow-empty -m 'Initial commit'".format(confdir))
+    bb.process.run(["git", "-C", confdir, "config", "user.name", "bitbake-setup"])
+    bb.process.run(["git", "-C", confdir, "config", "user.email", "[email protected]"])
+    bb.process.run(["git", "-C", confdir, "commit", "--no-verify", "--allow-empty", "-m", "Initial commit"])
 
     bb.event.register("bb.build.TaskProgress", handle_task_progress, data=d)
 
@@ -872,7 +872,7 @@ def init_config(top_dir, settings, args):
 
 def get_diff(file1, file2):
     try:
-        bb.process.run('diff {} -uNr {} {}'.format(get_diff_color_param(), file1, file2))
+        bb.process.run(["diff", get_diff_color_param(), "-uNr", file1, file2])
     except bb.process.ExecutionError as e:
         if e.exitcode == 1:
             return e.stdout
@@ -887,7 +887,7 @@ def are_layers_changed(layers, layerdir, d):
         rev = r_remote['rev']
         branch = r_remote.get('branch', None)
 
-        rev_parse_result = bb.process.run('git -C {} rev-parse HEAD'.format(os.path.join(layerdir, repodir)))
+        rev_parse_result = bb.process.run(['git', '-C', os.path.join(layerdir, repodir), "rev-parse", "HEAD"])
         local_revision = rev_parse_result[0].strip()
         if sha1_re.match(rev):
             if rev != local_revision:
@@ -937,7 +937,7 @@ def build_status(top_dir, settings, args, d, update=False):
     new_upstream_config = obtain_config(top_dir, registry, args, source_overrides, d)
 
     write_upstream_config(confdir, new_upstream_config)
-    config_diff = bb.process.run('git -C {} diff {}'.format(confdir, get_diff_color_param()))[0]
+    config_diff = bb.process.run(["git", "-C" , confdir, "diff", get_diff_color_param()])[0]
 
     if config_diff:
         logger.plain('\nConfiguration in {} has changed:\n{}'.format(setupdir, config_diff))
@@ -945,7 +945,7 @@ def build_status(top_dir, settings, args, d, update=False):
             update_build(new_upstream_config, confdir, setupdir, layerdir, d,
                          update_bb_conf=args.update_bb_conf, rebase_conflicts_strategy=args.rebase_conflicts_strategy)
         else:
-            bb.process.run('git -C {} restore config-upstream.json'.format(confdir))
+            bb.process.run(["git", "-C", confdir, "restore", "config-upstream.json"])
         return
 
     layer_config = merge_overrides_into_sources(current_upstream_config["data"]["sources"], current_upstream_config["source-overrides"]["sources"])
#1 lore raw

PATCH 2/6 tests/{fetch,setup}: Convert string commands to lists

String commands have to be processed by subprocess using shell=True which is suboptimal for all kinds of reasons, including quoting. Convert to use a list for self.git() to improve efficiency and the robustness of code. It also means copy and paste will be more likely to use the preferred form.

---
 lib/bb/tests/fetch.py | 105 ++++++++++++++++++++----------------------
 lib/bb/tests/setup.py |  34 +++++++-------
 2 files changed, 68 insertions(+), 71 deletions(-)
lib/bb/tests/fetch.py +51 -54
index ebc80aa8cce..626876ed07b 100644
@@ -429,16 +429,13 @@ class FetcherTest(unittest.TestCase):
             bb.utils.prunedir(self.tempdir)
 
     def git(self, cmd, cwd=None):
-        if isinstance(cmd, str):
-            cmd = 'git -c safe.bareRepository=all ' + cmd
-        else:
-            cmd = ['git', '-c', 'safe.bareRepository=all'] + cmd
+        cmd = ['git', '-c', 'safe.bareRepository=all'] + cmd
         if cwd is None:
             cwd = self.gitdir
         return bb.process.run(cmd, cwd=cwd)[0]
 
     def git_init(self, cwd=None):
-        self.git('init', cwd=cwd)
+        self.git(['init'], cwd=cwd)
         # Explicitly set initial branch to master as
         # a common setup is to use other default
         # branch than master.
@@ -1884,7 +1881,7 @@ class GitShallowTest(FetcherTest):
         self.add_empty_file('b')
         self.assertRevCount(2, cwd=self.srcdir)
 
-        srcrev = self.git('rev-parse HEAD', cwd=self.srcdir).strip()
+        srcrev = self.git(['rev-parse', 'HEAD'], cwd=self.srcdir).strip()
         self.d.setVar('SRCREV', srcrev)
         uri = self.d.getVar('SRC_URI').split()[0]
         uri = '%s;nobranch=1;bare=1' % uri
@@ -1951,7 +1948,7 @@ class GitShallowTest(FetcherTest):
         fetcher, ud = self.fetch()
 
         # Ensure we have a current mirror tarball, but an out of date clone
-        self.git('update-ref refs/heads/master refs/heads/master~1', cwd=ud.clonedir)
+        self.git(['update-ref', 'refs/heads/master', 'refs/heads/master~1'], cwd=ud.clonedir)
         self.assertRevCount(2, cwd=ud.clonedir)
 
         # Fetch and unpack, from the current tarball, not the out of date clone
@@ -1978,12 +1975,12 @@ class GitShallowTest(FetcherTest):
 
         self.fetch_shallow()
         self.assertRevCount(1)
-        assert not self.git('fsck --dangling')
+        assert not self.git(['fsck', '--dangling'])
 
     def test_shallow_srcrev_branch_truncation(self):
         self.add_empty_file('a')
         self.add_empty_file('b')
-        b_commit = self.git('rev-parse HEAD', cwd=self.srcdir).rstrip()
+        b_commit = self.git(['rev-parse', 'HEAD'], cwd=self.srcdir).rstrip()
         self.add_empty_file('c')
         self.assertRevCount(3, cwd=self.srcdir)
 
@@ -1992,7 +1989,7 @@ class GitShallowTest(FetcherTest):
 
         # The 'c' commit was removed entirely, and 'a' was removed from history
         self.assertRevCount(1, ['--all'])
-        self.assertEqual(self.git('rev-parse HEAD').strip(), b_commit)
+        self.assertEqual(self.git(['rev-parse', 'HEAD']).strip(), b_commit)
         assert os.path.exists(os.path.join(self.gitdir, 'a'))
         assert os.path.exists(os.path.join(self.gitdir, 'b'))
         assert not os.path.exists(os.path.join(self.gitdir, 'c'))
@@ -2000,7 +1997,7 @@ class GitShallowTest(FetcherTest):
     def test_shallow_ref_pruning(self):
         self.add_empty_file('a')
         self.add_empty_file('b')
-        self.git('branch a_branch', cwd=self.srcdir)
+        self.git(['branch', 'a_branch'], cwd=self.srcdir)
         self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
         self.assertRevCount(2, cwd=self.srcdir)
 
@@ -2017,15 +2014,15 @@ class GitShallowTest(FetcherTest):
         bb.utils.mkdirhier(smdir)
         self.git_init(cwd=smdir)
         # Make this look like it was cloned from a remote...
-        self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
-        self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
+        self.git(['config', '--add', 'remote.origin.url', '"%s"' % smdir], cwd=smdir)
+        self.git(['config', '--add', 'remote.origin.fetch', '"+refs/heads/*:refs/remotes/origin/*"'], cwd=smdir)
         self.add_empty_file('asub', cwd=smdir)
         self.add_empty_file('bsub', cwd=smdir)
 
-        self.git('submodule init', cwd=self.srcdir)
-        self.git('-c protocol.file.allow=always submodule add file://%s' % smdir, cwd=self.srcdir)
-        self.git('submodule update', cwd=self.srcdir)
-        self.git('commit -m submodule -a', cwd=self.srcdir)
+        self.git(['submodule', 'init'], cwd=self.srcdir)
+        self.git(['-c', 'protocol.file.allow=always', 'submodule', 'add', 'file://%s' % smdir], cwd=self.srcdir)
+        self.git(['submodule', 'update'], cwd=self.srcdir)
+        self.git(['commit', '-m', 'submodule', '-a'], cwd=self.srcdir)
 
         uri = 'gitsm://%s;protocol=file;subdir=${S};branch=master' % self.srcdir
         fetcher, ud = self.fetch_shallow(uri)
@@ -2047,15 +2044,15 @@ class GitShallowTest(FetcherTest):
         bb.utils.mkdirhier(smdir)
         self.git_init(cwd=smdir)
         # Make this look like it was cloned from a remote...
-        self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
-        self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
+        self.git(['config', '--add', 'remote.origin.url', '"%s"' % smdir], cwd=smdir)
+        self.git(['config', '--add', 'remote.origin.fetch', '"+refs/heads/*:refs/remotes/origin/*"'], cwd=smdir)
         self.add_empty_file('asub', cwd=smdir)
         self.add_empty_file('bsub', cwd=smdir)
 
-        self.git('submodule init', cwd=self.srcdir)
-        self.git('-c protocol.file.allow=always submodule add file://%s' % smdir, cwd=self.srcdir)
-        self.git('submodule update', cwd=self.srcdir)
-        self.git('commit -m submodule -a', cwd=self.srcdir)
+        self.git(['submodule', 'init'], cwd=self.srcdir)
+        self.git(['-c', 'protocol.file.allow=always', 'submodule', 'add', 'file://%s' % smdir], cwd=self.srcdir)
+        self.git(['submodule', 'update'], cwd=self.srcdir)
+        self.git(['commit', '-m', 'submodule', '-a'], cwd=self.srcdir)
 
         uri = 'gitsm://%s;protocol=file;subdir=${S};branch=master' % self.srcdir
 
@@ -2085,10 +2082,10 @@ class GitShallowTest(FetcherTest):
         def test_shallow_annex(self):
             self.add_empty_file('a')
             self.add_empty_file('b')
-            self.git('annex init', cwd=self.srcdir)
+            self.git(['annex', 'init'], cwd=self.srcdir)
             open(os.path.join(self.srcdir, 'c'), 'w').close()
-            self.git('annex add c', cwd=self.srcdir)
-            self.git('commit --author "Foo Bar <foo@bar>" -m annex-c -a', cwd=self.srcdir)
+            self.git(['annex', 'add', 'c'], cwd=self.srcdir)
+            self.git(['commit', '--author', '"Foo Bar <foo@bar>"', '-m', 'annex-c', '-a'], cwd=self.srcdir)
             bb.process.run('chmod u+w -R %s' % self.srcdir)
 
             uri = 'gitannex://%s;protocol=file;subdir=${S};branch=master' % self.srcdir
@@ -2180,7 +2177,7 @@ class GitShallowTest(FetcherTest):
     def test_shallow_extra_refs(self):
         self.add_empty_file('a')
         self.add_empty_file('b')
-        self.git('branch a_branch', cwd=self.srcdir)
+        self.git(['branch', 'a_branch'], cwd=self.srcdir)
         self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
         self.assertRevCount(2, cwd=self.srcdir)
 
@@ -2193,8 +2190,8 @@ class GitShallowTest(FetcherTest):
     def test_shallow_extra_refs_wildcard(self):
         self.add_empty_file('a')
         self.add_empty_file('b')
-        self.git('branch a_branch', cwd=self.srcdir)
-        self.git('tag v1.0', cwd=self.srcdir)
+        self.git(['branch', 'a_branch'], cwd=self.srcdir)
+        self.git(['tag', 'v1.0'], cwd=self.srcdir)
         self.assertRefs(['master', 'a_branch', 'v1.0'], cwd=self.srcdir)
         self.assertRevCount(2, cwd=self.srcdir)
 
@@ -2253,14 +2250,14 @@ class GitShallowTest(FetcherTest):
         # Create initial git repo
         self.add_empty_file('a')
         self.add_empty_file('b')
-        self.git('checkout -b a_branch', cwd=self.srcdir)
+        self.git(['checkout', '-b', 'a_branch'], cwd=self.srcdir)
         self.add_empty_file('c')
         self.add_empty_file('d')
-        self.git('checkout master', cwd=self.srcdir)
-        self.git('tag v0.0 a_branch', cwd=self.srcdir)
+        self.git(['checkout', 'master'], cwd=self.srcdir)
+        self.git(['tag', 'v0.0', 'a_branch'], cwd=self.srcdir)
         self.add_empty_file('e')
-        self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
-        self.git('branch -d a_branch', cwd=self.srcdir)
+        self.git(['merge', '--no-ff', '--no-edit', 'a_branch'], cwd=self.srcdir)
+        self.git(['branch', '-d', 'a_branch'], cwd=self.srcdir)
         self.add_empty_file('f')
         self.assertRevCount(7, cwd=self.srcdir)
 
@@ -2285,7 +2282,7 @@ class GitShallowTest(FetcherTest):
         self.add_empty_file('a')
         self.add_empty_file('b')
         fetcher, ud = self.fetch(self.d.getVar('SRC_URI'))
-        self.git('tag v0.0 master', cwd=self.srcdir)
+        self.git(['tag', 'v0.0', 'master'], cwd=self.srcdir)
         self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
         self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
 
@@ -2333,9 +2330,9 @@ class GitShallowTest(FetcherTest):
 
     @skipIfNoNetwork()
     def test_bitbake(self):
-        self.git('remote add --mirror=fetch origin https://github.com/openembedded/bitbake', cwd=self.srcdir)
-        self.git('config core.bare true', cwd=self.srcdir)
-        self.git('fetch', cwd=self.srcdir)
+        self.git(['remote', 'add', '--mirror=fetch', 'origin', 'https://github.com/openembedded/bitbake'], cwd=self.srcdir)
+        self.git(['config', 'core.bare', 'true'], cwd=self.srcdir)
+        self.git(['fetch'], cwd=self.srcdir)
 
         self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
         # Note that the 1.10.0 tag is annotated, so this also tests
@@ -2345,8 +2342,8 @@ class GitShallowTest(FetcherTest):
         self.fetch_shallow()
 
         # Confirm that the history of 1.10.0 was removed
-        orig_revs = len(self.git('rev-list master', cwd=self.srcdir).splitlines())
-        revs = len(self.git('rev-list master').splitlines())
+        orig_revs = len(self.git(['rev-list', 'master'], cwd=self.srcdir).splitlines())
+        revs = len(self.git(['rev-list', 'master']).splitlines())
         self.assertNotEqual(orig_revs, revs)
         self.assertRefs(['master', 'origin/master'])
         self.assertRevCount(orig_revs - 1760)
@@ -2378,10 +2375,10 @@ class GitShallowTest(FetcherTest):
     def test_shallow_succeeds_with_tag_containing_slash(self):
         self.add_empty_file('a')
         self.add_empty_file('b')
-        self.git('tag t1/t2/t3', cwd=self.srcdir)
+        self.git(['tag', 't1/t2/t3'], cwd=self.srcdir)
         self.assertRevCount(2, cwd=self.srcdir)
 
-        srcrev = self.git('rev-parse HEAD', cwd=self.srcdir).strip()
+        srcrev = self.git(['rev-parse', 'HEAD'], cwd=self.srcdir).strip()
         self.d.setVar('SRCREV', srcrev)
         uri = self.d.getVar('SRC_URI').split()[0]
         uri = '%s;tag=t1/t2/t3' % uri
@@ -2743,18 +2740,18 @@ class FetchLocallyMissingTagFromRemote(FetcherTest):
         # then add a tag to this repo, and fetch it again, without
         # changing SRC_REV, but by adding ';tag=tag1` to SRC_URI
         # the new tag should be fetched and unpacked
-        srcrev = self.git('rev-parse HEAD', cwd=self.srcdir).strip()
+        srcrev = self.git(['rev-parse', 'HEAD'], cwd=self.srcdir).strip()
         self.d.setVar('SRCREV', srcrev)
         src_uri = self.d.getVar('SRC_URI')
         self._fetch_and_unpack(src_uri)
 
-        self.git('tag -m -a tag1', cwd=self.srcdir)
+        self.git(['tag', '-m', '-a', 'tag1'], cwd=self.srcdir)
 
         src_uri = '%s;tag=tag1' % self.d.getVar('SRC_URI').split()[0]
         self.d.setVar('SRC_URI', src_uri)
         self._fetch_and_unpack(src_uri)
 
-        output = self.git('log --pretty=oneline -n 1 refs/tags/tag1', cwd=self.gitdir)
+        output = self.git(['log', '--pretty=oneline', '-n', '1', 'refs/tags/tag1'], cwd=self.gitdir)
         assert "fatal: ambiguous argument" not in output
 
 
@@ -3500,20 +3497,20 @@ class FetchPremirroronlyLocalTest(FetcherTest):
     def git_new_commit(self):
         import random
         os.unlink(os.path.join(self.mirrordir, self.mirrorname))
-        branch = self.git("branch --show-current", self.gitdir).split()
+        branch = self.git(["branch", "--show-current"], self.gitdir).split()
         with open(os.path.join(self.gitdir, self.testfilename), "w") as testfile:
             testfile.write("File {} from branch {}; Useless random data {}".format(self.testfilename, branch, random.random()))
-        self.git("add {}".format(self.testfilename), self.gitdir)
-        self.git("commit -a -m \"This random commit {} in branch {}. I'm useless.\"".format(random.random(), branch), self.gitdir)
+        self.git(['add', self.testfilename], self.gitdir)
+        self.git(['commit', '-a', '-m', "\"This random commit {} in branch {}. I'm useless.\"".format(random.random(), branch)], self.gitdir)
         bb.process.run('tar -czvf {} .'.format(os.path.join(self.mirrordir, self.mirrorname)), cwd =  self.gitdir)
-        return self.git("rev-parse HEAD", self.gitdir).strip()
+        return self.git(["rev-parse", "HEAD"], self.gitdir).strip()
 
     def git_new_branch(self, name):
         self.git_new_commit()
-        head = self.git("rev-parse HEAD", self.gitdir).strip()
-        self.git("checkout -b {}".format(name), self.gitdir)
+        head = self.git(["rev-parse", "HEAD"], self.gitdir).strip()
+        self.git(["checkout", "-b", name], self.gitdir)
         newrev = self.git_new_commit()
-        self.git("checkout {}".format(head), self.gitdir)
+        self.git(["checkout", head], self.gitdir)
         return newrev
 
     def test_mirror_multiple_fetches(self):
@@ -3573,8 +3570,8 @@ class FetchPremirroronlyNetworkTest(FetcherTest):
     def make_git_repo(self):
         self.mirrorname = "git2_git.yoctoproject.org.fstests.tar.gz"
         os.makedirs(self.clonedir)
-        self.git("clone --bare {}".format(self.giturl), self.clonedir)
-        self.git("update-ref HEAD 15413486df1f5a5b5af699b6f3ba5f0984e52a9f", self.gitdir)
+        self.git(["clone", "--bare", self.giturl], self.clonedir)
+        self.git(["update-ref", "HEAD", "15413486df1f5a5b5af699b6f3ba5f0984e52a9f"], self.gitdir)
         bb.process.run('tar -czvf {} .'.format(os.path.join(self.mirrordir, self.mirrorname)), cwd =  self.gitdir)
         shutil.rmtree(self.clonedir)
 
lib/bb/tests/setup.py +17 -17
index 638d56d3bb3..ad829c7ab65 100644
@@ -22,12 +22,12 @@ class BitbakeSetupTest(FetcherTest):
 
         os.makedirs(self.registrypath)
         self.git_init(cwd=self.registrypath)
-        self.git('commit --allow-empty -m "Initial commit"', cwd=self.registrypath)
+        self.git(['commit', '--allow-empty', '-m', "Initial commit"], cwd=self.registrypath)
 
         self.testrepopath = os.path.join(self.tempdir, "test-repo")
         os.makedirs(self.testrepopath)
         self.git_init(cwd=self.testrepopath)
-        self.git('commit --allow-empty -m "Initial commit"', cwd=self.testrepopath)
+        self.git(['commit', '--allow-empty', '-m', "Initial commit"], cwd=self.testrepopath)
 
         oeinitbuildenv = """BBPATH=$1
 export BBPATH
@@ -176,8 +176,8 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         os.makedirs(os.path.join(self.registrypath, os.path.dirname(name)), exist_ok=True)
         with open(os.path.join(self.registrypath, name), 'w') as f:
             f.write(config)
-        self.git('add {}'.format(name), cwd=self.registrypath)
-        self.git('commit -m "Adding {}"'.format(name), cwd=self.registrypath)
+        self.git(['add', name], cwd=self.registrypath)
+        self.git(['commit', '-m', "Adding " + name], cwd=self.registrypath)
         return json.loads(config)
 
     def add_json_config_to_registry(self, name, rev, branch):
@@ -214,8 +214,8 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         if script:
             st = os.stat(fullname)
             os.chmod(fullname, st.st_mode | stat.S_IEXEC)
-        self.git('add {}'.format(name), cwd=self.testrepopath)
-        self.git('commit -m "Adding {}"'.format(name), cwd=self.testrepopath)
+        self.git(['add', name], cwd=self.testrepopath)
+        self.git(['commit', '-m', "Adding " + name], cwd=self.testrepopath)
 
     def config_is_unchanged(self, setuppath):
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
@@ -238,7 +238,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         with open(os.path.join(setuppath, 'config', "sources-fixed-revisions.json")) as f:
             sources_fixed_revisions = json.load(f)
         self.assertTrue('test-repo' in sources_fixed_revisions['sources'].keys())
-        revision = self.git('rev-parse HEAD', cwd=self.testrepopath).strip()
+        revision = self.git(['rev-parse', 'HEAD'], cwd=self.testrepopath).strip()
         self.assertEqual(revision, sources_fixed_revisions['sources']['test-repo']['git-remote']['rev'])
 
         if "oe-template" in bitbake_config:
@@ -397,7 +397,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         prev_test_file_content = test_file_content
         test_file_content = 'modified-in-branch\n'
         branch = "another-branch"
-        self.git('checkout -b {}'.format(branch), cwd=self.testrepopath)
+        self.git(['checkout', '-b', branch], cwd=self.testrepopath)
         self.add_file_to_testrepo('test-file', test_file_content)
         json_1 = self.add_json_config_to_registry('test-config-1.conf.json', branch, branch)
         for c in variants:
@@ -427,7 +427,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         prev_test_file_content = test_file_content
         test_file_content = 'modified-in-branch-no-bb-conf-update\n'
         branch = "another-branch-no-bb-conf-update"
-        self.git('checkout -b {}'.format(branch), cwd=self.testrepopath)
+        self.git(['checkout', '-b', branch], cwd=self.testrepopath)
         self.add_file_to_testrepo('test-file', test_file_content)
         json_1 = self.add_json_config_to_registry('test-config-1.conf.json', branch, branch)
         for c in variants:
@@ -473,7 +473,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
         prev_path = self.testrepopath
         self.testrepopath = prev_path + "-2"
-        self.git("clone {} {}".format(prev_path, self.testrepopath), cwd=self.tempdir)
+        self.git(['clone', prev_path, self.testrepopath], cwd=self.tempdir)
         json_1 = self.add_local_json_config_to_registry('test-config-1.conf.json', self.testrepopath)
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
         out = self.runbbsetup("update --update-bb-conf='yes'")
@@ -499,8 +499,8 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         ## edit a file and make a commit such that no rebase conflicts occur
         with open(os.path.join(layer_path, 'local-modification'), 'w') as f:
             f.write('locally-modified-again\n')
-        self.git('add .', cwd=layer_path)
-        self.git('commit -m "Adding a local modification"', cwd=layer_path)
+        self.git(['add', '.'], cwd=layer_path)
+        self.git(['commit', '-m', 'Adding a local modification'], cwd=layer_path)
         test_file_content = "modified-again-and-again\n"
         self.add_file_to_testrepo('test-file', test_file_content)
         out = self.runbbsetup("update --update-bb-conf='yes'")
@@ -509,8 +509,8 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         ## edit a file and make a commit in a way that causes a rebase conflict
         with open(os.path.join(layer_path, 'test-file'), 'w') as f:
             f.write('locally-modified\n')
-        self.git('add .', cwd=layer_path)
-        self.git('commit -m "Adding a local modification"', cwd=layer_path)
+        self.git(['add', '.'], cwd=layer_path)
+        self.git(['commit', '-m', 'Adding a local modification'], cwd=layer_path)
         test_file_content = "remotely-modified\n"
         self.add_file_to_testrepo('test-file', test_file_content)
         with self.assertRaisesRegex(bb.process.ExecutionError, "Merge conflict in test-file"):
@@ -688,7 +688,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         with open(os.path.join(layer_path, 'test-file')) as f:
             self.assertEqual(f.read(), 'upstream-v2\n',
                              "re-cloned layer must contain the upstream content")
-        status = self.git('status --porcelain', cwd=layer_path).strip()
+        status = self.git(['status', '--porcelain'], cwd=layer_path).strip()
         self.assertEqual(status, '',
                          "re-cloned layer must have no local modifications")
         del os.environ['BBPATH']
@@ -703,8 +703,8 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         # Commit a local change that touches the same file as the next upstream commit.
         with open(os.path.join(conflict_layer, 'test-file'), 'w') as f:
             f.write('conflicting-local\n')
-        self.git('add test-file', cwd=conflict_layer)
-        self.git('commit -m "Local conflicting change"', cwd=conflict_layer)
+        self.git(['add', 'test-file'], cwd=conflict_layer)
+        self.git(['commit', '-m', 'Local conflicting change'], cwd=conflict_layer)
 
         # Advance upstream with a conflicting edit.
         self.add_file_to_testrepo('test-file', 'conflicting-upstream\n')
#2 lore raw

PATCH 3/6 lib/bb: Convert string commands to bb.process.run to lists where possible

In reply to Richard Purdie

String commands have to be processed by subprocess using shell=True which is suboptimal for all kinds of reasons, including quoting. Convert to use a list where possible to improve efficiency and the robustness of code. It also means copy and paste will be more likely to use the preferred form.

---
 lib/bb/tests/fetch.py | 32 ++++++++++++++++----------------
 lib/bb/tests/setup.py |  2 +-
 lib/bb/utils.py       |  2 +-
 3 files changed, 18 insertions(+), 18 deletions(-)
lib/bb/tests/fetch.py +16 -16
index 626876ed07b..95cf6c414bb 100644
@@ -425,7 +425,7 @@ class FetcherTest(unittest.TestCase):
         if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
             print("Not cleaning up %s. Please remove manually." % self.tempdir)
         else:
-            bb.process.run('chmod u+rw -R %s' % self.tempdir)
+            bb.process.run(['chmod', 'u+rw', '-R', self.tempdir])
             bb.utils.prunedir(self.tempdir)
 
     def git(self, cmd, cwd=None):
@@ -698,7 +698,7 @@ class CleanTarballTest(FetcherTest):
         fetcher.download()
 
         fetcher.unpack(self.unpackdir)
-        mtime = bb.process.run('git log --all -1 --format=%ct',
+        mtime = bb.process.run(['git', 'log', '--all', '-1', '--format=%ct'],
                 cwd=os.path.join(self.unpackdir, 'git'))
         self.assertEqual(len(mtime), 2)
         mtime = int(mtime[0])
@@ -733,9 +733,9 @@ class FetcherLocalTest(FetcherTest):
         os.makedirs(os.path.join(self.localsrcdir, 'dir', 'subdir'))
         touch(os.path.join(self.localsrcdir, 'dir', 'subdir', 'e'))
         touch(os.path.join(self.localsrcdir, r'backslash\x2dsystemd-unit.device'))
-        bb.process.run('tar cf archive.tar -C dir .', cwd=self.localsrcdir)
-        bb.process.run('tar czf archive.tar.gz -C dir .', cwd=self.localsrcdir)
-        bb.process.run('tar cjf archive.tar.bz2 -C dir .', cwd=self.localsrcdir)
+        bb.process.run(['tar', 'cf', 'archive.tar', '-C', 'dir', '.'], cwd=self.localsrcdir)
+        bb.process.run(['tar', 'czf', 'archive.tar.gz', '-C', 'dir', '.'], cwd=self.localsrcdir)
+        bb.process.run(['tar', 'cjf', 'archive.tar.bz2', '-C', 'dir', '.'], cwd=self.localsrcdir)
         self.d.setVar("FILESPATH", self.localsrcdir)
 
     def make_ar_package(self, package_name, data_member="data.tar"):
@@ -1341,18 +1341,18 @@ class SVNTest(FetcherTest):
         repo_dir = tempfile.mkdtemp(dir=self.tempdir,
                                    prefix='svnfetch_localrepo_')
         repo_dir = os.path.abspath(repo_dir)
-        bb.process.run("svnadmin create project", cwd=repo_dir)
+        bb.process.run(['svnadmin', 'create', 'project'], cwd=repo_dir)
 
         self.repo_url = "file://%s/project" % repo_dir
-        bb.process.run("svn import --non-interactive -m 'Initial import' %s %s/trunk" % (src_dir, self.repo_url),
+        bb.process.run(['svn', 'import', '--non-interactive', '-m', 'Initial import', src_dir, "%s/trunk" % self.repo_url],
                        cwd=repo_dir)
 
-        bb.process.run("svn co %s svnfetch_co" % self.repo_url, cwd=self.tempdir)
+        bb.process.run(['svn', 'co', self.repo_url, 'svnfetch_co'], cwd=self.tempdir)
         # Github won't emulate SVN anymore (see https://github.blog/2023-01-20-sunsetting-subversion-support/)
         # Use still accessible svn repo (only trunk to avoid longer downloads)
-        bb.process.run("svn propset svn:externals 'bitbake https://svn.apache.org/repos/asf/serf/trunk' .",
+        bb.process.run(['svn', 'propset', 'svn:externals', "'bitbake https://svn.apache.org/repos/asf/serf/trunk'", "."],
                        cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
-        bb.process.run("svn commit --non-interactive -m 'Add external'",
+        bb.process.run(['svn', 'commit', '--non-interactive', '-m', 'Add external'],
                        cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
 
         self.src_dir = src_dir
@@ -1854,7 +1854,7 @@ class GitShallowTest(FetcherTest):
         # fetch and unpack, from the shallow tarball
         bb.utils.remove(self.gitdir, recurse=True)
         if os.path.exists(ud.clonedir):
-            bb.process.run('chmod u+w -R "%s"' % ud.clonedir)
+            bb.process.run(['chmod', 'u+w', '-R', ud.clonedir])
             bb.utils.remove(ud.clonedir, recurse=True)
             bb.utils.remove(ud.clonedir.replace('gitsource', 'gitsubmodule'), recurse=True)
 
@@ -2086,13 +2086,13 @@ class GitShallowTest(FetcherTest):
             open(os.path.join(self.srcdir, 'c'), 'w').close()
             self.git(['annex', 'add', 'c'], cwd=self.srcdir)
             self.git(['commit', '--author', '"Foo Bar <foo@bar>"', '-m', 'annex-c', '-a'], cwd=self.srcdir)
-            bb.process.run('chmod u+w -R %s' % self.srcdir)
+            bb.process.run(['chmod', 'u+w', '-R', self.srcdir])
 
             uri = 'gitannex://%s;protocol=file;subdir=${S};branch=master' % self.srcdir
             fetcher, ud = self.fetch_shallow(uri)
 
             self.assertRevCount(1)
-            assert './.git/annex/' in bb.process.run('tar -tzf %s' % os.path.join(self.dldir, ud.mirrortarballs[0]))[0]
+            assert './.git/annex/' in bb.process.run(['tar', '-tzf', os.path.join(self.dldir, ud.mirrortarballs[0])])[0]
             assert os.path.exists(os.path.join(self.gitdir, 'c'))
 
     def test_shallow_clone_preferred_over_shallow(self):
@@ -3492,7 +3492,7 @@ class FetchPremirroronlyLocalTest(FetcherTest):
         self.git_init(cwd=self.gitdir)
         for i in range(0):
             self.git_new_commit()
-        bb.process.run('tar -czvf {} .'.format(os.path.join(self.mirrordir, self.mirrorname)), cwd =  self.gitdir)
+        bb.process.run(['tar', '-czvf', os.path.join(self.mirrordir, self.mirrorname), '.'], cwd=self.gitdir)
 
     def git_new_commit(self):
         import random
@@ -3502,7 +3502,7 @@ class FetchPremirroronlyLocalTest(FetcherTest):
             testfile.write("File {} from branch {}; Useless random data {}".format(self.testfilename, branch, random.random()))
         self.git(['add', self.testfilename], self.gitdir)
         self.git(['commit', '-a', '-m', "\"This random commit {} in branch {}. I'm useless.\"".format(random.random(), branch)], self.gitdir)
-        bb.process.run('tar -czvf {} .'.format(os.path.join(self.mirrordir, self.mirrorname)), cwd =  self.gitdir)
+        bb.process.run(['tar', '-czvf', os.path.join(self.mirrordir, self.mirrorname), '.'], cwd=self.gitdir)
         return self.git(["rev-parse", "HEAD"], self.gitdir).strip()
 
     def git_new_branch(self, name):
@@ -3572,7 +3572,7 @@ class FetchPremirroronlyNetworkTest(FetcherTest):
         os.makedirs(self.clonedir)
         self.git(["clone", "--bare", self.giturl], self.clonedir)
         self.git(["update-ref", "HEAD", "15413486df1f5a5b5af699b6f3ba5f0984e52a9f"], self.gitdir)
-        bb.process.run('tar -czvf {} .'.format(os.path.join(self.mirrordir, self.mirrorname)), cwd =  self.gitdir)
+        bb.process.run(['tar', '-czvf', os.path.join(self.mirrordir, self.mirrorname), '.'], cwd=self.gitdir)
         shutil.rmtree(self.clonedir)
 
     @skipIfNoNetwork()
lib/bb/tests/setup.py +1 -1
index ad829c7ab65..57d5752b0ed 100644
@@ -92,7 +92,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
     def runbbsetup(self, cmd):
         bbsetup = os.path.abspath(os.path.dirname(__file__) +  "/../../../bin/bitbake-setup")
-        return bb.process.run("{} --global-settings {} {}".format(bbsetup, os.path.join(self.tempdir, 'global-config'), cmd))
+        return bb.process.run([bbsetup, '--global-settings', os.path.join(self.tempdir, 'global-config')] + cmd)
 
 
     def _add_json_config_to_registry_helper(self, name, sources):
lib/bb/utils.py +1 -1
index b04ff6ffc76..f03c950af5a 100644
@@ -2293,5 +2293,5 @@ def is_path_on_nfs(path):
             path = os.path.dirname(path)
 
     import bb.process
-    fstype = bb.process.run("stat -f -c %T {}".format(path))[0].strip()
+    fstype = bb.process.run(['stat', '-f', '-c', '%T', path])[0].strip()
     return fstype == "nfs"
#3 lore raw

PATCH 4/6 tests/setup: Convert string commands to lists where possible for runbbsetup

In reply to Richard Purdie

String commands have to be processed by subprocess using shell=True which is suboptimal for all kinds of reasons, including quoting. Convert to use a list where possible to improve efficiency and the robustness of code. It also means copy and paste will be more likely to use the preferred form.

---
 lib/bb/tests/setup.py | 76 +++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 38 deletions(-)
lib/bb/tests/setup.py +38 -38
index 57d5752b0ed..b30168ed61d 100644
@@ -219,9 +219,9 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
     def config_is_unchanged(self, setuppath):
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        out = self.runbbsetup("status")
+        out = self.runbbsetup(["status"])
         self.assertIn("Configuration in {} has not changed".format(setuppath), out[0])
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         self.assertIn("Configuration in {} has not changed".format(setuppath), out[0])
         del os.environ['BBPATH']
 
@@ -286,50 +286,50 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
             del os.environ['BBPATH']
 
         # check that no arguments works
-        self.runbbsetup("")
+        self.runbbsetup([])
 
         # check that --help works
-        self.runbbsetup("--help")
+        self.runbbsetup(["--help"])
 
         # change to self.tempdir to work with cwd-based default settings
         os.chdir(self.tempdir)
 
         # check that the default top-dir-prefix is cwd (now self.tempdir) with no global settings
-        out = self.runbbsetup("settings list")
+        out = self.runbbsetup(["settings", "list"])
         self.assertIn("default top-dir-prefix {}".format(os.getcwd()), out[0])
 
         # set up global location for dl-dir
         settings_path = "{}/global-config".format(self.tempdir)
-        out = self.runbbsetup("settings set --global default dl-dir {}".format(os.path.join(self.tempdir, 'downloads')))
+        out = self.runbbsetup(["settings", "set", "--global", "default", "dl-dir", os.path.join(self.tempdir, 'downloads')])
         self.assertIn("From section 'default' the setting 'dl-dir' was changed to", out[0])
         self.assertIn("Settings written to".format(settings_path), out[0])
 
         # check that writing settings works and then adjust them to point to
         # test registry repo
-        out = self.runbbsetup("settings set default registry 'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath))
+        out = self.runbbsetup(["settings", "set", "default", "registry", "'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath)])
         settings_path = "{}/bitbake-builds/settings.conf".format(self.tempdir)
         self.assertIn(settings_path, out[0])
         self.assertIn("From section 'default' the setting 'registry' was changed to", out[0])
         self.assertIn("Settings written to".format(settings_path), out[0])
 
         # check that listing settings works
-        out = self.runbbsetup("settings list")
+        out = self.runbbsetup(["settings", "list"])
         self.assertIn("default top-dir-prefix {}".format(self.tempdir), out[0])
         self.assertIn("default dl-dir {}".format(os.path.join(self.tempdir, 'downloads')), out[0])
         self.assertIn("default registry {}".format('git://{};protocol=file;branch=master;rev=master'.format(self.registrypath)), out[0])
 
         # check that 'list' produces correct output with no configs, one config and two configs
-        out = self.runbbsetup("list")
+        out = self.runbbsetup(["list"])
         self.assertNotIn("test-config-1", out[0])
         self.assertNotIn("test-config-2", out[0])
 
         json_1 = self.add_json_config_to_registry('test-config-1.conf.json', 'master', 'master')
-        out = self.runbbsetup("list")
+        out = self.runbbsetup(["list"])
         self.assertIn("test-config-1", out[0])
         self.assertNotIn("test-config-2", out[0])
 
         json_2 = self.add_json_config_to_registry('config-2/test-config-2.conf.json', 'master', 'master')
-        out = self.runbbsetup("list --write-json={}".format(os.path.join(self.tempdir, "test-configs.json")))
+        out = self.runbbsetup(["list", "--write-json={}".format(os.path.join(self.tempdir, "test-configs.json"))])
         self.assertIn("test-config-1", out[0])
         self.assertIn("test-config-2", out[0])
         with open(os.path.join(self.tempdir, "test-configs.json")) as f:
@@ -364,14 +364,14 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         try:
             for v in test_configurations:
                 for c in v['buildconfigs']:
-                    out = self.runbbsetup("init --non-interactive {} {}".format(v['cmdline'], c))
+                    out = self.runbbsetup(["init", "--non-interactive", v['cmdline']] + c.split())
                     setuppath = self.get_setup_path(v['name'], c)
                     self.check_setupdir_files(setuppath, test_file_content)
         finally:
             server.stop()
 
         # install buildtools
-        out = self.runbbsetup("install-buildtools --setup-dir {}".format(setuppath))
+        out = self.runbbsetup(["install-buildtools", "--setup-dir", setuppath])
         self.assertIn("Buildtools installed into", out[0])
         self.assertTrue(os.path.exists(os.path.join(setuppath, 'buildtools')))
 
@@ -383,9 +383,9 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         for c in variants:
             setuppath = self.get_setup_path('test-config-1', c)
             os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-            out = self.runbbsetup("status")
+            out = self.runbbsetup(["status"])
             self.assertIn("Layer repository file://{} checked out into {}/layers/test-repo updated revision master from".format(self.testrepopath, setuppath), out[0])
-            out = self.runbbsetup("update --update-bb-conf='yes'")
+            out = self.runbbsetup(["update", "--update-bb-conf=yes"])
             if c in ('gadget', 'gizmo'):
                 self.assertIn("Leaving the previous configuration in {}/build/conf-backup.".format(setuppath), out[0])
                 self.assertIn('-{}+{}'.format(prev_test_file_content, test_file_content), out[0])
@@ -403,10 +403,10 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         for c in variants:
             setuppath = self.get_setup_path('test-config-1', c)
             os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-            out = self.runbbsetup("status")
+            out = self.runbbsetup(["status"])
             self.assertIn("Configuration in {} has changed:".format(setuppath), out[0])
             self.assertIn('-                    "rev": "master"\n+                    "rev": "another-branch"', out[0])
-            out = self.runbbsetup("update --update-bb-conf='yes'")
+            out = self.runbbsetup(["update", "--update-bb-conf=yes"])
             if c in ('gadget', 'gizmo'):
                 self.assertIn("Leaving the previous configuration in {}/build/conf-backup.".format(setuppath), out[0])
                 self.assertIn('-{}+{}'.format(prev_test_file_content, test_file_content), out[0])
@@ -438,7 +438,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
                 with open(f"{setuppath}/build/conf/{f}", "w") as fd:
                     fd.write("deadbeef")
             sums_before = _conf_chksum(f"{setuppath}/build/conf")
-            out = self.runbbsetup("update --update-bb-conf='no'")
+            out = self.runbbsetup(["update", "--update-bb-conf=no"])
             sums_after = _conf_chksum(f"{setuppath}/build/conf")
             self.assertEqual(sums_before, sums_after)
 
@@ -467,7 +467,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
         json_1 = self.add_local_json_config_to_registry('test-config-1.conf.json', self.testrepopath)
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         _check_local_sources(setuppath)
         _check_layer_backups(layers_path, 1)
 
@@ -476,14 +476,14 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         self.git(['clone', prev_path, self.testrepopath], cwd=self.tempdir)
         json_1 = self.add_local_json_config_to_registry('test-config-1.conf.json', self.testrepopath)
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         _check_local_sources(setuppath)
         _check_layer_backups(layers_path, 1)
 
         self.testrepopath = prev_path
         json_1 = self.add_json_config_to_registry('test-config-1.conf.json', branch, branch)
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         self.check_setupdir_files(setuppath, test_file_content)
         _check_layer_backups(layers_path, 1)
 
@@ -493,7 +493,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         test_file_content = "modified-again\n"
         self.add_file_to_testrepo('test-file', test_file_content)
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         _check_layer_backups(layers_path, 1)
 
         ## edit a file and make a commit such that no rebase conflicts occur
@@ -503,7 +503,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         self.git(['commit', '-m', 'Adding a local modification'], cwd=layer_path)
         test_file_content = "modified-again-and-again\n"
         self.add_file_to_testrepo('test-file', test_file_content)
-        out = self.runbbsetup("update --update-bb-conf='yes'")
+        out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         _check_layer_backups(layers_path, 1)
 
         ## edit a file and make a commit in a way that causes a rebase conflict
@@ -514,7 +514,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         test_file_content = "remotely-modified\n"
         self.add_file_to_testrepo('test-file', test_file_content)
         with self.assertRaisesRegex(bb.process.ExecutionError, "Merge conflict in test-file"):
-            out = self.runbbsetup("update --update-bb-conf='yes'")
+            out = self.runbbsetup(["update", "--update-bb-conf=yes"])
         _check_layer_backups(layers_path, 1)
 
         # check source overrides, local sources provided with symlinks, and custom setup dir name
@@ -531,12 +531,12 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         override_filename = 'source-overrides.json'
         custom_setup_dir = 'special-setup-dir'
         self.add_file_to_testrepo(override_filename, source_override_content)
-        out = self.runbbsetup("init --non-interactive --source-overrides {} --setup-dir-name {} test-config-1 gadget".format(os.path.join(self.testrepopath, override_filename), custom_setup_dir))
+        out = self.runbbsetup(["init", "--non-interactive", "--source-overrides", os.path.join(self.testrepopath, override_filename), "--setup-dir-name", custom_setup_dir, "test-config-1", "gadget"])
         _check_local_sources(custom_setup_dir)
 
         # same but use command line options to specify local overrides
         custom_setup_dir = 'special-setup-dir-with-cmdline-overrides'
-        out = self.runbbsetup("init --non-interactive -L test-repo {} --setup-dir-name {} test-config-1 gadget".format(self.testrepopath, custom_setup_dir))
+        out = self.runbbsetup(["init", "--non-interactive", "-L", "test-repo", self.testrepopath, "--setup-dir-name", custom_setup_dir, "test-config-1", "gadget"])
         _check_local_sources(custom_setup_dir)
 
     def test_vscode(self):
@@ -544,12 +544,12 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
             del os.environ['BBPATH']
         os.chdir(self.tempdir)
 
-        self.runbbsetup("settings set default registry 'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath))
+        self.runbbsetup(["settings", "set", "default", "registry", "'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath)])
         self.add_file_to_testrepo('test-file', 'initial\n')
         self.add_json_config_to_registry('test-config-1.conf.json', 'master', 'master')
 
         # --init-vscode should create bitbake.code-workspace
-        self.runbbsetup("init --non-interactive --init-vscode test-config-1 gadget")
+        self.runbbsetup(["init", "--non-interactive", "--init-vscode", "test-config-1", "gadget"])
         setuppath = self.get_setup_path('test-config-1', 'gadget')
         workspace_file = os.path.join(setuppath, 'bitbake.code-workspace')
         self.assertTrue(os.path.exists(workspace_file),
@@ -595,7 +595,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
                          settings.get('python.autoComplete.extraPaths'))
 
         # --no-init-vscode should NOT create a workspace file
-        self.runbbsetup("init --non-interactive --no-init-vscode test-config-1 gadget-notemplate")
+        self.runbbsetup(["init", "--non-interactive", "--no-init-vscode", "test-config-1", "gadget-notemplate"])
         notemplate_path = self.get_setup_path('test-config-1', 'gadget-notemplate')
         self.assertFalse(
             os.path.exists(os.path.join(notemplate_path, 'bitbake.code-workspace')),
@@ -610,7 +610,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
         self.add_file_to_testrepo('test-file', 'updated\n')
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        self.runbbsetup("update --update-bb-conf='no'")
+        self.runbbsetup(["update", "--update-bb-conf=no"])
         del os.environ['BBPATH']
 
         with open(workspace_file) as f:
@@ -625,7 +625,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         with open(workspace_file, 'w') as f:
             f.write('{invalid json')
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
-        self.runbbsetup("update --update-bb-conf='no'")
+        self.runbbsetup(["update", "--update-bb-conf=no"])
         del os.environ['BBPATH']
         with open(workspace_file) as f:
             content = f.read()
@@ -654,10 +654,10 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
             del os.environ['BBPATH']
         os.chdir(self.tempdir)
 
-        self.runbbsetup("settings set default registry 'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath))
+        self.runbbsetup(["settings", "set", "default", "registry", "'git://{};protocol=file;branch=master;rev=master'".format(self.registrypath)])
         self.add_file_to_testrepo('test-file', 'initial\n')
         self.add_json_config_to_registry('test-config-1.conf.json', 'master', 'master')
-        self.runbbsetup("init --non-interactive test-config-1 gadget")
+        self.runbbsetup(["init", "--non-interactive", "test-config-1", "gadget"])
 
         setuppath = self.get_setup_path('test-config-1', 'gadget')
         layer_path = os.path.join(setuppath, 'layers', 'test-repo')
@@ -672,7 +672,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
         os.environ['BBPATH'] = os.path.join(setuppath, 'build')
         with self.assertRaises(bb.process.ExecutionError) as ctx:
-            self.runbbsetup("update --update-bb-conf='no'")
+            self.runbbsetup(["update", "--update-bb-conf=no"])
         self.assertIn('has uncommitted changes', str(ctx.exception))
         self.assertIn('--rebase-conflicts-strategy=backup', str(ctx.exception))
         # No backup directory must have been created.
@@ -680,7 +680,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
                          "abort strategy must not create any backup")
 
         # Scenario 2: same uncommitted change, 'backup' strategy
-        out = self.runbbsetup("update --update-bb-conf='no' --rebase-conflicts-strategy=backup")
+        out = self.runbbsetup(["update", "--update-bb-conf=no", "--rebase-conflicts-strategy=backup"])
         # One backup directory must now exist.
         self.assertEqual(self._count_layer_backups(layers_path), 1,
                          "backup strategy must create exactly one backup")
@@ -695,7 +695,7 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
 
         # Scenario 3: committed conflicting change, 'backup' strategy
         # Re-initialise a fresh setup so we start from a clean state.
-        self.runbbsetup("init --non-interactive --setup-dir-name rebase-conflict-setup test-config-1 gadget")
+        self.runbbsetup(["init", "--non-interactive", "--setup-dir-name", "rebase-conflict-setup", "test-config-1", "gadget"])
         conflict_setup = os.path.join(self.tempdir, 'bitbake-builds', 'rebase-conflict-setup')
         conflict_layer = os.path.join(conflict_setup, 'layers', 'test-repo')
         conflict_layers = os.path.join(conflict_setup, 'layers')
@@ -713,13 +713,13 @@ print("BBPATH is {{}}".format(os.environ["BBPATH"]))
         # Default stop strategy must still fail with a conflict error and include
         # the --rebase-conflicts-strategy=backup hint (same handler as LocalModificationsError).
         with self.assertRaises(bb.process.ExecutionError) as ctx:
-            self.runbbsetup("update --update-bb-conf='no'")
+            self.runbbsetup(["update", "--update-bb-conf=no"])
         self.assertIn('Merge conflict in test-file', str(ctx.exception))
         self.assertIn('--rebase-conflicts-strategy=backup', str(ctx.exception))
         self.assertEqual(self._count_layer_backups(conflict_layers), 0)
 
         # Backup strategy must succeed: backup the conflicted dir and re-clone.
-        self.runbbsetup("update --update-bb-conf='no' --rebase-conflicts-strategy=backup")
+        self.runbbsetup(["update", "--update-bb-conf=no", "--rebase-conflicts-strategy=backup"])
         self.assertEqual(self._count_layer_backups(conflict_layers), 1,
                          "backup strategy must create exactly one backup after a conflict")
         with open(os.path.join(conflict_layer, 'test-file')) as f:
#4 lore raw

PATCH 5/6 lib/layerindex/cooker: Convert string commands to lists where possible

In reply to Richard Purdie

String commands have to be processed by subprocess using shell=True which is suboptimal for all kinds of reasons, including quoting. Convert to use a list where possible to improve efficiency and the robustness of code. It also means copy and paste will be more likely to use the preferred form.

---
 lib/layerindexlib/cooker.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
lib/layerindexlib/cooker.py +8 -8
index ced3e063604..82d77335f9a 100644
@@ -55,10 +55,10 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
         bb_path = os.path.dirname(bb_path)  # .../bitbake/lib/layerindex
         bb_path = os.path.dirname(bb_path)  # .../bitbake/lib
         bb_path = os.path.dirname(bb_path)  # .../bitbake
-        bb_path = self._run_command('git rev-parse --show-toplevel', os.path.dirname(__file__), default=bb_path)
-        bb_branch = self._run_command('git rev-parse --abbrev-ref HEAD', bb_path, default="<unknown>")
-        bb_rev = self._run_command('git rev-parse HEAD', bb_path, default="<unknown>")
-        for remotes in self._run_command('git remote -v', bb_path, default="").split("\n"):
+        bb_path = self._run_command(['git', 'rev-parse', '--show-toplevel'], os.path.dirname(__file__), default=bb_path)
+        bb_branch = self._run_command(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], bb_path, default="<unknown>")
+        bb_rev = self._run_command(['git', 'rev-parse', 'HEAD'], bb_path, default="<unknown>")
+        for remotes in self._run_command(['git', 'remote', '-v'], bb_path, default="").split("\n"):
             remote = remotes.split("\t")[1].split(" ")[0]
             if "(fetch)" == remotes.split("\t")[1].split(" ")[1]:
                 bb_remote = self._handle_git_remote(remote)
@@ -118,14 +118,14 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
             layerbranch = "<unknown>"
 
             if os.path.isdir(layerpath):
-                layerbasepath = self._run_command('git rev-parse --show-toplevel', layerpath, default=layerpath)
+                layerbasepath = self._run_command(['git', 'rev-parse', '--show-toplevel'], layerpath, default=layerpath)
                 if os.path.abspath(layerpath) != os.path.abspath(layerbasepath):
                     layersubdir = os.path.abspath(layerpath)[len(layerbasepath) + 1:]
 
-                layerbranch = self._run_command('git rev-parse --abbrev-ref HEAD', layerpath, default="<unknown>")
-                layerrev = self._run_command('git rev-parse HEAD', layerpath, default="<unknown>")
+                layerbranch = self._run_command(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], layerpath, default="<unknown>")
+                layerrev = self._run_command(['git', 'rev-parse', 'HEAD'], layerpath, default="<unknown>")
 
-                for remotes in self._run_command('git remote -v', layerpath, default="").split("\n"):
+                for remotes in self._run_command(['git', 'remote', '-v'], layerpath, default="").split("\n"):
                     if not remotes:
                         layerurl = self._handle_git_remote(layerpath)
                     else:
#5 lore raw

PATCH 6/6 bb/fetch2: Improve runfetchcmd environment handling

In reply to Richard Purdie

One of the reasons for using shell=True in runfetchcmd is due to environment manipulations. There are better ways to do this directly with subprocess' env parameter. Switch to doing this, paving the way for fewer shell=True process calls.

---
 lib/bb/fetch2/__init__.py | 12 ++++++++----
 lib/bb/fetch2/git.py      | 16 ++++++++--------
 lib/bb/fetch2/svn.py      |  2 +-
 3 files changed, 17 insertions(+), 13 deletions(-)
lib/bb/fetch2/__init__.py +8 -4
index ce7456b60f1..564147603b5 100644
@@ -933,7 +933,7 @@ def get_fetcher_environment(d):
             newenv[name] = value
     return newenv
 
-def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None):
+def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None, extraenv=None):
     """
     Run cmd returning the command output
     Raise an error if interrupted or cmd fails
@@ -958,13 +958,17 @@ def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None):
         d.setVar("PR", "fetcheravoidrecurse")
 
     origenv = d.getVar("BB_ORIGENV", False)
+    env = os.environ.copy()
     for var in exportvars:
         val = d.getVar(var) or (origenv and origenv.getVar(var))
         if val:
-            cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd)
+            env[var] = val
+
+    for var in (extraenv or []):
+        env[var] = extraenv[var]
 
     # Disable pseudo as it may affect ssh, potentially causing it to hang.
-    cmd = 'export PSEUDO_DISABLED=1; ' + cmd
+    env["PSEUDO_DISABLED"] = "1"
 
     if workdir:
         logger.debug("Running '%s' in %s" % (cmd, workdir))
@@ -975,7 +979,7 @@ def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None):
     error_message = ""
 
     try:
-        (output, errors) = bb.process.run(cmd, log=log, shell=True, stderr=subprocess.PIPE, cwd=workdir)
+        (output, errors) = bb.process.run(cmd, log=log, shell=True, stderr=subprocess.PIPE, cwd=workdir, env=env)
         success = True
     except bb.process.NotFoundError as e:
         error_message = "Fetch command %s not found" % (e.command)
lib/bb/fetch2/git.py +8 -8
index d305eb878e8..793b3570b11 100644
@@ -397,8 +397,8 @@ class Git(FetchMethod):
                     if 'mirror' in output:
                         runfetchcmd("%s remote rm mirror" % ud.basecmd, d, workdir=ud.clonedir)
                     runfetchcmd("%s remote add --mirror=fetch mirror %s" % (ud.basecmd, tmpdir), d, workdir=ud.clonedir)
-                    fetch_cmd = "LANG=C %s fetch -f --update-head-ok  --progress mirror " % (ud.basecmd)
-                    runfetchcmd(fetch_cmd, d, workdir=ud.clonedir)
+                    fetch_cmd = "%s fetch -f --update-head-ok  --progress mirror " % (ud.basecmd)
+                    runfetchcmd(fetch_cmd, d, workdir=ud.clonedir, extraenv={'LANG':'C'})
         repourl = self._get_repo_url(ud)
 
         needs_clone = False
@@ -407,7 +407,7 @@ class Git(FetchMethod):
             # repository in which case it needs to be deleted and re-cloned.
             try:
                 # Since clones can be bare, use --absolute-git-dir instead of --show-toplevel
-                output = runfetchcmd("LANG=C %s rev-parse --absolute-git-dir" % ud.basecmd, d, workdir=ud.clonedir)
+                output = runfetchcmd("%s rev-parse --absolute-git-dir" % ud.basecmd, d, workdir=ud.clonedir, extraenv={'LANG':'C'})
                 toplevel = output.rstrip()
 
                 if not bb.utils.path_is_descendant(toplevel, ud.clonedir):
@@ -434,7 +434,7 @@ class Git(FetchMethod):
                 objects = os.path.join(repourl_path, 'objects')
                 if os.path.isdir(objects) and not os.path.islink(objects):
                     repourl = repourl_path
-            clone_cmd = "LANG=C %s clone --bare --mirror %s %s --progress" % (ud.basecmd, shlex.quote(repourl), ud.clonedir)
+            clone_cmd = "%s clone --bare --mirror %s %s --progress" % (ud.basecmd, shlex.quote(repourl), ud.clonedir)
             if ud.proto.lower() != 'file':
                 bb.fetch2.check_network_access(d, clone_cmd, ud.url)
             progresshandler = GitProgressHandler(d)
@@ -456,7 +456,7 @@ class Git(FetchMethod):
 
             # When skipping fast initial shallow or the fast inital shallow clone failed:
             # Try again with an initial regular clone
-            runfetchcmd(clone_cmd, d, log=progresshandler)
+            runfetchcmd(clone_cmd, d, log=progresshandler, extraenv={'LANG':'C'})
 
         # Update the checkout if needed
         if self.clonedir_need_update(ud, d):
@@ -467,13 +467,13 @@ class Git(FetchMethod):
             runfetchcmd("%s remote add --mirror=fetch origin %s" % (ud.basecmd, shlex.quote(repourl)), d, workdir=ud.clonedir)
 
             if ud.nobranch:
-                fetch_cmd = "LANG=C %s fetch -f --progress %s refs/*:refs/*" % (ud.basecmd, shlex.quote(repourl))
+                fetch_cmd = "%s fetch -f --progress %s refs/*:refs/*" % (ud.basecmd, shlex.quote(repourl))
             else:
-                fetch_cmd = "LANG=C %s fetch -f --progress %s refs/heads/*:refs/heads/* refs/tags/*:refs/tags/*" % (ud.basecmd, shlex.quote(repourl))
+                fetch_cmd = "%s fetch -f --progress %s refs/heads/*:refs/heads/* refs/tags/*:refs/tags/*" % (ud.basecmd, shlex.quote(repourl))
             if ud.proto.lower() != 'file':
                 bb.fetch2.check_network_access(d, fetch_cmd, ud.url)
             progresshandler = GitProgressHandler(d)
-            runfetchcmd(fetch_cmd, d, log=progresshandler, workdir=ud.clonedir)
+            runfetchcmd(fetch_cmd, d, log=progresshandler, workdir=ud.clonedir, extraenv={'LANG':'C'})
             runfetchcmd("%s repack -adk" % ud.basecmd, d, workdir=ud.clonedir)
             runfetchcmd("%s pack-refs --all" % ud.basecmd, d, workdir=ud.clonedir)
             runfetchcmd("%s prune-packed" % ud.basecmd, d, workdir=ud.clonedir)
lib/bb/fetch2/svn.py +1 -1
index a097ffb76b5..b5862139fac 100644
@@ -193,7 +193,7 @@ class Svn(FetchMethod):
         """
         bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "log1"), ud.url)
 
-        output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "log1"), d, True)
+        output = runfetchcmd(self._buildsvncommand(ud, d, "log1"), d, True, extraenv={'LANG':'C', 'LC_ALL': 'C'})
 
         # skip the first line, as per output of svn log
         # then we expect the revision on the 2nd line