[yocto] [layerindex-web][PATCH 06/10] layerindex/tools/import_project: Add import_project

Liam R. Howlett Liam.Howlett at windriver.com
Mon Sep 26 11:25:34 PDT 2016


import_project will scan through a project and find any layer and add it
to the database by calling import_layer on each layer.  This differs
from import_layer as it tires to figure out the remote url and uses the
subdirectory (if one exists) as the name.

Signed-off-by: Liam R. Howlett <Liam.Howlett at WindRiver.com>
---
 layerindex/tools/import_layer.py   |   7 ++
 layerindex/tools/import_project.py | 176 +++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)
 create mode 100755 layerindex/tools/import_project.py

diff --git a/layerindex/tools/import_layer.py b/layerindex/tools/import_layer.py
index 21b31f5..f84fd85 100755
--- a/layerindex/tools/import_layer.py
+++ b/layerindex/tools/import_layer.py
@@ -360,6 +360,13 @@ def main():
                 layerbranch = LayerBranch()
                 layerbranch.layer = layer
                 layerbranch.branch = master_branch
+                layerbranch.save()
+
+                if layer.name != settings.CORE_LAYER_NAME:
+                    layerconfparser = LayerConfParse(logger=logger)
+                    config_data = layerconfparser.parse_layer(layerdir)
+                    layerconfparser.shutdown()
+                    utils.set_layerbranch_collection_version(layerbranch, config_data, logger=logger)
                 if layerdir != repodir:
                     layerbranch.vcs_subdir = subdir
                 if actual_branch:
diff --git a/layerindex/tools/import_project.py b/layerindex/tools/import_project.py
new file mode 100755
index 0000000..0b91c08
--- /dev/null
+++ b/layerindex/tools/import_project.py
@@ -0,0 +1,176 @@
+#!/usr/bin/python3
+
+# Import a project into the database.
+#  This will scan through the directories in a project and find any layer and
+#  call import_layer.
+#
+#
+# Copyright (C) 2016 Wind River Systems
+# Author: Liam R. Howlett <liam.howlett at windriver.com>
+#
+# Licensed under the MIT license, see COPYING.MIT for details
+
+from git import Repo
+from urllib.parse import urlparse
+import logging
+import optparse
+import os, fnmatch
+import sys
+
+sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
+
+import import_layer
+import update
+
+import utils
+
+
+
+
+class ImportProject:
+    logger = utils.logger_create('ProjectIndexImport')
+
+    def find_layers(self, path):
+        self.logger.debug("finding layer..");
+        result = []
+        for root, dirs, files in os.walk(path, followlinks=True):
+            for name in fnmatch.filter(files, 'layer.conf'):
+                if not root.endswith('conf'):
+                    continue
+
+                self.logger.debug("Found %s" % root)
+                result.append(root)
+        return result
+
+
+    def main(self):
+        parser = optparse.OptionParser(
+                usage = """
+                %prog [options] [directory]""")
+
+        parser.add_option("-d", "--debug",
+                help = "Enable debug output",
+                action="store_const", const=logging.DEBUG, dest="loglevel", default=logging.INFO)
+        parser.add_option("-n", "--dry-run",
+                help = "Don't write any data back to the database",
+                action="store_true", dest="dryrun")
+
+        self.options, args = parser.parse_args(sys.argv)
+
+        self.logger.setLevel(self.options.loglevel)
+
+        if len(args) == 1:
+            print("Please provide a directory.");
+            sys.exit(1)
+
+        install_dir = args[1]
+        lc_list = self.find_layers(install_dir)
+        self.add_core(lc_list)
+
+        for layer in lc_list:
+            self.add_layer(layer)
+
+    def add_layer(self, layer):
+        self.logger.debug("Processing layer %s" % layer);
+        try:
+            git_dir = utils.runcmd("git rev-parse --show-toplevel", destdir=layer, logger=self.logger)
+        except Exception as e:
+            self.logger.error("Cannot get root dir for layer %s: %s - Skipping." % (layer, str(e)))
+            return 1
+
+        self.logger.error("git_dir = %s" % git_dir);
+        repo = Repo(git_dir)
+        actual_branch = repo.active_branch.name
+
+
+        layer_name = layer.split('/')[-2]
+
+        layer_subdir = None
+        if os.path.basename(git_dir) != layer_name:
+            layer_subdir = layer_name
+
+        layer_name = self.get_layer_name(layer)
+
+        for i in [1, 2, 3]:
+            try:
+                git_url = utils.runcmd("git config --get remote.origin.url", destdir=git_dir, logger=self.logger)
+            except Exception as e:
+                self.logger.info("Cannot get remote.origin.url for git dir %s: %s" % (git_dir, str(e)))
+
+            if not os.path.exists(git_url):
+                # Assume this is remote.
+                self.logger.debug("Found git url = %s" % git_url)
+                break;
+            self.logger.debug("Iterating to find git url into %s" % git_dir)
+            git_dir = git_url
+
+        cmd = ['import_layer.py']
+        if self.options.loglevel == logging.DEBUG:
+                cmd.append("-d")
+        if layer_subdir:
+                cmd.append("-s")
+                cmd.append(layer_subdir)
+
+        if actual_branch:
+            cmd.append("-a")
+            cmd.append(actual_branch)
+        cmd.append(git_url)
+        cmd.append(layer_name)
+        prefix = "Calling"
+
+        if self.options.dryrun:
+            prefix = "Would Call"
+
+
+        self.logger.info("%s import_layer.main with %s for dir %s" % (prefix, str(cmd), layer))
+        sys.argv = cmd
+        if not self.options.dryrun:
+                try:
+                    import_layer.main();
+                except SystemExit:
+                    return 1
+        return 0
+
+    def get_layer_name(self, layerconfdir):
+        from layerconfparse import LayerConfParse
+
+        layer_name = layerconfdir.split('/')[-2]
+        self.logger.debug('getting layer %s' % layerconfdir)
+        layer_conf = os.path.join(layerconfdir, 'layer.conf')
+        if os.path.isfile(layer_conf):
+            with open(layer_conf) as conf:
+                for line in conf:
+                    if 'BBLAYERS_LAYERINDEX_NAME' in line:
+                        layer_name = line.split('=')[1].strip(' "\n')
+        return layer_name
+
+    def add_core(self, layers):
+        utils.setup_django()
+        import settings
+        for layer in layers:
+            layer_name = self.get_layer_name(layer)
+            if layer_name == settings.CORE_LAYER_NAME:
+                if self.add_layer(layer):
+                    self.logger.info('Failed to add core layer\n')
+                self.update()
+                layers.remove(layer)
+
+    def update(self):
+        update_py = os.path.realpath(os.path.join(os.path.dirname(__file__), '../update.py'))
+        cmd = [update_py]
+        if self.options.loglevel == logging.DEBUG:
+                cmd.append("-d")
+        sys.argv = cmd
+        self.logger.info("update")
+        if not self.options.dryrun:
+                try:
+                    update.main();
+                except SystemExit:
+                    return 1
+
+        return 0
+
+
+if __name__ == "__main__":
+    x = ImportProject()
+    x.main()
-- 
1.9.1




More information about the yocto mailing list