summaryrefslogtreecommitdiffstats
path: root/website/convert-to-static.py
blob: 8a5a1c8c1aee16cb61e495bbdacc169b57a5a6af (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#! /usr/bin/env python

import xml.dom.minidom
import os
import stat
import re
from cStringIO import StringIO
import sys

import ezt

base_template = ezt.Template()
base_template.parse(file('templates/base.ezt').read())
buildlog_template = ezt.Template()
buildlog_template.parse(file('templates/buildlog.ezt').read())
changelog_template = ezt.Template()
changelog_template.parse(file('templates/changelog.ezt').read())
tests_template = ezt.Template()
tests_template.parse(file('templates/tests.ezt').read())

def getText(nodelist):
    if not nodelist:
        return None
    rc = ''
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc.encode('utf-8')


class ChangelogFile:
    def __init__(self, node):
        for attr in ('name', 'revision'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)


class ChangelogEntry:
    def __init__(self, node):
        for attr in ('date', 'weekday', 'time', 'isoDate', 'msg', 'author', 'revision'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)
        self.file = [ChangelogFile(x) for x in node.getElementsByTagName('file')]

class ChangelogSvnEntry:
    def __init__(self, node):
        for attr in ('date', 'msg', 'author', 'file'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)
        self.revision = node.attributes['revision'].value
        if self.date:
            self.time = self.date[11:16]


class TestTest:
    def __init__(self, node):
        for attr in ('id', 'description'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)
        self.result = node.attributes['result'].value

class TestSuite:
    def __init__(self, node):
        for attr in ('title', 'duration'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)
        if self.duration:
            self.duration = '%.4f' % float(self.duration)
        self.test = [TestTest(x) for x in node.getElementsByTagName('test')]
        self.len_tests = len(self.test)


class Build:
    def __init__(self, node):
        for attr in ('date', 'hostname', 'duration', 'buildlog', 'buildlog295', 'changelog'):
            try:
                setattr(self, attr, getText(node.getElementsByTagName(attr)[0].childNodes))
            except IndexError:
                setattr(self, attr, None)

        self.display_date = '%s-%s-%s' % (self.date[:4], self.date[4:6], self.date[6:8])
        self.display_hour = '%s:%s' % (self.date[9:11], self.date[11:13])

        for component in ('liblasso', 'java', 'python', 'php', 'perl', 'csharp', 'liblasso295'):
            try:
                cnode = [x for x in node.getElementsByTagName(component) if \
                        x.attributes.has_key('buildlog')][0]
            except IndexError:
                setattr(self, component + '_status', None)
                continue
            setattr(self, component + '_status', getText(cnode.childNodes))
            setattr(self, component + '_href', cnode.attributes['buildlog'].value.replace('.xml',''))

        for test in ('c', 'python', 'souk'):
            try:
                cnode = [x for x in node.getElementsByTagName(test) if \
                        x.attributes.has_key('href')][0]
            except IndexError:
                setattr(self, 'tests_' + test + '_status', None)
                continue
            setattr(self, 'tests_' + test + '_status', getText(cnode.childNodes))
            setattr(self, 'tests_' + test + '_href', cnode.attributes['href'].value.replace('.xml', ''))

        if self.changelog:
            self.changelog = self.changelog.replace('.xml', '')
            try:
                dom_cl = xml.dom.minidom.parse(file('web' + self.changelog + '.xml'))
            except:
                self.nb_commits = '?'
                self.last_commit_author = '?'
            else:
                self.last_commit_author = getText(dom_cl.getElementsByTagName('author')[-1].childNodes)
                self.nb_commits = len(dom_cl.getElementsByTagName('entry'))
                if not self.nb_commits:
                    self.nb_commits = len(dom_cl.getElementsByTagName('logentry'))



re_body = re.compile('<body(.*?)>(.*)</body>', re.DOTALL)
re_div = re.compile('<div(.*?)>(.*)</div>', re.DOTALL)
re_title = re.compile('<title>(.*)</title>', re.DOTALL)
re_summary = re.compile('[a-z]+\.[0-9]{4}.xml')

if not os.path.exists('web-static'):
    os.mkdir('web-static')

for BUILDLOGS_DIR in ('build-logs', 'build-logs-wsf'):
    if not os.path.exists('web/%s' % BUILDLOGS_DIR):
        continue
    if not os.path.exists('web-static/%s' % BUILDLOGS_DIR):
        os.mkdir('web-static/%s' % BUILDLOGS_DIR)

    for base, dirs, files in os.walk('web/%s' % BUILDLOGS_DIR):
        if base.endswith('/CVS') or base.endswith('/.svn') or base.endswith('/.git'):
            continue
        for dirname in dirs:
            src_file = os.path.join(base, dirname)
            dst_file = 'web-static/' + src_file[4:]
            if not os.path.exists(dst_file):
                os.mkdir(dst_file)
        for filename in files:
            if filename[0] == '.':
                continue
            src_file = os.path.join(base, filename)
            dst_file = 'web-static/' + src_file[4:].replace('.xml', '.html')
            if os.path.exists(dst_file) and \
                    os.stat(dst_file)[stat.ST_MTIME] >= os.stat(src_file)[stat.ST_MTIME]:
                continue
            if src_file.endswith('.log'):
                os.link(src_file, dst_file)
                continue
            if src_file.endswith('.html'):
                try:
                    body = re_body.findall(file(src_file).read())[0][1].strip()
                except IndexError:
                    raise "no body found"
                fd = StringIO()
                base_template.generate(fd, {'body': body, 'title': 'Build Log', 'section': 'buildbox'})
                open(dst_file, 'w').write(fd.getvalue())
                continue

            try:
                dom = xml.dom.minidom.parse(file(src_file))
            except:
                continue
            type = dom.childNodes[0].nodeName
            if type == 'changelog':
                entries = [ChangelogEntry(x) for x in dom.getElementsByTagName('entry')]
                fd = StringIO()
                changelog_template.generate(fd, {'entry': entries})
                body = fd.getvalue()
                fd = StringIO()
                base_template.generate(fd, {'body': body, 'title': 'ChangeLog', 'section': 'buildbox'})
                open(dst_file, 'w').write(fd.getvalue())

            if type == 'log':
                entries = [ChangelogSvnEntry(x) for x in dom.getElementsByTagName('logentry')]
                fd = StringIO()
                changelog_template.generate(fd, {'entry': entries})
                body = fd.getvalue()
                fd = StringIO()
                base_template.generate(fd, {'body': body, 'title': 'ChangeLog', 'section': 'buildbox'})
                open(dst_file, 'w').write(fd.getvalue())

            if type == 'testsuites':
                datetime = getText(dom.getElementsByTagName('datetime')[0].childNodes)
                title = getText(dom.getElementsByTagName('title')[0].childNodes)
                suites = [TestSuite(x) for x in dom.getElementsByTagName('suite')]
                fd = StringIO()
                tests_template.generate(fd, {'datetime': datetime, 'title': title,
                        'suite': suites})
                body = fd.getvalue()
                fd = StringIO()
                base_template.generate(fd, {'body': body,
                        'title': 'Test Suite - %s' % title, 'section': 'buildbox'})
                open(dst_file, 'w').write(fd.getvalue())


    day_dirs = os.listdir('web/%s/' % BUILDLOGS_DIR)
    day_dirs.sort()
    day_dirs.reverse()
    day_dirs = day_dirs[:60]

    main_page = []

    for base, dirs, files in os.walk('web/%s' % BUILDLOGS_DIR):
        for dirname in dirs:
            if dirname in day_dirs:
                for t in [x for x in os.listdir(os.path.join(base, dirname)) if re_summary.match(x)]:
                    main_page.append(os.path.join(base, dirname, t))

    main_page.sort()
    main_page.reverse()
    main_page = main_page[:50]
    builds = []
    for filename in main_page:
        try:
            builds.append( Build(xml.dom.minidom.parse(filename)) )
            if len(builds) > 1 and builds[-2].date[:8] == builds[-1].date[:8]:
                builds[-1].display_date = ''
        except:
            pass

    fd = StringIO()
    buildlog_template.generate(fd, {'build': builds})
    body = fd.getvalue()
    fd = StringIO()
    base_template.generate(fd, {'body': body, 'title': 'Build Box', 'section': 'buildbox'})
    if BUILDLOGS_DIR == 'build-logs':
        open('web-static/buildbox.html', 'w').write(fd.getvalue())
    elif BUILDLOGS_DIR == 'build-logs-wsf':
        open('web-static/buildbox-wsf.html', 'w').write(fd.getvalue())

for base, dirs, files in os.walk('web'):
    if '/build-logs' in base or '/news/' in base:
        continue
    if base.endswith('CVS') or base.endswith('.svn'):
        continue
    for dirname in dirs:
        if dirname in ('CVS', 'news', '.svn'):
            continue
        src_file = os.path.join(base, dirname)
        dst_file = 'web-static/' + src_file[4:]
        if not os.path.exists(dst_file):
            os.mkdir(dst_file)
    for filename in files:
        if filename in ('.cvsignore', 'buildbox.xml'):
            continue
        if filename[0] == '.':
            continue
        basename, ext = os.path.splitext(filename)
        src_file = os.path.join(base, filename)
        dst_file = 'web-static/' + src_file[4:]

        if os.path.isdir(src_file): continue

        if os.path.exists(dst_file) and \
                os.stat(dst_file)[stat.ST_MTIME] >= os.stat(src_file)[stat.ST_MTIME]:
            continue

        if ext not in ('.html', '.xml') or filename.startswith('doap.') or 'api-reference' in src_file:
            if os.path.exists(dst_file):
                os.unlink(dst_file)
            os.link(src_file, dst_file)
            continue

        type = None
        if ext == '.xml':
            dom = xml.dom.minidom.parse(file(src_file))
            type = dom.childNodes[0].nodeName
            dst_file = dst_file.replace('.xml', '.html')

        news = None
        if dst_file == 'web-static/index.html':
            news_files = [x for x in os.listdir('web/news/') if x.endswith('.xml') and x[2] == '-']
            news_files.sort()
            news_files.reverse()
            news_files = news_files[:2]
            news = []
            for f in news_files:
                news.append('<div>%s</div>' % re_div.findall(file(os.path.join('web/news/', f)).read())[0][1].strip())
            news = '\n'.join(news)

        section = src_file.split('/')[1].replace('.xml', '')
        if ext == '.html' or type == 'html':
            content = file(src_file).read()
            try:
                body = re_body.findall(content)[0][1].strip()
            except IndexError:
                raise "no body found"
            title = re_title.findall(content)[0]
            fd = StringIO()
            base_template.generate(fd, {'body': body, 'title': title, 'section': section,
                    'news': news})
            open(dst_file, 'w').write(fd.getvalue())
            continue