1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#!/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 <http://www.gnu.org/licenses/>.
#
# =*= 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()
|