4 USAGE: translations-status.py BUILDSCRIPT-DIR LOCALEDIR
6 This script must be run from Documentation/
8 Reads template files translations.template.html.in
9 and for each LANG in LANGUAGES LANG/translations.template.html.in
11 Writes translations.html.in and for each LANG in LANGUAGES
12 translations.LANG.html.in
23 sys.stderr.write (str + '\n')
25 progress ("translations-status.py")
27 buildscript_dir = sys.argv[1]
28 localedir = sys.argv[2]
32 sys.path.append (buildscript_dir)
35 # load gettext messages catalogs
37 for l in langdefs.LANGUAGES:
38 if l.enabled and l.code != 'en':
39 translation[l.code] = gettext.translation('lilypond-doc', localedir, [l.code]).gettext
41 def read_pipe (command):
42 child = subprocess.Popen (command,
43 stdout = subprocess.PIPE,
44 stderr = subprocess.PIPE,
46 (output, error) = child.communicate ()
47 code = str (child.wait ())
48 if not child.stdout or child.stdout.close ():
49 print "pipe failed: %(command)s" % locals ()
51 error = code + ' ' + error
52 return (output, error)
54 comments_re = re.compile (r'^@ignore\n(.|\n)*?\n@end ignore$|@c .*?$', re.M)
55 space_re = re.compile (r'\s+', re.M)
56 lilypond_re = re.compile (r'@lilypond({.*?}|(.|\n)*?\n@end lilypond$)', re.M)
57 node_re = re.compile ('^@node .*?$', re.M)
58 title_re = re.compile ('^@(top|chapter|(?:sub){0,2}section|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?) (.*?)$', re.M)
59 include_re = re.compile ('^@include (.*?)$', re.M)
61 committish_re = re.compile ('GIT [Cc]ommittish: ([a-f0-9]+)')
62 translators_re = re.compile (r'^@c\s+Translators\s*:\s*(.*?)$', re.M | re.I)
63 checkers_re = re.compile (r'^@c\s+Translation\s*checkers\s*:\s*(.*?)$', re.M | re.I)
64 status_re = re.compile (r'^@c\s+Translation\s*status\s*:\s*(.*?)$', re.M | re.I)
65 post_gdp_re = re.compile ('post.GDP', re.I)
66 untranslated_node_str = 'UNTRANSLATED NODE: IGNORE ME'
67 skeleton_str = '-- SKELETON FILE --'
69 diff_cmd = 'git diff --no-color %(committish)s HEAD -- %(original)s | cat'
72 'not translated': {'color':'d0f0f8', 'short':_doc ('no'), 'abbr':'NT',
73 'long':_doc ('not translated')},
74 'partially translated': {'color':'dfef77', 'short':_doc ('partially (%(p)d %%)'),
75 'abbr':'%(p)d%%', 'long':_doc ('partially translated (%(p)d %%)')},
76 'fully translated': {'color':'1fff1f', 'short':_doc ('yes'), 'abbr':'FT',
77 'long': _doc ('translated')},
78 'up to date': {'short':_doc ('yes'), 'long':_doc ('up to date'), 'abbr':'100%%',
79 'vague':_doc ('up to date')},
80 'outdated': {'short':_doc ('partially (%(p)d %%)'), 'abbr':'%(p)d%%',
81 'vague':_doc ('partially up to date')},
82 'N/A': {'short':_doc ('N/A'), 'abbr':'N/A', 'color':'d587ff', 'vague':''},
83 'pre-GDP':_doc ('pre-GDP'),
84 'post-GDP':_doc ('post-GDP')
88 # (Unumbered/Numbered/Lettered, level)
90 'unnumbered': ('u', 1),
91 'unnumberedsec': ('u', 2),
92 'unnumberedsubsec': ('u', 3),
95 'subsection': ('n', 3),
99 appendix_number_trans = string.maketrans ('@ABCDEFGHIJKLMNOPQRSTUVWXY',
100 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
102 class SectionNumber (object):
104 self.__data = [[0,'u']]
106 def __increase_last_index (self):
107 type = self.__data[-1][1]
109 self.__data[-1][0] = self.__data[-1][0].translate (appendix_number_trans)
111 self.__data[-1][0] += 1
114 if self.__data[-1][1] == 'u':
116 return '.'.join ([str (i[0]) for i in self.__data if i[1] != 'u']) + ' '
118 def increase (self, (type, level)):
120 self.__data = [[0,'u']]
121 while level + 1 < len (self.__data):
123 if level + 1 > len (self.__data):
124 self.__data.append ([0, type])
126 self.__data[-1][0] = '@'
127 if type == self.__data[-1][1]:
128 self.__increase_last_index ()
130 self.__data[-1] = ([0, type])
132 self.__data[-1][0] = 'A'
134 self.__data[-1][0] = 1
135 return self.format ()
138 def percentage_color (percent):
141 c = [hex (int (3 * p * b + (1 - 3 * p) * a))[2:] for (a, b) in [(0xff, 0xff), (0x5c, 0xa6), (0x5c, 0x4c)]]
143 c = [hex (int ((3 * p - 1) * b + (2 - 3 * p) * a))[2:] for (a, b) in [(0xff, 0xff), (0xa6, 0xff), (0x4c, 0x3d)]]
145 c = [hex (int ((3 * p - 2) * b + 3 * (1 - p) * a))[2:] for (a, b) in [(0xff, 0x1f), (0xff, 0xff), (0x3d, 0x1f)]]
148 def tely_word_count (tely_doc):
150 Calculate word count of a Texinfo document node by node.
152 Take string tely_doc as an argument.
153 Return a list of integers.
155 Texinfo comments and @lilypond blocks are not included in word counts.
157 tely_doc = comments_re.sub ('', tely_doc)
158 tely_doc = lilypond_re.sub ('', tely_doc)
159 nodes = node_re.split (tely_doc)
160 return [len (space_re.split (n)) for n in nodes]
163 class TelyDocument (object):
164 def __init__ (self, filename):
165 self.filename = filename
166 self.contents = open (filename).read ()
168 ## record title and sectionning level of first Texinfo section
169 m = title_re.search (self.contents)
171 self.title = m.group (2)
172 self.level = texi_level [m.group (1)]
174 self.title = 'Untitled'
175 self.level = ('u', 1)
177 included_files = [os.path.join (os.path.dirname (filename), t) for t in include_re.findall (self.contents)]
178 self.included_files = [p for p in included_files if os.path.exists (p)]
180 def print_title (self, section_number):
181 return section_number.increase (self.level) + self.title
184 class TranslatedTelyDocument (TelyDocument):
185 def __init__ (self, filename, masterdocument, parent_translation=None):
186 TelyDocument.__init__ (self, filename)
188 self.masterdocument = masterdocument
190 ## record authoring information
191 m = translators_re.search (self.contents)
193 self.translators = [n.strip () for n in m.group (1).split (',')]
195 self.translators = parent_translation.translators
196 m = checkers_re.search (self.contents)
198 self.checkers = [n.strip () for n in m.group (1).split (',')]
199 elif isinstance (parent_translation, TranslatedTelyDocument):
200 self.checkers = parent_translation.checkers
204 ## check whether translation is pre- or post-GDP
205 m = status_re.search (self.contents)
207 self.post_gdp = bool (post_gdp_re.search (m.group (1)))
209 self.post_gdp = False
211 ## record which parts (nodes) of the file are actually translated
212 self.partially_translated = not skeleton_str in self.contents
213 nodes = node_re.split (self.contents)
214 self.translated_nodes = [not untranslated_node_str in n for n in nodes]
216 ## calculate translation percentage
217 master_total_word_count = sum (masterdocument.word_count)
218 translation_word_count = sum ([masterdocument.word_count[k] * self.translated_nodes[k]
219 for k in range (min (len (masterdocument.word_count), len (self.translated_nodes)))])
220 self.translation_percentage = 100 * translation_word_count / master_total_word_count
222 ## calculate how much the file is outdated
223 m = committish_re.search (self.contents)
225 sys.stderr.write ('error: ' + filename + \
226 ": no 'GIT committish: <hash>' found.\nPlease check " + \
227 'the whole file against the original in English, then ' + \
228 'fill in HEAD committish in the header.\n')
230 (diff_string, error) = read_pipe (diff_cmd % {'committish':m.group (1), 'original':masterdocument.filename})
232 sys.stderr.write ('warning: %s: %s' % (self.filename, error))
233 self.uptodate_percentage = None
235 diff = diff_string.splitlines ()
236 insertions = sum ([len (l) - 1 for l in diff if l.startswith ('+') and not l.startswith ('+++')])
237 deletions = sum ([len (l) - 1 for l in diff if l.startswith ('-') and not l.startswith ('---')])
238 outdateness_percentage = 50.0 * (deletions + insertions) / (masterdocument.size + 0.5 * (deletions - insertions))
239 self.uptodate_percentage = 100 - int (outdateness_percentage)
240 if self.uptodate_percentage > 100:
242 progress ("%s: strange uptodateness percentage %d %%, setting to %d %%" \
243 % (self.filename, self.uptodate_percentage, alternative))
244 self.uptodate_percentage = alternative
245 elif self.uptodate_percentage < 1:
247 progress ("%s: strange uptodateness percentage %d %%, setting to %d %%" \
248 % (self.filename, self.uptodate_percentage, alternative))
249 self.uptodate_percentage = alternative
251 def completeness (self, formats=['long']):
252 if isinstance (formats, str):
254 p = self.translation_percentage
256 status = 'not translated'
258 status = 'fully translated'
260 status = 'partially translated'
261 return dict ([(f, format_table[status][f] % locals()) for f in formats])
263 def uptodateness (self, formats=['long']):
264 if isinstance (formats, str):
266 p = self.uptodate_percentage
270 status = 'up to date'
275 if f == 'color' and p != None:
276 l['color'] = percentage_color (p)
278 l[f] = format_table[status][f] % locals ()
281 def gdp_status (self, translation=lambda s: s):
283 return translation (format-table['post-GDP'])
285 return translation (format-table['pre-GDP'])
287 def short_html_status (self):
289 if self.partially_translated:
290 s += '<br>\n '.join (self.translators) + '<br>\n'
292 s += ' <small>' + '<br>\n '.join (self.checkers) + '</small><br>\n'
294 c = self.completeness (['color', 'long'])
295 s += ' <span style="background-color: #%(color)s">%(long)s</span><br>\n' % c
297 if self.partially_translated:
298 u = self.uptodateness (['vague', 'color'])
299 s += ' <span style="background-color: #%(color)s">%(vague)s</span><br>\n' % u
304 def text_status (self):
305 s = self.completeness ('abbr')['abbr'] + ' '
307 if self.partially_translated:
308 s += self.uptodateness ('abbr')['abbr'] + ' '
311 def html_status (self):
315 class MasterTelyDocument (TelyDocument):
316 def __init__ (self, filename, parent_translations=dict ([(lang, None) for lang in langdefs.LANGDICT.keys()])):
317 TelyDocument.__init__ (self, filename)
318 self.size = len (self.contents)
319 self.word_count = tely_word_count (self.contents)
320 translations = dict ([(lang, os.path.join (lang, filename)) for lang in langdefs.LANGDICT.keys()])
321 self.translations = dict ([(lang, TranslatedTelyDocument (translations[lang], self, parent_translations.get (lang)))
322 for lang in langdefs.LANGDICT.keys() if os.path.exists (translations[lang])])
323 if self.translations:
324 self.includes = [MasterTelyDocument (f, self.translations) for f in self.included_files]
329 def print_wc_priority (self):
332 def html_status (self, numbering=SectionNumber ()):
333 if self.title == 'Untitled' or not self.translations:
335 if self.level[1] == 0: # if self is a master document
336 s = '''<table align="center" border="2">
338 <th>%s</th>''' % self.print_title (numbering)
339 s += ''.join ([' <th>%s</th>\n' % l for l in self.translations.keys ()])
341 s += ' <tr align="left">\n <td>Section titles<br>(%d)</td>\n' \
342 % sum (self.word_count)
345 s = ' <tr align="left">\n <td>%s<br>(%d)</td>\n' \
346 % (self.print_title (numbering), sum (self.word_count))
348 s += ''.join ([t.short_html_status () for t in self.translations.values ()])
350 s += ''.join ([i.html_status (numbering) for i in self.includes])
352 if self.level[1] == 0:
353 s += '</table>\n<p></p>\n'
356 def text_status (self, numbering=SectionNumber (), colspec=[48,12]):
357 if self.title == 'Untitled' or not self.translations:
361 if self.level[1] == 0: # if self is a master document
362 s += (self.print_title (numbering) + ' ').ljust (colspec[0])
363 s += ''.join (['%s'.ljust (colspec[1]) % l for l in self.translations.keys ()])
365 s += ('Section titles (%d)' % sum (self.word_count)).ljust (colspec[0])
369 % (self.print_title (numbering), sum (self.word_count))
370 s = s.ljust (colspec[0])
372 s += ''.join ([t.text_status ().ljust(colspec[1]) for t in self.translations.values ()])
374 s += ''.join ([i.text_status (numbering) for i in self.includes])
376 if self.level[1] == 0:
381 progress ("Reading documents...")
383 tely_files = read_pipe ("find -maxdepth 2 -name '*.tely'")[0].splitlines ()
384 master_docs = [MasterTelyDocument (os.path.normpath (filename)) for filename in tely_files]
385 master_docs = [doc for doc in master_docs if doc.translations]
387 main_status_page = open ('translations.template.html.in').read ()
390 #per_lang_status_pages = dict ([(l, open (os.path.join (l, 'translations.template.html')). read ())
391 # for l in langdefs.LANGDICT.keys ()
392 # if langdefs.LANGDICT[l].enabled])
394 progress ("Generating status pages...")
396 date_time = read_pipe ('LANG= date -u')[0]
398 main_status_html = ' <p><i>Last updated %s</i></p>\n' % date_time
399 main_status_html += '\n'.join ([doc.html_status () for doc in master_docs])
401 html_re = re.compile ('<html>', re.I)
402 end_body_re = re.compile ('</body>', re.I)
404 main_status_page = html_re.sub ('''<html>
405 <!-- This page is automatically generated by translation-status.py from
406 translations.template.html.in; DO NOT EDIT !-->''', main_status_page)
408 main_status_page = end_body_re.sub (main_status_html + '\n</body>', main_status_page)
410 open ('translations.html.in', 'w').write (main_status_page)
412 main_status_txt = '''Documentation translations status
415 FT = fully translated
419 main_status_txt += '\n'.join ([doc.text_status () for doc in master_docs])
421 status_txt_file = 'out/translations-status.txt'
422 progress ("Writing %s..." % status_txt_file)
423 open (status_txt_file, 'w').write (main_status_txt)