~launchpad-pqm/launchpad/devel

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
'''Check whether page templates are OK with regards to whether they provide a
title or not. Macros and portlets are excused from providing titles.'''

import os
import sys

templates_location = "lib/canonical/launchpad/templates"

def file_is_macro(path):
    '''Check whether a file is a page template macro.'''

    fh = file(path)

    line = fh.readline()

    fh.close()

    return line.startswith('<metal')

def file_is_portlet(path):
    '''Check whether a template file is a portlet.'''

    return os.path.split(path)[-1].startswith('portlet-')

def file_has_title(path):
    '''Check whether a template file provides a title.'''

    fh = file(path)

    for line in fh:
        if 'metal:fill-slot="title"' in line:
            fh.close()
            return 1

    fh.close()
    return 0

def check_file(path):
    '''Check whether a file is OK with regards to providing a title.'''

    return file_is_macro(path) or file_is_portlet(path) or file_has_title(path)

def find_templates(path):
    '''Find all the page templates in a given directory.'''

    stdout = os.popen("find '%s' -name '*.pt'" % path, 'r')

    return [ line[:-1] for line in stdout.readlines() ]

def check_directory(path):
    '''Check whether all the page templates in a directory are OK with regards
    to providing titles.'''

    for file in find_templates(path):
        if not check_file(file):
            return False

def summarise_directory(path):
    '''Print a summary of the bad files in a directory, or nothing if there
    are no bad files.'''

    bad = [ file for file in find_templates(path) if not check_file(file) ]

    if not bad:
        return True

    bad.sort()

    print
    print "The following page templates are not macros and have no title:"
    print

    for file in bad:
        print file

    print
    print "This will in future cause a test failure."
    print

    return False

if __name__ == '__main__':
    if summarise_directory(templates_location):
        sys.exit(0)
    else:
        sys.exit(1)