diff mbox

[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
State Accepted
Headers show

Commit Message

Loïc Minier Feb. 1, 2011, 5:26 p.m. UTC
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 <lool@dooz.org>
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
diff mbox

Patch

=== 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.