From patchwork Tue Feb 1 17:26:28 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: 57 Return-Path: Delivered-To: unknown Received: from imap.gmail.com (74.125.159.109) by localhost6.localdomain6 with IMAP4-SSL; 08 Jun 2011 14:39:35 -0000 Delivered-To: patches@linaro.org Received: by 10.147.124.5 with SMTP id b5cs111747yan; Tue, 1 Feb 2011 09:26:30 -0800 (PST) Received: by 10.227.72.147 with SMTP id m19mr7941225wbj.131.1296581189261; Tue, 01 Feb 2011 09:26:29 -0800 (PST) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id d17si15307855wbd.17.2011.02.01.09.26.28; Tue, 01 Feb 2011 09:26:29 -0800 (PST) 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 1PkJzo-0008Ph-IY for ; Tue, 01 Feb 2011 17:26:28 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 8A7832E80AA for ; Tue, 1 Feb 2011 17:26:28 +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: 279 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-maintainers/linaro-image-tools/trunk] Rev 279: Merge lp:~lool/linaro-image-tools/optional-sudo; makes usage of sudo optional Message-Id: <20110201172628.8143.64441.launchpad@loganberry.canonical.com> Date: Tue, 01 Feb 2011 17:26:28 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="12274"; Instance="initZopeless config overlay" X-Launchpad-Hash: 819c5839e3bd4a45c0a777cb4ef27c5d5aafcc5d Merge authors: Loïc Minier (lool) Related merge proposals: https://code.launchpad.net/~lool/linaro-image-tools/optional-sudo/+merge/47907 proposed by: Loïc Minier (lool) review: Approve - James Westby (james-w) ------------------------------------------------------------ revno: 279 [merge] committer: Loïc Minier branch nick: linaro-image-tools timestamp: Tue 2011-02-01 18:23:43 +0100 message: Merge lp:~lool/linaro-image-tools/optional-sudo; makes usage of sudo optional so that one can run tools directly as root. modified: linaro-hwpack-install linaro-media-create linaro_media_create/cmd_runner.py linaro_media_create/tests/test_media_create.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-hwpack-install' --- linaro-hwpack-install 2011-01-28 19:50:48 +0000 +++ linaro-hwpack-install 2011-01-29 16:35:06 +0000 @@ -38,6 +38,11 @@ APT_GET_OPTIONS="Dir::Etc::SourceList=${SOURCES_LIST_FILE}" SUPPORTED_FORMATS="1.0" # A space-separated list of hwpack formats. +sudo="sudo" +if [ $(id -u) -eq 0 ]; then + sudo="" +fi + die() { echo -e "$@" exit 1 @@ -79,7 +84,7 @@ # Ensure our temp dir and apt sources are removed. echo -n "Cleaning up ..." rm -rf $TEMP_DIR - sudo apt-get update -qq + $sudo apt-get update -qq echo "Done" } @@ -128,14 +133,14 @@ done < $stripped_file if [ $should_install -eq 1 ]; then - sudo cp $file /etc/apt/sources.list.d/hwpack.$filename + $sudo cp $file /etc/apt/sources.list.d/hwpack.$filename fi done # Import the OpenPGP keys for the files installed above. for filename in $(ls "${HWPACK_DIR}"/sources.list.d.gpg/); do file="${HWPACK_DIR}"/sources.list.d.gpg/$filename - sudo apt-key add $file + $sudo apt-key add $file done # Add one extra apt source for the packages included in the hwpack and make @@ -145,7 +150,7 @@ cat /etc/apt/sources.list >> "$SOURCES_LIST_FILE" echo "Updating apt package lists ..." -sudo apt-get -o "$APT_GET_OPTIONS" update -q +$sudo apt-get -o "$APT_GET_OPTIONS" update -q echo -n "Installing packages ..." @@ -199,11 +204,11 @@ done fi -sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" install ${packages} +$sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" install ${packages} if [ "$DEP_PACKAGE_PRESENT" == "yes" ]; then if [ -n "${to_be_installed}" ]; then - sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" markauto ${to_be_installed} + $sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" markauto ${to_be_installed} fi fi === modified file 'linaro-media-create' --- linaro-media-create 2011-01-28 19:50:48 +0000 +++ linaro-media-create 2011-01-29 16:46:33 +0000 @@ -22,7 +22,6 @@ import os import sys import tempfile -from subprocess import Popen from linaro_media_create.boards import ( board_configs, @@ -42,7 +41,10 @@ ensure_command, is_arm_host, ) -from linaro_media_create import get_args_parser +from linaro_media_create import ( + cmd_runner, + get_args_parser + ) # Just define the global variables TMP_DIR = None @@ -60,18 +62,20 @@ Before doing so, make sure BOOT_DISK and ROOT_DISK are not mounted. """ devnull = open('/dev/null', 'w') - # Use raw subprocess.Popen as we don't want to stop in case the - # commands end with a non-zero return code. - if BOOT_DISK is not None: - Popen(['sudo', 'umount', BOOT_DISK], - stdout=devnull, stderr=devnull).wait() - if ROOT_DISK is not None: - Popen(['sudo', 'umount', ROOT_DISK], - stdout=devnull, stderr=devnull).wait() + # ignore non-zero return codes + try: + if BOOT_DISK is not None: + cmd_runner.Popen(['umount', BOOT_DISK], + stdout=devnull, stderr=devnull, as_root=True).wait() + if ROOT_DISK is not None: + cmd_runner.Popen(['umount', ROOT_DISK], + stdout=devnull, stderr=devnull, as_root=True).wait() + except SubcommandNonZeroReturnValue: + pass # Remove TMP_DIR as root because some files written there are # owned by root. if TMP_DIR is not None: - Popen(['sudo', 'rm', '-rf', TMP_DIR]).wait() + cmd_runner.Popen(['rm', '-rf', TMP_DIR], as_root=True).wait() def ensure_required_commands(args): === modified file 'linaro_media_create/cmd_runner.py' --- linaro_media_create/cmd_runner.py 2011-01-28 19:50:48 +0000 +++ linaro_media_create/cmd_runner.py 2011-01-29 16:33:38 +0000 @@ -37,10 +37,10 @@ """ assert isinstance(args, (list, tuple)), ( "The command to run must be a list or tuple, found: %s" % type(args)) - # TODO: We might want to always use 'sudo -E' here to avoid problems like - # https://launchpad.net/bugs/673570 - if as_root: + if as_root and os.getuid() != 0: args = args[:] + # TODO: We might want to always use 'sudo -E' here to avoid problems + # like https://launchpad.net/bugs/673570 args.insert(0, 'sudo') return Popen(args, stdin=stdin, stdout=stdout, stderr=stderr) === modified file 'linaro_media_create/tests/test_media_create.py' --- linaro_media_create/tests/test_media_create.py 2011-02-01 13:16:43 +0000 +++ linaro_media_create/tests/test_media_create.py 2011-02-01 17:23:43 +0000 @@ -449,11 +449,18 @@ self.assertEqual(0, proc.returncode) self.assertEqual([['foo', 'bar', 'baz']], fixture.mock.calls) - def test_run_as_root(self): + def test_run_as_root_with_sudo(self): fixture = self.useFixture(MockCmdRunnerPopenFixture()) + self.useFixture(MockSomethingFixture(os, 'getuid', lambda: 1000)) cmd_runner.run(['foo', 'bar'], as_root=True).wait() self.assertEqual([['sudo', 'foo', 'bar']], fixture.mock.calls) + def test_run_as_root_as_root(self): + fixture = self.useFixture(MockCmdRunnerPopenFixture()) + self.useFixture(MockSomethingFixture(os, 'getuid', lambda: 0)) + cmd_runner.run(['foo', 'bar'], as_root=True).wait() + self.assertEqual([['foo', 'bar']], fixture.mock.calls) + def test_run_succeeds_on_zero_return_code(self): proc = cmd_runner.run(['true']) # Need to wait() here as we're using the real Popen.