Cleaned up a build_chromeos.py a little (as per gpylint).
 Moved the make_chroot code out as a function and build_tc.py now uses that function.

PRESUBMIT=passed
R=raymes,bjanakiraman
DELTA=55  (23 added, 13 deleted, 19 changed)
OCL=45276-p2
RCL=45278-p2
RDATE=2010/11/17 14:26:44


P4 change: 42606263
diff --git a/v14/build_chromeos.py b/v14/build_chromeos.py
index 954bd0e..a92c31c 100755
--- a/v14/build_chromeos.py
+++ b/v14/build_chromeos.py
@@ -13,8 +13,10 @@
 import optparse
 import os
 import sys
+import tc_enter_chroot
 from utils import utils
 
+
 def Usage(parser, message):
   print "ERROR: " + message
   parser.print_help()
@@ -23,16 +25,33 @@
 
 def ExecuteCommandInChroot(chromeos_root, toolchain_root, command,
                            return_output=False, chrome_root=""):
+  """Executes a command in the chroot."""
   chrome_mount = ""
   if chrome_root:
     chrome_mount = "--chrome_root=" + chromeos_root + "/" + chrome_root
-  commands = []
-  tc_enter_chroot = (os.path.dirname(os.path.abspath(__file__)) +
-                     "/tc-enter-chroot.sh")
-  commands.append("%s --chromeos_root=%s --toolchain_root=%s %s -- \"%s\""
-                  % (tc_enter_chroot, chromeos_root, toolchain_root,
-                     chrome_mount, command))
-  return utils.RunCommands(commands, return_output)
+  argv=[os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py",
+        "--chromeos_root=" + chromeos_root,
+        "--toolchain_root=" + toolchain_root,
+        chrome_mount,
+        "--",
+        command]
+  return tc_enter_chroot.Main(argv)
+
+
+def MakeChroot(chromeos_root, clobber_chroot=False):
+  """Make a chroot given a chromeos checkout."""
+  if (not os.path.isdir(chromeos_root + "/chroot")
+      or clobber_chroot):
+    commands = []
+    commands.append("cd " + chromeos_root + "/src/scripts")
+    clobber_chroot = ""
+    if clobber_chroot:
+      clobber_chroot = "--replace"
+    commands.append("./make_chroot --fast " + clobber_chroot)
+    ret = utils.RunCommands(commands)
+    utils.AssertTrue(ret == 0, "make_chroot failed")
+  else:
+    utils.main_logger.LogOutput("Did not make_chroot because it already exists")
 
 
 def Main():
@@ -72,19 +91,7 @@
   if options.board is None:
     Usage(parser, "--board must be set")
 
-  # Make chroot
-  if (not os.path.isdir(options.chromeos_root + "/chroot")
-      or options.clobber_chroot):
-    commands = []
-    commands.append("cd " + options.chromeos_root + "/src/scripts")
-    clobber_chroot = ""
-    if options.clobber_chroot:
-      clobber_chroot = "--replace"
-    commands.append("./make_chroot --fast " + clobber_chroot)
-    ret = utils.RunCommands(commands)
-    utils.AssertTrue(ret == 0, "make_chroot failed")
-  else:
-    utils.main_logger.LogOutput("Did not make_chroot because it already exists")
+  MakeChroot(options.chromeos_root, options.clobber_chroot)
 
   # Setup board
   if not os.path.isdir(options.chromeos_root + "/chroot/build/"
@@ -109,7 +116,6 @@
     utils.main_logger.LogOutput("Did not setup_board because it already exists")
 
   # Modify make.conf to add CFLAGS/CXXFLAGS/LDFLAGS
-  commands = []
   ret1 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
                                 "[ -e /build/%s/etc/make.conf.orig ] || "
                                 "sudo mv /build/%s/etc/make.conf "
diff --git a/v14/build_tc.py b/v14/build_tc.py
index ea05156..d100947 100755
--- a/v14/build_tc.py
+++ b/v14/build_tc.py
@@ -13,6 +13,7 @@
 import optparse
 import sys
 import tc_enter_chroot
+import build_chromeos
 from utils import utils
 
 # Common initializations
@@ -50,6 +51,8 @@
     parser.print_help()
     sys.exit()
 
+  build_chromeos.MakeChroot(options.chromeos_root)
+
   portage_flags = ""
   if options.binary == True:
     # FIXME(asharif): This should be using --usepkg but that was not working.
@@ -127,8 +130,9 @@
   libc_version = "2.10.1-r1"
   kernel_version = "2.6.30-r1"
 
-  sys.argv = ["--chromeos_root=" + chromeos_root,
-              "--toolchain_root=" + toolchain_root]
+  argv = [rootdir + "tc_enter_chroot.py",
+          "--chromeos_root=" + chromeos_root,
+          "--toolchain_root=" + toolchain_root]
 
   env += " "
 
@@ -141,8 +145,8 @@
 
   if uninstall == True:
     command += " crossdev " + tflag + target
-    sys.argv.append(command)
-    retval = tc_enter_chroot.Main()
+    argv.append(command)
+    retval = tc_enter_chroot.Main(argv)
     return retval
 
   if incremental_component == "binutils":
@@ -163,8 +167,8 @@
                 " --kernel " + kernel_version +
                 crossdev_flags)
 
-  sys.argv.append(command)
-  retval = tc_enter_chroot.Main()
+  argv.append(command)
+  retval = tc_enter_chroot.Main(argv)
   return retval
 
 if __name__ == "__main__":
diff --git a/v14/tc_enter_chroot.py b/v14/tc_enter_chroot.py
old mode 100644
new mode 100755
index b13a188..680e950
--- a/v14/tc_enter_chroot.py
+++ b/v14/tc_enter_chroot.py
@@ -20,7 +20,7 @@
 utils.InitLogger(rootdir, basename)
 
 
-def Main():
+def Main(argv):
   """The main function."""
   parser = optparse.OptionParser()
   parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
@@ -28,7 +28,7 @@
   parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
                     help="Toolchain root directory.")
 
-  (options, args) = parser.parse_args()
+  (options, args) = parser.parse_args(argv)
 
   if options.chromeos_root is None:
     chromeos_root = "../.."
@@ -77,8 +77,8 @@
   # Now call enter_chroot with the rest of the arguments.
   command = "./enter_chroot.sh"
 
-  if len(args) != 0:
-    command += " -- " + " ".join(args)
+  if len(args) > 1:
+    command += " -- " + " ".join(args[1:])
     retval = utils.RunCommand(command)
     return retval
   else:
@@ -106,5 +106,5 @@
 
 
 if __name__ == "__main__":
-  Main()
+  Main(sys.argv)