2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008 Laurentian University
4 # Dan Scott <dscott@laurentian.ca>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
17 # vim:et:sw=4:ts=4: set fileencoding=utf-8 :
20 Parse DTD files and XUL files looking for trouble
30 '../../../Open-ILS/web/opac/locale/en-US/',
34 '../../../Open-ILS/xul/staff_client/server/',
35 '../../../Open-ILS/xul/staff_client/chrome/',
40 Parse entities files in known places
43 basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
55 for p_dir in DTD_DIRS:
56 p_dir = os.path.normpath(os.path.join(basedir, p_dir))
57 file_list = os.listdir(p_dir)
58 for d_file in file_list:
59 if os.path.splitext(d_file)[1] == '.dtd':
60 dtd_files.append(os.path.join(p_dir, d_file))
62 prefix = os.path.commonprefix(dtd_files)
64 for d_file in dtd_files:
66 # Get the shortest unique address for this file
67 short_df = d_file[len(prefix):]
69 dtd_file = open(d_file, 'r')
76 # Get rid of trailing linefeed
80 unpack = re.search(r'<!ENTITY\s+(.+?)\s+([\'"])(.*?)\2\s*>', line)
84 # Skip anything other than entity definitions
85 # Note that this makes some massive assumptions:
86 # 1. that we only have with one entity defined per line
87 # 2. that we only have single-line entities
88 # 3. that the entity begins in position 0 on the line
89 if not unpack or not line or not line.startswith('<!ENTITY'):
92 # If we did not retrieve an entity and definition, that's probably not good
93 if len(unpack.groups()) != 3:
94 print("%s:%d: No entity defined on line [%s]" % (short_df, line_num, line))
97 entity_key, quote, value = unpack.groups()
99 print entity_key, value
101 if not entities.has_key(entity_key):
102 entities[entity_key] = [{'value': value, 'file': short_df}]
105 for entry in entities[entity_key]:
106 if ['file'] == short_df:
107 print("%s:%d: Duplicate key '%s' in line [%s]" % (short_df, line_num, entity_key, line[0:-1]))
110 entities[entity_key].append({'value': value, 'file': short_df})
116 def check_xul_files(entities):
118 Finds all the XUL and JavaScript files
121 basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
125 for x_dir in XUL_DIRS:
126 for root, dirs, files in os.walk(x_dir):
128 if os.path.splitext(x_file)[1] == '.xul' or os.path.splitext(x_file)[1] == '.js':
129 check_xul(root, x_file, entities)
131 def check_xul(root, filename, entities):
133 Checks all XUL files to ensure:
134 * that the requested entity exists
135 * that every entity is actually required
140 # Typical entity usage:
141 # &blah.blah.blah_bity.blah;
142 strings = re.compile(r'''&([a-zA-Z._]+);''')
144 xul = open(os.path.join(root, filename), 'r')
149 print "File: %s" % (os.path.normpath(os.path.join(root, filename)))
151 for s_match in strings.finditer(content):
153 if not entities.has_key(s_match.group(1)):
154 print "File: %s" % (os.path.normpath(os.path.join(root, filename)))
155 print "\tEntity %s not found, expected in %s" % (s_match.group(1), 'lang.dtd')
158 print "\t%d entities found" % (num_strings)
160 if __name__ == '__main__':
161 entities = parse_entities()
162 check_xul_files(entities)