=== modified file 'linaro_image_tools/cmd_runner.py'
@@ -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'
@@ -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'
@@ -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'
@@ -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