diff mbox

[core] add hostprep command

Message ID 1378891370-25037-1-git-send-email-kim.hansen@prevas.dk
State Superseded
Delegated to: Esben Haabendal
Headers show

Commit Message

kim.hansen@prevas.dk Sept. 11, 2013, 9:22 a.m. UTC
From: Kim Højgaard-Hansen <kiho@prevas.dk>

The hostprep command can show what command is needed to prepare the
host for OE-lite. A file for each supported host configuration must
be added to th supported-host-configs directory. The file should
contain the command needed to install the dependencies needed for
using oe-lite. The file name should be: distro_release_codename
as defined by lsb-release.
The lsb-release file/tool is used for automatic host detection. A
list of the supported host configurations can be shown by passing
-l to the hostprep command. If only the command for the host should
be shown, run 'hostprep -c'
---
 lib/oelite/cmd/__init__.py                         |   2 +-
 lib/oelite/cmd/hostprep.py                         | 130 +++++++++++++++++++++
 .../cmd/supported-host-configs/exherbo_none_none   |   1 +
 .../supported-host-configs/ubuntu_12.04_precise    |   7 ++
 4 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 lib/oelite/cmd/hostprep.py
 create mode 100644 lib/oelite/cmd/supported-host-configs/exherbo_none_none
 create mode 100644 lib/oelite/cmd/supported-host-configs/ubuntu_12.04_precise
diff mbox

Patch

diff --git a/lib/oelite/cmd/__init__.py b/lib/oelite/cmd/__init__.py
index d2feabe..6919262 100644
--- a/lib/oelite/cmd/__init__.py
+++ b/lib/oelite/cmd/__init__.py
@@ -1 +1 @@ 
-manifest_cmds = [ "bake", "show", "cherry" ]
+manifest_cmds = [ "bake", "hostprep","show", "cherry" ]
diff --git a/lib/oelite/cmd/hostprep.py b/lib/oelite/cmd/hostprep.py
new file mode 100644
index 0000000..1ed08be
--- /dev/null
+++ b/lib/oelite/cmd/hostprep.py
@@ -0,0 +1,130 @@ 
+import oebakery
+import logging
+import os
+import subprocess
+
+description = "Host preparation tool"
+arguments = ()
+
+def add_parser_options(parser):
+    parser.add_option("-c", "--command",
+                      action="store_true", default=False,
+                      help="Show the command needed to prepare the host for OE-lite")
+    parser.add_option("-l", "--list-supported-host-configs",
+                      action="store_true", default=False,
+                      help="Show a list of known host configurations")
+    parser.add_option("-s", "--select-host-config",
+                      action="store_true", default=False,
+                      help="Manually select a host configuration")
+    parser.add_option("-d", "--debug",
+                      action="store_true", default=False,
+                      help="Debug the OE-lite metadata")
+    return
+
+
+def parse_args(options, args):
+    if options.debug:
+        logging.getLogger().setLevel(logging.DEBUG)
+    else:
+        logging.getLogger().setLevel(logging.INFO)
+    if args:
+        options.head = args.pop(0)
+    else:
+        options.head = None
+    return
+
+def get_supported_host_configs():
+    logging.debug("Reading supported host configurations")
+    #TODO: fix path handling, find place to put host configs
+    hid = 0
+    hc=[]
+    config_dir = "./meta/core/lib/oelite/cmd/supported-host-configs"
+    logging.debug("Using config directory: %s", config_dir)
+    for config in os.listdir(config_dir):
+        logging.debug("Reading config: %s", config)
+        keys = config.split('_')
+        if(len(keys) != 3):
+            logging.debug("Ignoring host-config with wrong name: %s", config)
+            continue
+        #read out the cmd to run for this host configuration
+        try:
+            logging.debug("Read the host preparation command")
+            #strip off the last newline
+            cmd = open(config_dir+"/"+config, 'r').read()[:-1]
+        except:
+            logging.debug("Reading the host preparation command from the host configuration file failed")
+            raise
+        hc.append(dict(distro=keys[0],  release=keys[1],  codename=keys[2],  hostID=hid,  command=cmd))
+        hid+=1
+    logging.debug("Adding host config for unknown host")
+    hc.append(dict(distro="unknown", release="none",  codename="none", hostID=hid,  command="Unknown host - Please refer to the installation documentation"))
+    return hc
+
+def determine_host(host_configs):
+    logging.debug("Running host detection")
+    logging.debug("Searching for lsb_release information")
+    #try lsb-release which is the most widely available method
+    if(os.path.exists("/etc/lsb-release") and os.path.isfile("/etc/lsb-release")):
+        try:
+            logging.debug("checking for the lsb_release binary")
+            subprocess.call(["lsb_release"])
+        except OSError as e:
+            if e.errno == os.errno.ENOENT:
+                # handle file not found error.
+                logging.debug("/etc/lsb-release found, but lsb_release binary not available")
+                return (host for host in host_configs if host["distro"] == "unknown").next()
+            else:
+                logging.debug("unhandled exception when calling lsb_release")
+                raise
+        else:
+            try:
+                logging.debug("using lsb_release to get host information")
+                #strip newlines and make lowercase for later matching
+                distro = subprocess.check_output(["lsb_release", "-si"])[:-1].lower()
+                release = subprocess.check_output(["lsb_release", "-sr"])[:-1].lower()
+                codename = subprocess.check_output(["lsb_release", "-sc"])[:-1].lower()
+            except:
+                logging.debug("unhandled exception when calling lsb_release")
+                raise
+            else:
+                logging.debug("matching lsb_release information to supported configs")
+                logging.debug("lsb_release: distro: %s | release: %s | codename: %s", distro, release, codename)
+                for host in host_configs:
+                    if((distro == host["distro"]) and (release == host["release"]) and (codename == host["codename"])):
+                        logging.debug("found matching host config with ID: %d", host["hostID"])
+                        return host
+    logging.debug("No lsb_release information found")
+    logging.debug("Checking for Exherbo")
+    if(os.path.exists("/etc/exherbo-release") and os.path.isfile("/etc/exherbo-release")):
+        return (host for host in host_configs if host["distro"] == "exherbo").next()
+    logging.debug("No host config found for this host")
+    return (host for host in host_configs if host["distro"] == "unknown").next()
+
+def print_host_config(host):
+     print '{0:8}{1:15}{2:9}{3:15}{4:9}{5:15}{6:22}{7:2}'.format("Distro:", host["distro"], "Release:", host["release"], "Codename", host["codename"], "Host Configuration ID:", host["hostID"])
+
+def run(options, args, config):
+    logging.debug("hostprep.run %s", options)
+
+    hc = get_supported_host_configs()
+
+    if(options.list_supported_host_configs):
+        print "List of supported host configurations:\n"
+        for host in hc:
+           print_host_config(host)
+        return 0
+
+    #try automatic host detection
+    host = determine_host(hc)
+    if(options.command):
+        logging.debug("printing command for hostID: %d", host["hostID"])
+        logging.debug("Command: '%s'", host["command"])
+        print host["command"]
+        return 0
+    print "Your host configuration detected to be:\n"
+    print_host_config(host)
+    print
+    print "Run this command on your host to prepare it for OE-lite:\n"
+    print host["command"]
+
+    return 0
diff --git a/lib/oelite/cmd/supported-host-configs/exherbo_none_none b/lib/oelite/cmd/supported-host-configs/exherbo_none_none
new file mode 100644
index 0000000..2161776
--- /dev/null
+++ b/lib/oelite/cmd/supported-host-configs/exherbo_none_none
@@ -0,0 +1 @@ 
+sudo cave resolve oe-bakery
diff --git a/lib/oelite/cmd/supported-host-configs/ubuntu_12.04_precise b/lib/oelite/cmd/supported-host-configs/ubuntu_12.04_precise
new file mode 100644
index 0000000..98c533c
--- /dev/null
+++ b/lib/oelite/cmd/supported-host-configs/ubuntu_12.04_precise
@@ -0,0 +1,7 @@ 
+sudo apt-get install python python-support python-magic python-ply \
+python-pycurl python-pysqlite2 python-pkg-resources python-dev \
+coreutils sed git-core cvs subversion mercurial quilt gawk texinfo \
+automake autoconf autopoint libtool curl texi2html diffstat \
+openjade groff mtd-utils build-essential make gcc g++ binutils \
+bison flex bc ncurses-dev unzip lzma gtk-doc-tools docbook-utils \
+libxml2-utils xmlto help2man libglib2.0-dev lzop gperf python-svn