Add a new scripts to generate the buildlogs dir for snapshots
authorHasan Wan <hasan.wan@intel.com>
Thu, 17 Oct 2013 07:23:00 +0000 (07:23 +0000)
committerHasan Wan <hasan.wan@intel.com>
Mon, 21 Oct 2013 02:34:52 +0000 (02:34 +0000)
Change-Id: I91934f21fd9c15f7a834312d2ccf4a1dc075bbc6
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
job_buildlogs.py [new file with mode: 0755]
packaging/jenkins-scripts.spec

diff --git a/job_buildlogs.py b/job_buildlogs.py
new file mode 100755 (executable)
index 0000000..caa7372
--- /dev/null
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+
+"""
+This code is called when create snapshot.
+"""
+
+import os
+import sys
+import shutil
+import hashlib
+
+from xml.dom import minidom
+
+from common.buildtrigger import trigger_info
+from common.buildservice import BuildService
+from common.utils import sync
+
+def restructure_output_dir(package_build_dict, target_path):
+    """ Put the logs in request structure
+    """
+
+    for (pkg_name, arch), (status, dummy, path_to_log) in \
+            package_build_dict.items():
+        try:
+            shutil.copy(path_to_log, os.path.join(target_path,
+                                                  arch,
+                                                  status,
+                                                  "%s.buildlog.txt" %pkg_name))
+        except IOError, err:
+            if err.errno == 2:
+                # Directory not exists, just create it and copy again
+                os.makedirs(os.path.join(target_path, arch, status))
+                shutil.copy(path_to_log,
+                            os.path.join(target_path,
+                                         arch,
+                                         status,
+                                         "%s.buildlog.txt" %pkg_name))
+            else:
+                raise IOError(err)
+
+def collect_build_data(base_path):
+    """ Collect package build information
+    """
+    package_build_dict = {}
+
+    for root, dummy, files in os.walk(base_path):
+        if 'status' in files and 'logfile' in files:
+            with open(os.path.join(root, 'status')) as xml_fh:
+                xml_root = minidom.parse(xml_fh)
+                status_element = xml_root.getElementsByTagName('status')[0]
+                status = status_element.getAttribute('status')
+
+                readytime_element = \
+                    status_element.getElementsByTagName('readytime')[0]
+
+                readytime = readytime_element.childNodes[0].data
+                arch = os.path.basename(os.path.dirname(root))
+
+                package_build_dict[(os.path.basename(root), arch)] = \
+                    (status, readytime, os.path.join(root, 'logfile'))
+
+    return package_build_dict
+
+def get_buildconf(build, project, target_path_base):
+    """ Get build conf from through OSC api
+    """
+
+    conf_str = build.get_project_config(project)
+
+    with open(os.path.join(target_path_base,
+                           "%s-build.conf" %hashlib.md5(conf_str).hexdigest()),
+              'w') as conf_fh:
+        conf_fh.write(conf_str)
+
+def main():
+    """Script entry point.
+    """
+
+    print '---[JOB STARTED: buildlog ]-------------------------'
+
+    obs_api = os.getenv("OBS_API_URL")
+    obs_user = os.getenv("OBS_API_USERNAME")
+    obs_passwd = os.getenv("OBS_API_PASSWD")
+
+    build_base_path = os.getenv("PATH_OBS_BUILD_BASE", "/srv/obs/build")
+
+    sync_out_dir = os.path.join(os.getenv('WORKSPACE'),
+                                    'outdir')
+
+    content = trigger_info(os.getenv("TRIGGER_INFO"))
+
+    sync_dest = os.path.join(os.getenv('IMG_SYNC_DEST_BASE'),
+                             content['repo_path'])
+
+    project = content.get("project")
+    repository = content.get("repo")
+
+    project_logs_base = os.path.join(build_base_path,
+                                     project,
+                                     repository)
+    # Get the build data dict
+    packages_data = collect_build_data(project_logs_base)
+
+    # Restructure the directory for publishing
+    restructure_output_dir(packages_data,
+                           os.path.join(sync_out_dir,
+                                        'builddata',
+                                        'buildlogs'))
+
+    build = BuildService(obs_api, obs_user, obs_passwd)
+
+    # Get OBS project build conf
+    get_buildconf(build, project, os.path.join(sync_out_dir, 'builddata'))
+
+    # sync to donwload server
+    sync(sync_out_dir, sync_dest)
+
+if __name__ == '__main__':
+    sys.exit(main())
index 69835ff..f2c9d2c 100644 (file)
@@ -118,6 +118,7 @@ fi
 %{destdir}/common/snapshot.py
 %{destdir}/common/utils.py
 %{destdir}/job_create_snapshot.py
+%{destdir}/job_buildlogs.py
 %{destdir}/job_jobs_dispatcher.py
 %{destdir}/job_imager.py
 %{destdir}/job_mail_sender.py