#!/usr/bin/env python2 # Copyright 2015 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # =*= License: GPL-3+ =*= import glob import os import shutil import cliapp import jinja2 import yaml import icklib class IckHTML(cliapp.Application): def process_args(self, args): ick_filename, html_dir = self.parse_command_line_args(args) ick = self.read_ick_file(ick_filename) self.create_html(ick_filename, ick, html_dir) def parse_command_line_args(self, args): if len(args) != 2: raise cliapp.AppException('Must have exactly two arguments') return args[0], args[1] def read_ick_file(self, filename): with open(filename) as f: return yaml.safe_load(f) def create_html(self, ick_filename, ick, html_dir): loader = jinja2.PackageLoader('icklib', 'templates') env = jinja2.Environment( loader=loader, autoescape=lambda foo: True, extensions=['jinja2.ext.autoescape']) statedir = icklib.create_statedir_from_ick(ick) self.create_index_html(ick_filename, ick, env, html_dir) for project in self.get_projects(ick): project.builds = statedir.get_build_infos(project.name) self.copy_build_files_to_html_dir(statedir, project, html_dir) self.create_project_html(statedir, project, env, html_dir) def create_index_html(self, ick_filename, ick, env, html_dir): vars = { 'ick_filename': ick_filename, 'projects': self.get_projects(ick), 'statedir': icklib.create_statedir_from_ick(ick), } template = env.get_template('index.j2') with open(os.path.join(html_dir, 'index.html'), 'w') as f: f.write(template.render(**vars)) def get_projects(self, ick): return sorted( icklib.create_projects_from_ick(ick, None), key=lambda p: p.name) def copy_build_files_to_html_dir(self, statedir, project, html_dir): for build in project.builds: build_dir = os.path.join( html_dir, project.name, str(build.build_number)) if not os.path.exists(build_dir): os.makedirs(build_dir) self.copy_build_log(build, build_dir) self.copy_build_artifacts(statedir, project, build, build_dir) def copy_build_log(self, build, build_dir): orig = build.get_build_log_filename() target = os.path.join(build_dir, 'build_log.txt') shutil.copy2(orig, target) def copy_build_artifacts(self, statedir, project, build, build_dir): artifacts = build.get_artifacts_directory() target = os.path.join(build_dir, 'build_log.txt') for artifact in glob.glob(os.path.join(artifacts, '*')): shutil.copy2(artifact, build_dir) def create_project_html(self, statedir, project, env, html_dir): vars = { 'project': project, } template = env.get_template('project.j2') html_file = os.path.join(html_dir, '%s.html' % project.name) with open(html_file, 'w') as f: f.write(template.render(**vars)) self.copy_mtime( statedir.get_project_state_path(project.name), html_file) def copy_mtime(self, orig_filename, dest_filename): st = os.lstat(orig_filename) os.utime(dest_filename, (long(st.st_atime), long(st.st_mtime))) if __name__ == '__main__': IckHTML().run()