~james-w/udd/management-commands

« back to all changes in this revision

Viewing changes to udd/scripts/categorise_failures.py

  • Committer: James Westby
  • Date: 2011-12-13 21:09:23 UTC
  • mfrom: (557.1.1 drop_email_failures)
  • Revision ID: james.westby@canonical.com-20111213210923-tfrirlx3xbwmi70u
Merged drop_email_failures into management-commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import sys
8
8
import time
9
9
 
10
 
from udd import icommon
11
 
from udd.paths import paths
 
10
from udd import (
 
11
    icommon,
 
12
    iconfig,
 
13
    )
 
14
 
 
15
# Not ideal to declare this here but this is used by several local functions
 
16
# below. Refactoring the whole script may help -- vila 2011-12-07
 
17
conf = None # Will be set by main()
12
18
 
13
19
html_head = '''<html>
14
20
<head>
57
63
 
58
64
 
59
65
def process_failures():
60
 
    db = icommon.StatusDatabase(paths.sqlite_file)
 
66
    db = icommon.StatusDatabase(conf.get('pi.sqlite_file'))
61
67
    return db.summarise_failures()
62
68
 
63
69
 
78
84
 
79
85
def list_old_pages():
80
86
    old_pages = set()
81
 
    for page in os.listdir(paths.web_status_dir):
82
 
        path = os.path.join(paths.web_status_dir, page)
 
87
    web_status_dir = conf.get('pi.web_status_dir')
 
88
    for page in os.listdir(web_status_dir):
 
89
        path = os.path.join(web_status_dir, page)
83
90
        if not os.path.isdir(path):
84
91
            old_pages.add(path)
85
92
    return old_pages
99
106
 
100
107
 
101
108
def write_individual_pages(reasons, explanations):
 
109
    web_status_dir = conf.get('pi.web_status_dir')
102
110
    written_paths = set()
103
111
    for reason in reasons:
104
112
        for info in reasons[reason][:]:
105
113
            assert not "/" in info.name
106
 
            path = os.path.join(paths.web_status_dir, cgi.escape(info.name,
 
114
            path = os.path.join(web_status_dir, cgi.escape(info.name,
107
115
                        True) + ".html")
108
116
            f = open(path, "wb")
109
117
            try:
120
128
            finally:
121
129
                f.close()
122
130
            written_paths.add(path)
123
 
        path = os.path.join(paths.web_status_dir, error_page(reason))
 
131
        path = os.path.join(web_status_dir, error_page(reason))
124
132
        f = open(path, "wb")
125
133
        try:
126
134
            f.write(html_head % {"title": "Failures of type"})
243
251
        f.write("</ul>\n</li>\n")
244
252
 
245
253
def write_overview_page(reasons, package_info, explanations):
 
254
    web_status_dir = conf.get('pi.web_status_dir')
246
255
    written_paths = set()
247
 
    path = os.path.join(paths.web_status_dir, "index.html")
 
256
    path = os.path.join(web_status_dir, "index.html")
248
257
    f = open(path, "wb")
249
258
    try:
250
259
        f.write(html_head % {"title": "bzr import failures"})
257
266
        f.write('</div>\n<h3>All Failures by Category</h3>\n<div id="analysis">\n<ul>\n')
258
267
        write_analysis(f, reasons, explanations)
259
268
        f.write('</ul>\n</div>\n')
260
 
        db = icommon.HistoryDatabase(paths.sqlite_history_file)
 
269
        db = icommon.HistoryDatabase(conf.get('pi.sqlite_history_file'))
261
270
        write_graphs(f, db.get_counts())
262
271
        f.write(html_tail)
263
272
    finally:
267
276
 
268
277
 
269
278
def get_main_packages():
270
 
    db = icommon.PackageDatabase(paths.sqlite_package_file)
 
279
    db = icommon.PackageDatabase(conf.get('pi.sqlite_package_file'))
271
280
    return db.list_packages_in_main()
272
281
 
273
282
 
274
283
def filter_packages(filter_set, reasons, package_info):
275
 
    filtered_package_info = find_info_where(package_info, lambda x: x.name in filter_set)
 
284
    filtered_package_info = find_info_where(package_info,
 
285
                                            lambda x: x.name in filter_set)
276
286
    filtered_reasons = {}
277
287
    for sig in reasons:
278
288
        for info in reasons[sig]:
308
318
 
309
319
 
310
320
def write_main_page(reasons, package_info, explanations):
 
321
    web_status_dir = conf.get('pi.web_status_dir')
311
322
    main_packages = get_main_packages()
312
323
    reasons, package_info = filter_packages(main_packages, reasons, package_info)
313
324
    written_paths = set()
314
 
    path = os.path.join(paths.web_status_dir, "main.html")
 
325
    path = os.path.join(web_status_dir, "main.html")
315
326
    f = open(path, "wb")
316
327
    try:
317
328
        f.write(html_head % {"title": "bzr import failures for main"})
324
335
        f.write('</div>\n<h3>All Failures by Category</h3>\n<div id="analysis">\n<ul>\n')
325
336
        write_analysis(f, reasons, explanations)
326
337
        f.write('</ul>\n</div>\n')
327
 
        db = icommon.HistoryDatabase(paths.sqlite_history_file)
 
338
        db = icommon.HistoryDatabase(conf.get('pi.sqlite_history_file'))
328
339
        write_graphs(f, db.get_main_counts())
329
340
        f.write(html_tail)
330
341
    finally:
334
345
 
335
346
 
336
347
def copy_js_files():
 
348
    web_status_dir = conf.get('pi.web_status_dir')
337
349
    source_paths = set(["jquery.min.js", "jquery.flot.js"])
338
350
    copied_paths = set()
339
351
    for fn in source_paths:
340
 
        shutil.copy(paths.static_file_path(fn), paths.web_status_dir)
341
 
        copied_paths.add(os.path.join(paths.web_status_dir, fn))
 
352
        source = os.path.join(conf.get('pi.install_dir'), fn)
 
353
        shutil.copy(source, web_status_dir)
 
354
        copied_paths.add(os.path.join(web_status_dir, fn))
342
355
    return copied_paths
343
356
 
344
357
 
354
367
 
355
368
 
356
369
def install_index():
 
370
    web_base_dir = conf.get('pi.www_dir')
 
371
    web_status_dir = conf.get('pi.web_status_dir')
 
372
    index_path = os.path.join(web_base_dir, 'index.html')
 
373
    relpath = os.path.relpath(web_status_dir, web_base_dir)
357
374
    content = '''<html>
358
375
<head>
359
376
<title>Ubuntu Bazaar package importer</title>
362
379
<body>
363
380
Hi!
364
381
 
365
 
See <a href="status">the status page.</a>
 
382
See <a href="%s">the status page.</a>
366
383
</body>
367
384
</html>
368
 
'''
369
 
    # FIXME: 'status' in content should stay in sync with 'status' in
370
 
    # web_status_dir, we could use os.path.basename(paths.web_status_dir)
371
 
    # -- vila 2011-03-23
372
 
    index_path = os.path.join(paths.web_base_dir, 'index.html')
373
 
    f = open(index_path, 'w')
374
 
    try:
 
385
''' % (relpath,)
 
386
    with open(index_path, 'w') as f:
375
387
        f.write(content)
376
 
    finally:
377
 
        f.close()
 
388
 
378
389
 
379
390
def main():
380
 
    lock = icommon.lock_categorise_failures()
 
391
    global conf
 
392
    conf = iconfig.ImporterStack()
 
393
    lock = icommon.lock_path(conf.get('pi.script_locks_dir'),
 
394
                             'categorise_failures')
381
395
    if lock is None:
382
396
        print "Another instance of categorise_failures is already running."
383
397
        sys.exit(0)
384
398
    try:
385
 
        icommon.ensure_directory(paths.web_status_dir)
 
399
        icommon.ensure_directory(conf.get('pi.web_status_dir'))
386
400
        install_index()
387
401
        get_info()
388
402
    finally: