]> git.evergreen-ils.org Git - Evergreen.git/blob - build/i18n/tests/check_entities.py
784effc94293e4a57ee4673666138c9e6cb9c566
[Evergreen.git] / build / i18n / tests / check_entities.py
1 #!/usr/bin/env python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008  Laurentian University
4 # Dan Scott <dscott@laurentian.ca>
5
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.
10
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 # -----------------------------------------------------------------------
16
17 # vim:et:sw=4:ts=4: set fileencoding=utf-8 :
18
19 """
20 Parse DTD files and XUL files looking for trouble
21     * Missing entities
22 """
23
24 import os
25 import re
26
27 DEBUG = False
28
29 DTD_DIRS = (
30         '../../../Open-ILS/web/opac/locale/en-US/',
31         )
32
33 XUL_DIRS = (
34         '../../../Open-ILS/xul/staff_client/server/',
35         '../../../Open-ILS/xul/staff_client/chrome/',
36         )
37
38 def parse_entities():
39     """
40     Parse entities files in known places
41     """
42
43     basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
44
45     entities = {
46         "amp" : "&",
47         "lt" : "<",
48         "gt" : ">",
49         "nbsp" : ">",
50         "quot" : ">",
51     }
52
53     dtd_files = []
54
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))
61
62     prefix = os.path.commonprefix(dtd_files)
63
64     for d_file in dtd_files:
65                 if DEBUG:
66                         print "Checking %s\n" % (d_file)
67
68         # Get the shortest unique address for this file
69         short_df = d_file[len(prefix):]
70
71         dtd_file = open(d_file, 'r')
72
73         line_num = 1
74
75         for line in dtd_file:
76             line_num += 1
77
78             # Get rid of trailing linefeed
79             line = line[0:-1]
80
81             # Parse entity/value 
82             unpack = re.search(r'<!ENTITY\s+(.+?)\s+([\'"])(.*?)\2\s*>', line)
83             if DEBUG and unpack:
84                 print(unpack.groups())
85
86             # Skip anything other than entity definitions
87             # Note that this makes some massive assumptions:
88             #   1. that we only have with one entity defined per line
89             #   2. that we only have single-line entities
90             #   3. that the entity begins in position 0 on the line
91             if not unpack or not line or not line.startswith('<!ENTITY'):
92                 continue
93
94             # If we did not retrieve an entity and definition, that's probably not good
95             if len(unpack.groups()) != 3:
96                 print("%s:%d: No entity defined on line [%s]" % (short_df, line_num, line))
97                 continue
98
99             entity_key, quote, value = unpack.groups()
100             if DEBUG:
101                 print(entity_key, value)
102
103             if not entities.has_key(entity_key):
104                 entities[entity_key] = [{'value': value, 'file': short_df}]
105                 continue
106
107             for entry in entities[entity_key]:
108                 if ['file'] == short_df:
109                     print("%s:%d: Duplicate key '%s' in line [%s]" % (short_df, line_num, entity_key, line[0:-1]))
110                     continue
111
112             entities[entity_key].append({'value': value, 'file': short_df})
113
114         dtd_file.close()
115
116     return entities
117
118 def check_xul_files(entities):
119     """
120     Finds all the XUL and JavaScript files
121     """
122
123     basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
124
125     xul_files = []
126
127     for x_dir in XUL_DIRS:
128         for root, dirs, files in os.walk(x_dir):
129             for x_file in files:
130                 if os.path.splitext(x_file)[1] == '.xul' or os.path.splitext(x_file)[1] == '.js':
131                     check_xul(root, x_file, entities)
132
133 def check_xul(root, filename, entities):
134     """
135     Checks all XUL files to ensure:
136       * that the requested entity exists
137       * that every entity is actually required
138     """
139
140     num_strings = 0
141
142     # Typical entity usage:
143     # &blah.blah.blah_bity.blah;
144     strings = re.compile(r'''&([a-zA-Z:_][a-zA-Z0-9:_\-.]+);''')
145
146     xul = open(os.path.join(root, filename), 'r')
147     content = xul.read()
148     xul.close()
149
150     if DEBUG:
151         print("File: %s" % (os.path.normpath(os.path.join(root, filename))))
152
153     for s_match in strings.finditer(content):
154         num_strings += 1
155         if not entities.has_key(s_match.group(1)):
156             print("File: %s" % (os.path.normpath(os.path.join(root, filename))))
157             print("\tEntity %s not found, expected in %s" % (s_match.group(1), 'lang.dtd'))
158
159         # Find bad entities
160         bad_strings = re.compile(r'''&([^a-zA-Z:_]?[a-zA-Z0-9:_]*[^a-zA-Z0-9:_\-.;][a-zA-Z0-9:_\-.]*);''')
161
162         # Match character entities (&#0129; etc), which are okay
163         char_entity = re.compile(r'''^((#([0-9])+)|(#x([0-9a-fA-F])+))$''')
164
165         for s_match in bad_strings.finditer(content):
166                 # Rule out character entities and URL concatenation
167                 if (not char_entity.search(s_match.group(1))) and s_match.group(1) != "'":
168                         print("File: %s" % (os.path.normpath(os.path.join(root, filename))))
169                         print("\tBad entity: %s" % (s_match.group(1)))
170
171     if DEBUG:
172         print("\t%d entities found" % (num_strings))
173
174 if __name__ == '__main__':
175     entities = parse_entities() 
176     check_xul_files(entities)