diff mbox

[Branch,~linaro-maintainers/linaro-image-tools/trunk] Rev 305: Merge lp:~lool/linaro-image-tools/missing-sbin; fixes runs of the testsuite

Message ID 20110324222213.19222.35968.launchpad@loganberry.canonical.com
State Accepted
Headers show

Commit Message

Loïc Minier March 24, 2011, 10:22 p.m. UTC
Merge authors:
  Loïc Minier (lool)
Related merge proposals:
  https://code.launchpad.net/~lool/linaro-image-tools/missing-sbin/+merge/54693
  proposed by: Loïc Minier (lool)
  review: Approve - Guilherme Salgado (salgado)
------------------------------------------------------------
revno: 305 [merge]
fixes bug(s): https://launchpad.net/bugs/709517
committer: Loïc Minier <lool@dooz.org>
branch nick: linaro-image-tools
timestamp: Thu 2011-03-24 23:19:15 +0100
message:
  Merge lp:~lool/linaro-image-tools/missing-sbin; fixes runs of the testsuite
  when /sbin isn't in the PATH; LP: #709517.
modified:
  linaro_image_tools/cmd_runner.py
  linaro_image_tools/hwpack/packages.py
  linaro_image_tools/tests/test_cmd_runner.py
  linaro_image_tools/utils.py


--
lp:linaro-image-tools
https://code.launchpad.net/~linaro-maintainers/linaro-image-tools/trunk

You are subscribed to branch lp:linaro-image-tools.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-maintainers/linaro-image-tools/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'linaro_image_tools/cmd_runner.py'
--- linaro_image_tools/cmd_runner.py	2011-03-24 10:48:18 +0000
+++ linaro_image_tools/cmd_runner.py	2011-03-24 22:12:56 +0000
@@ -21,9 +21,19 @@ 
 import subprocess
 
 
+DEFAULT_PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
 SUDO_ARGS = ['sudo', '-E']
 
 
+def sanitize_path(env):
+    """Makes sure PATH is set and has important directories"""
+    dirs = env.get('PATH', DEFAULT_PATH).split(os.pathsep)
+    for d in DEFAULT_PATH.split(os.pathsep):
+        if d not in dirs:
+            dirs.append(d)
+    env['PATH'] = os.pathsep.join(dirs)
+
+
 def run(args, as_root=False, stdin=None, stdout=None, stderr=None):
     """Run the given command as a sub process.
 
@@ -59,6 +69,10 @@ 
         if env is None:
             env = os.environ.copy()
         env['LC_ALL'] = 'C'
+        # ensure a proper PATH before calling Popen
+        sanitize_path(os.environ)
+        # and for subcommands
+        sanitize_path(env)
         super(Popen, self).__init__(args, env=env, **kwargs)
 
     def wait(self):

=== modified file 'linaro_image_tools/hwpack/packages.py'
--- linaro_image_tools/hwpack/packages.py	2011-01-28 19:50:48 +0000
+++ linaro_image_tools/hwpack/packages.py	2011-03-24 21:47:55 +0000
@@ -34,6 +34,8 @@ 
 
 from debian.debfile import DebFile
 
+from linaro_image_tools import cmd_runner
+
 
 logger = logging.getLogger(__name__)
 
@@ -191,12 +193,12 @@ 
         with open(os.path.join(tmpdir, 'Packages'), 'w') as packages_file:
             packages_file.write(get_packages_file(local_debs, rel_to=tmpdir))
         if label:
-            subprocess.check_call(
+            proc = cmd_runner.run(
                 ['apt-ftparchive',
                  '-oAPT::FTPArchive::Release::Label=%s' % label,
                  'release',
                  tmpdir],
-                stdout=open(os.path.join(tmpdir, 'Release'), 'w'))
+                stdout=open(os.path.join(tmpdir, 'Release'), 'w')).wait()
         return 'file://%s ./' % (tmpdir, )
 
 
@@ -245,7 +247,7 @@ 
         env = os.environ
         env['LC_ALL'] = 'C'
         env['NO_PKG_MANGLE'] = '1'
-        proc = subprocess.Popen(
+        proc = cmd_runner.Popen(
             ['dpkg-deb', '-b', packaging_dir],
             env=env,
             stdout=subprocess.PIPE, stderr=subprocess.PIPE)

=== modified file 'linaro_image_tools/tests/test_cmd_runner.py'
--- linaro_image_tools/tests/test_cmd_runner.py	2011-03-24 17:58:26 +0000
+++ linaro_image_tools/tests/test_cmd_runner.py	2011-03-24 22:12:56 +0000
@@ -30,6 +30,27 @@ 
 sudo_args = " ".join(cmd_runner.SUDO_ARGS)
 
 
+class TestSanitizePath(TestCaseWithFixtures):
+    def setUp(self):
+        super(TestSanitizePath, self).setUp()
+        self.env = {}
+
+    def test_path_unset(self):
+        cmd_runner.sanitize_path(self.env)
+        self.assertEqual(cmd_runner.DEFAULT_PATH, self.env['PATH'])
+
+    def test_path_missing_dirs(self):
+        path = '/bin:/sbin:/foo:/usr/local/sbin'
+        self.env['PATH'] = path
+        cmd_runner.sanitize_path(self.env)
+        expected = '%s:/usr/local/bin:/usr/sbin:/usr/bin' % path
+        self.assertEqual(expected, self.env['PATH'])
+
+    def test_idempotent(self):
+        self.env['PATH'] = cmd_runner.DEFAULT_PATH
+        cmd_runner.sanitize_path(self.env)
+        self.assertEqual(cmd_runner.DEFAULT_PATH, self.env['PATH'])
+
 class TestCmdRunner(TestCaseWithFixtures):
 
     def test_run(self):

=== modified file 'linaro_image_tools/utils.py'
--- linaro_image_tools/utils.py	2011-03-24 18:41:59 +0000
+++ linaro_image_tools/utils.py	2011-03-24 21:47:55 +0000
@@ -73,8 +73,7 @@ 
     assert name != ""
     assert os.path.dirname(name) == ""
 
-    if not os.environ.has_key("PATH"):
-        os.environ["PATH"] = ":/bin:usr/bin"
+    cmd_runner.sanitize_path(os.environ)
 
     # default to searching in current directory when running from a bzr
     # checkout