From patchwork Thu Mar 24 22:22:13 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Lo=C3=AFc_Minier?= X-Patchwork-Id: 776 Return-Path: Delivered-To: unknown Received: from imap.gmail.com (74.125.159.109) by localhost6.localdomain6 with IMAP4-SSL; 08 Jun 2011 14:45:43 -0000 Delivered-To: patches@linaro.org Received: by 10.42.161.68 with SMTP id s4cs137645icx; Thu, 24 Mar 2011 15:22:14 -0700 (PDT) Received: by 10.216.230.231 with SMTP id j81mr16572weq.104.1301005333931; Thu, 24 Mar 2011 15:22:13 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id b44si543085wer.28.2011.03.24.15.22.13; Thu, 24 Mar 2011 15:22:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1Q2suz-0002NV-2U for ; Thu, 24 Mar 2011 22:22:13 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 105DE2E8014 for ; Thu, 24 Mar 2011 22:22:13 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: linaro-image-tools X-Launchpad-Branch: ~linaro-maintainers/linaro-image-tools/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 305 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [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> Date: Thu, 24 Mar 2011 22:22:13 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="12633"; Instance="initZopeless config overlay" X-Launchpad-Hash: 8ad7be1ea10790dbc11ade762dcdf8486b9aab01 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 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 === 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