Moved build-related functions from utils.py to cmd_build.py
authorEd Bartosh <eduard.bartosh@intel.com>
Fri, 1 Jun 2012 10:52:47 +0000 (13:52 +0300)
committerEd Bartosh <eduard.bartosh@intel.com>
Mon, 4 Jun 2012 10:11:19 +0000 (13:11 +0300)
Change-Id: I9dd5c078d46496f73a3f322d0c73e4302c9ea303

gitbuildsys/cmd_build.py
gitbuildsys/utils.py

index 17e6cb39b8e4e2401fab255b03940e9d3b6f1265..5b66a56f57079663c05f0b449635f3a8776344d4 100644 (file)
@@ -26,6 +26,7 @@ import urlparse
 
 import msger
 import utils
+import runner
 import errors
 from conf import configmgr
 
@@ -66,6 +67,87 @@ supportedarchs = [
             'armv7l',
           ]
 
+def get_processors():
+    """
+    get number of processors (online) based on
+    SC_NPROCESSORS_ONLN (returns 1 if config name does not exist).
+    """
+    try:
+        return os.sysconf('SC_NPROCESSORS_ONLN')
+    except ValueError:
+        return 1
+
+def get_hostarch():
+    hostarch = os.uname()[4]
+    if hostarch == 'i686':
+        hostarch = 'i586'
+    return hostarch
+
+def find_binary_path(binary):
+    if os.environ.has_key("PATH"):
+        paths = os.environ["PATH"].split(":")
+    else:
+        paths = []
+        if os.environ.has_key("HOME"):
+            paths += [os.environ["HOME"] + "/bin"]
+        paths += ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin",
+                  "/usr/bin", "/sbin", "/bin"]
+
+    for path in paths:
+        bin_path = "%s/%s" % (path, binary)
+        if os.path.exists(bin_path):
+            return bin_path
+    return None
+
+def is_statically_linked(binary):
+    return ", statically linked, " in runner.outs(['file', binary])
+
+def setup_qemu_emulator():
+    # mount binfmt_misc if it doesn't exist
+    if not os.path.exists("/proc/sys/fs/binfmt_misc"):
+        modprobecmd = find_binary_path("modprobe")
+        runner.show([modprobecmd, "binfmt_misc"])
+    if not os.path.exists("/proc/sys/fs/binfmt_misc/register"):
+        mountcmd = find_binary_path("mount")
+        runner.show([mountcmd, "-t", "binfmt_misc", "none",
+                     "/proc/sys/fs/binfmt_misc"])
+
+    # qemu_emulator is a special case, we can't use find_binary_path
+    # qemu emulator should be a statically-linked executable file
+    qemu_emulator = "/usr/bin/qemu-arm"
+    if not os.path.exists(qemu_emulator) or \
+           not is_statically_linked(qemu_emulator):
+        qemu_emulator = "/usr/bin/qemu-arm-static"
+    if not os.path.exists(qemu_emulator):
+        raise errors.QemuError("Please install a statically-linked qemu-arm")
+
+    # disable selinux, selinux will block qemu emulator to run
+    if os.path.exists("/usr/sbin/setenforce"):
+        msger.info('Try to disable selinux')
+        runner.show(["/usr/sbin/setenforce", "0"])
+
+    node = "/proc/sys/fs/binfmt_misc/arm"
+    if is_statically_linked(qemu_emulator) and os.path.exists(node):
+        return qemu_emulator
+
+    # unregister it if it has been registered and
+    # is a dynamically-linked executable
+    if not is_statically_linked(qemu_emulator) and os.path.exists(node):
+        qemu_unregister_string = "-1\n"
+        fds = open("/proc/sys/fs/binfmt_misc/arm", "w")
+        fds.write(qemu_unregister_string)
+        fds.close()
+
+    # register qemu emulator for interpreting other arch executable file
+    if not os.path.exists(node):
+        qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator
+        fds = open("/proc/sys/fs/binfmt_misc/register", "w")
+        fds.write(qemu_arm_string)
+        fds.close()
+
+    return qemu_emulator
+
+
 def do(opts, args):
 
     workdir = os.getcwd()
@@ -74,7 +156,7 @@ def do(opts, args):
     if len(args) == 1:
         workdir = args[0]
 
-    hostarch = utils.get_hostarch()
+    hostarch = get_hostarch()
     buildarch = hostarch
     if opts.arch:
         if opts.arch in buildarchmap:
@@ -107,7 +189,7 @@ def do(opts, args):
     if suwrapper:
         cmd = [suwrapper] + cmd
 
-    build_jobs = utils.get_processors()
+    build_jobs = get_processors()
     if build_jobs > 1:
         cmd += ['--jobs=%s' % build_jobs]
     if opts.clean:
@@ -132,7 +214,7 @@ def do(opts, args):
 
     if buildarch.startswith('arm'):
         try:
-            utils.setup_qemu_emulator()
+            setup_qemu_emulator()
         except errors.QemuError, exc:
             msger.error('%s' % exc)
 
index 289eec62f8d720a154b4f0e86fcfcd6de1875843..3c2e91aed0031b639a9edc526747e9cff7fe5d2a 100644 (file)
@@ -22,9 +22,6 @@ import tempfile
 import shutil
 
 import msger
-import runner
-import errors
-
 
 class Workdir(object):
     def __init__(self, path):
@@ -37,86 +34,6 @@ class Workdir(object):
     def __exit__(self, _type, _value, _tb):
         os.chdir(self._cwd)
 
-def get_processors():
-    """
-    get number of processors (online) based on
-    SC_NPROCESSORS_ONLN (returns 1 if config name does not exist).
-    """
-    try:
-        return os.sysconf('SC_NPROCESSORS_ONLN')
-    except ValueError:
-        return 1
-
-def get_hostarch():
-    hostarch = os.uname()[4]
-    if hostarch == 'i686':
-        hostarch = 'i586'
-    return hostarch
-
-def find_binary_path(binary):
-    if os.environ.has_key("PATH"):
-        paths = os.environ["PATH"].split(":")
-    else:
-        paths = []
-        if os.environ.has_key("HOME"):
-            paths += [os.environ["HOME"] + "/bin"]
-        paths += ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin",
-                  "/usr/bin", "/sbin", "/bin"]
-
-    for path in paths:
-        bin_path = "%s/%s" % (path, binary)
-        if os.path.exists(bin_path):
-            return bin_path
-    return None
-
-def is_statically_linked(binary):
-    return ", statically linked, " in runner.outs(['file', binary])
-
-def setup_qemu_emulator():
-    # mount binfmt_misc if it doesn't exist
-    if not os.path.exists("/proc/sys/fs/binfmt_misc"):
-        modprobecmd = find_binary_path("modprobe")
-        runner.show([modprobecmd, "binfmt_misc"])
-    if not os.path.exists("/proc/sys/fs/binfmt_misc/register"):
-        mountcmd = find_binary_path("mount")
-        runner.show([mountcmd, "-t", "binfmt_misc", "none",
-                     "/proc/sys/fs/binfmt_misc"])
-
-    # qemu_emulator is a special case, we can't use find_binary_path
-    # qemu emulator should be a statically-linked executable file
-    qemu_emulator = "/usr/bin/qemu-arm"
-    if not os.path.exists(qemu_emulator) or \
-           not is_statically_linked(qemu_emulator):
-        qemu_emulator = "/usr/bin/qemu-arm-static"
-    if not os.path.exists(qemu_emulator):
-        raise errors.QemuError("Please install a statically-linked qemu-arm")
-
-    # disable selinux, selinux will block qemu emulator to run
-    if os.path.exists("/usr/sbin/setenforce"):
-        msger.info('Try to disable selinux')
-        runner.show(["/usr/sbin/setenforce", "0"])
-
-    node = "/proc/sys/fs/binfmt_misc/arm"
-    if is_statically_linked(qemu_emulator) and os.path.exists(node):
-        return qemu_emulator
-
-    # unregister it if it has been registered and
-    # is a dynamically-linked executable
-    if not is_statically_linked(qemu_emulator) and os.path.exists(node):
-        qemu_unregister_string = "-1\n"
-        fds = open("/proc/sys/fs/binfmt_misc/arm", "w")
-        fds.write(qemu_unregister_string)
-        fds.close()
-
-    # register qemu emulator for interpreting other arch executable file
-    if not os.path.exists(node):
-        qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator
-        fds = open("/proc/sys/fs/binfmt_misc/register", "w")
-        fds.write(qemu_arm_string)
-        fds.close()
-
-    return qemu_emulator
-
 def guess_spec(workdir, default_spec):
     if default_spec:
         if not os.path.exists(default_spec):