--- /dev/null
+#!/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())