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
10 Writes translations.html.in and for each LANG in LANGUAGES
11 translations.LANG.html.in
12 Writes out/translations-status.txt
13 Updates word counts in TRANSLATION
24 sys.stderr.write (str + '\n')
26 progress ("translations-status.py")
28 buildscript_dir = sys.argv[1]
29 localedir = sys.argv[2]
33 sys.path.append (buildscript_dir)
36 # load gettext messages catalogs
38 for l in langdefs.LANGUAGES:
39 if l.enabled and l.code != 'en':
40 translation[l.code] = gettext.translation('lilypond-doc', localedir, [l.code]).gettext
42 def read_pipe (command):
43 child = subprocess.Popen (command,
44 stdout = subprocess.PIPE,
45 stderr = subprocess.PIPE,
47 (output, error) = child.communicate ()
48 code = str (child.wait ())
49 if not child.stdout or child.stdout.close ():
50 print "pipe failed: %(command)s" % locals ()
52 error = code + ' ' + error
53 return (output, error)
55 comments_re = re.compile (r'^@ignore\n(.|\n)*?\n@end ignore$|@c .*?$', re.M)
56 space_re = re.compile (r'\s+', re.M)
57 lilypond_re = re.compile (r'@lilypond({.*?}|(.|\n)*?\n@end lilypond$)', re.M)
58 node_re = re.compile ('^@node .*?$', re.M)
59 title_re = re.compile ('^@(top|chapter|(?:sub){0,2}section|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?) (.*?)$', re.M)
60 include_re = re.compile ('^@include (.*?)$', re.M)
62 committish_re = re.compile ('GIT [Cc]ommittish: ([a-f0-9]+)')
63 translators_re = re.compile (r'^@c\s+Translators\s*:\s*(.*?)$', re.M | re.I)
64 checkers_re = re.compile (r'^@c\s+Translation\s*checkers\s*:\s*(.*?)$', re.M | re.I)
65 status_re = re.compile (r'^@c\s+Translation\s*status\s*:\s*(.*?)$', re.M | re.I)
66 post_gdp_re = re.compile ('post.GDP', re.I)
67 untranslated_node_str = 'UNTRANSLATED NODE: IGNORE ME'
68 skeleton_str = '-- SKELETON FILE --'
70 diff_cmd = 'git diff --no-color %(committish)s HEAD -- %(original)s | cat'
73 'not translated': {'color':'d0f0f8', 'short':_doc ('no'), 'abbr':'NT',
74 'long':_doc ('not translated')},
75 'partially translated': {'color':'dfef77', 'short':_doc ('partially (%(p)d %%)'),
76 'abbr':'%(p)d%%', 'long':_doc ('partially translated (%(p)d %%)')},
77 'fully translated': {'color':'1fff1f', 'short':_doc ('yes'), 'abbr':'FT',
78 'long': _doc ('translated')},
79 'up to date': {'short':_doc ('yes'), 'long':_doc ('up to date'), 'abbr':'100%%',
80 'vague':_doc ('up to date')},
81 'outdated': {'short':_doc ('partially (%(p)d %%)'), 'abbr':'%(p)d%%',
82 'vague':_doc ('partially up to date')},
83 'N/A': {'short':_doc ('N/A'), 'abbr':'N/A', 'color':'d587ff', 'vague':''},
84 'pre-GDP':_doc ('pre-GDP'),
85 'post-GDP':_doc ('post-GDP')
89 # (Unumbered/Numbered/Lettered, level)
91 'unnumbered': ('u', 1),
92 'unnumberedsec': ('u', 2),
93 'unnumberedsubsec': ('u', 3),
96 'subsection': ('n', 3),
100 appendix_number_trans = string.maketrans ('@ABCDEFGHIJKLMNOPQRSTUVWXY',
101 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
103 class SectionNumber (object):
105 self.__data = [[0,'u']]
107 def __increase_last_index (self):
108 type = self.__data[-1][1]
110 self.__data[-1][0] = self.__data[-1][0].translate (appendix_number_trans)
112 self.__data[-1][0] += 1
115 if self.__data[-1][1] == 'u':
117 return '.'.join ([str (i[0]) for i in self.__data if i[1] != 'u']) + ' '
119 def increase (self, (type, level)):
121 self.__data = [[0,'u']]
122 while level + 1 < len (self.__data):
124 if level + 1 > len (self.__data):
125 self.__data.append ([0, type])
127 self.__data[-1][0] = '@'
128 if type == self.__data[-1][1]:
129 self.__increase_last_index ()
131 self.__data[-1] = ([0, type])
133 self.__data[-1][0] = 'A'
135 self.__data[-1][0] = 1
136 return self.format ()
139 def percentage_color (percent):
142 c = [hex (int (3 * p * b + (1 - 3 * p) * a))[2:] for (a, b) in [(0xff, 0xff), (0x5c, 0xa6), (0x5c, 0x4c)]]
144 c = [hex (int ((3 * p - 1) * b + (2 - 3 * p) * a))[2:] for (a, b) in [(0xff, 0xff), (0xa6, 0xff), (0x4c, 0x3d)]]
146 c = [hex (int ((3 * p - 2) * b + 3 * (1 - p) * a))[2:] for (a, b) in [(0xff, 0x1f), (0xff, 0xff), (0x3d, 0x1f)]]
150 def update_word_count (text, filename, word_count):
151 return re.sub (r'(?m)^(\d+) *' + filename,
152 str (word_count).ljust (6) + filename,
155 po_msgid_re = re.compile (r'^msgid "(.*?)"(?:\n"(.*?)")*', re.M)
157 def po_word_count (po_content):
158 s = ' '.join ([''.join (t) for t in po_msgid_re.findall (po_content)])
159 return len (space_re.split (s))
161 sgml_tag_re = re.compile (r'<.*?>', re.S)
163 def sgml_word_count (sgml_doc):
164 s = sgml_tag_re.sub ('', sgml_doc)
165 return len (space_re.split (s))
167 def tely_word_count (tely_doc):
169 Calculate word count of a Texinfo document node by node.
171 Take string tely_doc as an argument.
172 Return a list of integers.
174 Texinfo comments and @lilypond blocks are not included in word counts.
176 tely_doc = comments_re.sub ('', tely_doc)
177 tely_doc = lilypond_re.sub ('', tely_doc)
178 nodes = node_re.split (tely_doc)
179 return [len (space_re.split (n)) for n in nodes]
182 class TelyDocument (object):
183 def __init__ (self, filename):
184 self.filename = filename
185 self.contents = open (filename).read ()
187 ## record title and sectionning level of first Texinfo section
188 m = title_re.search (self.contents)
190 self.title = m.group (2)
191 self.level = texi_level [m.group (1)]
193 self.title = 'Untitled'
194 self.level = ('u', 1)
196 included_files = [os.path.join (os.path.dirname (filename), t) for t in include_re.findall (self.contents)]
197 self.included_files = [p for p in included_files if os.path.exists (p)]
199 def print_title (self, section_number):
200 return section_number.increase (self.level) + self.title
203 class TranslatedTelyDocument (TelyDocument):
204 def __init__ (self, filename, masterdocument, parent_translation=None):
205 TelyDocument.__init__ (self, filename)
207 self.masterdocument = masterdocument
209 ## record authoring information
210 m = translators_re.search (self.contents)
212 self.translators = [n.strip () for n in m.group (1).split (',')]
214 self.translators = parent_translation.translators
215 m = checkers_re.search (self.contents)
217 self.checkers = [n.strip () for n in m.group (1).split (',')]
218 elif isinstance (parent_translation, TranslatedTelyDocument):
219 self.checkers = parent_translation.checkers
223 ## check whether translation is pre- or post-GDP
224 m = status_re.search (self.contents)
226 self.post_gdp = bool (post_gdp_re.search (m.group (1)))
228 self.post_gdp = False
230 ## record which parts (nodes) of the file are actually translated
231 self.partially_translated = not skeleton_str in self.contents
232 nodes = node_re.split (self.contents)
233 self.translated_nodes = [not untranslated_node_str in n for n in nodes]
235 ## calculate translation percentage
236 master_total_word_count = sum (masterdocument.word_count)
237 translation_word_count = sum ([masterdocument.word_count[k] * self.translated_nodes[k]
238 for k in range (min (len (masterdocument.word_count), len (self.translated_nodes)))])
239 self.translation_percentage = 100 * translation_word_count / master_total_word_count
241 ## calculate how much the file is outdated
242 m = committish_re.search (self.contents)
244 sys.stderr.write ('error: ' + filename + \
245 ": no 'GIT committish: <hash>' found.\nPlease check " + \
246 'the whole file against the original in English, then ' + \
247 'fill in HEAD committish in the header.\n')
249 (diff_string, error) = read_pipe (diff_cmd % {'committish':m.group (1), 'original':masterdocument.filename})
251 sys.stderr.write ('warning: %s: %s' % (self.filename, error))
252 self.uptodate_percentage = None
254 diff = diff_string.splitlines ()
255 insertions = sum ([len (l) - 1 for l in diff if l.startswith ('+') and not l.startswith ('+++')])
256 deletions = sum ([len (l) - 1 for l in diff if l.startswith ('-') and not l.startswith ('---')])
257 outdateness_percentage = 50.0 * (deletions + insertions) / (masterdocument.size + 0.5 * (deletions - insertions))
258 self.uptodate_percentage = 100 - int (outdateness_percentage)
259 if self.uptodate_percentage > 100:
261 progress ("%s: strange uptodateness percentage %d %%, setting to %d %%" \
262 % (self.filename, self.uptodate_percentage, alternative))
263 self.uptodate_percentage = alternative
264 elif self.uptodate_percentage < 1:
266 progress ("%s: strange uptodateness percentage %d %%, setting to %d %%" \
267 % (self.filename, self.uptodate_percentage, alternative))
268 self.uptodate_percentage = alternative
270 def completeness (self, formats=['long']):
271 if isinstance (formats, str):
273 p = self.translation_percentage
275 status = 'not translated'
277 status = 'fully translated'
279 status = 'partially translated'
280 return dict ([(f, format_table[status][f] % locals()) for f in formats])
282 def uptodateness (self, formats=['long']):
283 if isinstance (formats, str):
285 p = self.uptodate_percentage
289 status = 'up to date'
294 if f == 'color' and p != None:
295 l['color'] = percentage_color (p)
297 l[f] = format_table[status][f] % locals ()
300 def gdp_status (self, translation=lambda s: s):
302 return translation (format-table['post-GDP'])
304 return translation (format-table['pre-GDP'])
306 def short_html_status (self):
308 if self.partially_translated:
309 s += '<br>\n '.join (self.translators) + '<br>\n'
311 s += ' <small>' + '<br>\n '.join (self.checkers) + '</small><br>\n'
313 c = self.completeness (['color', 'long'])
314 s += ' <span style="background-color: #%(color)s">%(long)s</span><br>\n' % c
316 if self.partially_translated:
317 u = self.uptodateness (['vague', 'color'])
318 s += ' <span style="background-color: #%(color)s">%(vague)s</span><br>\n' % u
323 def text_status (self):
324 s = self.completeness ('abbr')['abbr'] + ' '
326 if self.partially_translated:
327 s += self.uptodateness ('abbr')['abbr'] + ' '
330 def html_status (self):
334 class MasterTelyDocument (TelyDocument):
335 def __init__ (self, filename, parent_translations=dict ([(lang, None) for lang in langdefs.LANGDICT.keys()])):
336 TelyDocument.__init__ (self, filename)
337 self.size = len (self.contents)
338 self.word_count = tely_word_count (self.contents)
339 translations = dict ([(lang, os.path.join (lang, filename)) for lang in langdefs.LANGDICT.keys()])
340 self.translations = dict ([(lang, TranslatedTelyDocument (translations[lang], self, parent_translations.get (lang)))
341 for lang in langdefs.LANGDICT.keys() if os.path.exists (translations[lang])])
342 if self.translations:
343 self.includes = [MasterTelyDocument (f, self.translations) for f in self.included_files]
347 def update_word_counts (self, s):
348 s = update_word_count (s, self.filename, sum (self.word_count))
349 for i in self.includes:
350 s = i.update_word_counts (s)
353 def html_status (self, numbering=SectionNumber ()):
354 if self.title == 'Untitled' or not self.translations:
356 if self.level[1] == 0: # if self is a master document
357 s = '''<table align="center" border="2">
359 <th>%s</th>''' % self.print_title (numbering)
360 s += ''.join ([' <th>%s</th>\n' % l for l in self.translations.keys ()])
362 s += ' <tr align="left">\n <td>Section titles<br>(%d)</td>\n' \
363 % sum (self.word_count)
366 s = ' <tr align="left">\n <td>%s<br>(%d)</td>\n' \
367 % (self.print_title (numbering), sum (self.word_count))
369 s += ''.join ([t.short_html_status () for t in self.translations.values ()])
371 s += ''.join ([i.html_status (numbering) for i in self.includes])
373 if self.level[1] == 0:
374 s += '</table>\n<p></p>\n'
377 def text_status (self, numbering=SectionNumber (), colspec=[48,12]):
378 if self.title == 'Untitled' or not self.translations:
382 if self.level[1] == 0: # if self is a master document
383 s += (self.print_title (numbering) + ' ').ljust (colspec[0])
384 s += ''.join (['%s'.ljust (colspec[1]) % l for l in self.translations.keys ()])
386 s += ('Section titles (%d)' % sum (self.word_count)).ljust (colspec[0])
390 % (self.print_title (numbering), sum (self.word_count))
391 s = s.ljust (colspec[0])
393 s += ''.join ([t.text_status ().ljust(colspec[1]) for t in self.translations.values ()])
395 s += ''.join ([i.text_status (numbering) for i in self.includes])
397 if self.level[1] == 0:
402 update_category_word_counts_re = re.compile (r'(?ms)^-(\d+)-(.*?\n)\d+ *total')
404 counts_re = re.compile (r'(?m)^(\d+) ')
406 def update_category_word_counts_sub (m):
407 return '-' + m.group (1) + '-' + m.group (2) + \
408 str (sum ([int (c) for c in counts_re.findall (m.group (2))])).ljust (6) + 'total'
411 progress ("Reading documents...")
413 tely_files = read_pipe ("find -maxdepth 2 -name '*.tely'")[0].splitlines ()
414 master_docs = [MasterTelyDocument (os.path.normpath (filename)) for filename in tely_files]
415 master_docs = [doc for doc in master_docs if doc.translations]
417 main_status_page = open ('translations.template.html.in').read ()
420 #per_lang_status_pages = dict ([(l, open (os.path.join (l, 'translations.template.html')). read ())
421 # for l in langdefs.LANGDICT.keys ()
422 # if langdefs.LANGDICT[l].enabled])
424 progress ("Generating status pages...")
426 date_time = read_pipe ('LANG= date -u')[0]
428 main_status_html = ' <p><i>Last updated %s</i></p>\n' % date_time
429 main_status_html += '\n'.join ([doc.html_status () for doc in master_docs])
431 html_re = re.compile ('<html>', re.I)
432 end_body_re = re.compile ('</body>', re.I)
434 main_status_page = html_re.sub ('''<html>
435 <!-- This page is automatically generated by translation-status.py from
436 translations.template.html.in; DO NOT EDIT !-->''', main_status_page)
438 main_status_page = end_body_re.sub (main_status_html + '\n</body>', main_status_page)
440 open ('translations.html.in', 'w').write (main_status_page)
442 main_status_txt = '''Documentation translations status
445 FT = fully translated
449 main_status_txt += '\n'.join ([doc.text_status () for doc in master_docs])
451 status_txt_file = 'out/translations-status.txt'
452 progress ("Writing %s..." % status_txt_file)
453 open (status_txt_file, 'w').write (main_status_txt)
455 translation_instructions_file = 'TRANSLATION'
456 progress ("Updating %s..." % translation_instructions_file)
457 translation_instructions = open (translation_instructions_file).read ()
459 for doc in master_docs:
460 translation_instructions = doc.update_word_counts (translation_instructions)
462 for html_file in re.findall (r'(?m)^\d+ *(\S+?\.html\S*?)(?: |$)', translation_instructions):
463 word_count = sgml_word_count (open (html_file).read ())
464 translation_instructions = update_word_count (translation_instructions,
468 for po_file in re.findall (r'(?m)^\d+ *(\S+?\.po\S*?)(?: |$)', translation_instructions):
469 word_count = po_word_count (open (po_file).read ())
470 translation_instructions = update_word_count (translation_instructions,
474 translation_instructions = update_category_word_counts_re.sub (update_category_word_counts_sub,
475 translation_instructions)
477 open (translation_instructions_file, 'w').write (translation_instructions)