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
25 sys.stderr.write (str + '\n')
27 progress ("translations-status.py")
29 buildscript_dir = sys.argv[1]
33 sys.path.append (buildscript_dir)
36 # load gettext messages catalogs
37 translation = langdefs.translation
40 comments_re = re.compile (r'^@ignore\n(.|\n)*?\n@end ignore$|@c .*?$', re.M)
41 space_re = re.compile (r'\s+', re.M)
42 lilypond_re = re.compile (r'@lilypond({.*?}|(.|\n)*?\n@end lilypond$)', re.M)
43 node_re = re.compile ('^@node .*?$', re.M)
44 title_re = re.compile ('^@(top|chapter|(?:sub){0,2}section|' + \
45 '(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?) (.*?)$', re.M)
46 include_re = re.compile ('^@include (.*?)$', re.M)
48 translators_re = re.compile (r'^@c\s+Translators\s*:\s*(.*?)$', re.M | re.I)
49 checkers_re = re.compile (r'^@c\s+Translation\s*checkers\s*:\s*(.*?)$',
51 status_re = re.compile (r'^@c\s+Translation\s*status\s*:\s*(.*?)$', re.M | re.I)
52 post_gdp_re = re.compile ('post.GDP', re.I)
53 untranslated_node_str = 'UNTRANSLATED NODE: IGNORE ME'
54 skeleton_str = '-- SKELETON FILE --'
57 'not translated': {'color':'d0f0f8', 'short':_doc ('no'), 'abbr':'NT',
58 'long':_doc ('not translated')},
59 'partially translated': {'color':'dfef77',
60 'short':_doc ('partially (%(p)d %%)'),
62 'long':_doc ('partially translated (%(p)d %%)')},
63 'fully translated': {'color':'1fff1f', 'short':_doc ('yes'), 'abbr':'FT',
64 'long': _doc ('translated')},
65 'up to date': {'short':_doc ('yes'), 'long':_doc ('up to date'),
66 'abbr':'100%%', 'vague':_doc ('up to date')},
67 'outdated': {'short':_doc ('partially (%(p)d %%)'), 'abbr':'%(p)d%%',
68 'vague':_doc ('partially up to date')},
69 'N/A': {'short':_doc ('N/A'), 'abbr':'N/A', 'color':'d587ff', 'vague':''},
70 'pre-GDP':_doc ('pre-GDP'),
71 'post-GDP':_doc ('post-GDP')
75 # (Unumbered/Numbered/Lettered, level)
77 'unnumbered': ('u', 1),
78 'unnumberedsec': ('u', 2),
79 'unnumberedsubsec': ('u', 3),
82 'subsection': ('n', 3),
86 appendix_number_trans = string.maketrans ('@ABCDEFGHIJKLMNOPQRSTUVWXY',
87 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
89 class SectionNumber (object):
91 self.__data = [[0,'u']]
93 def __increase_last_index (self):
94 type = self.__data[-1][1]
96 self.__data[-1][0] = \
97 self.__data[-1][0].translate (appendix_number_trans)
99 self.__data[-1][0] += 1
102 if self.__data[-1][1] == 'u':
104 return '.'.join ([str (i[0]) for i in self.__data if i[1] != 'u']) + ' '
106 def increase (self, (type, level)):
108 self.__data = [[0,'u']]
109 while level + 1 < len (self.__data):
111 if level + 1 > len (self.__data):
112 self.__data.append ([0, type])
114 self.__data[-1][0] = '@'
115 if type == self.__data[-1][1]:
116 self.__increase_last_index ()
118 self.__data[-1] = ([0, type])
120 self.__data[-1][0] = 'A'
122 self.__data[-1][0] = 1
123 return self.format ()
126 def percentage_color (percent):
129 c = [hex (int (3 * p * b + (1 - 3 * p) * a))[2:]
130 for (a, b) in [(0xff, 0xff), (0x5c, 0xa6), (0x5c, 0x4c)]]
132 c = [hex (int ((3 * p - 1) * b + (2 - 3 * p) * a))[2:]
133 for (a, b) in [(0xff, 0xff), (0xa6, 0xff), (0x4c, 0x3d)]]
135 c = [hex (int ((3 * p - 2) * b + 3 * (1 - p) * a))[2:]
136 for (a, b) in [(0xff, 0x1f), (0xff, 0xff), (0x3d, 0x1f)]]
140 def update_word_count (text, filename, word_count):
141 return re.sub (r'(?m)^(\d+) *' + filename,
142 str (word_count).ljust (6) + filename,
145 po_msgid_re = re.compile (r'^msgid "(.*?)"(?:\n"(.*?)")*', re.M)
147 def po_word_count (po_content):
148 s = ' '.join ([''.join (t) for t in po_msgid_re.findall (po_content)])
149 return len (space_re.split (s))
151 sgml_tag_re = re.compile (r'<.*?>', re.S)
153 def sgml_word_count (sgml_doc):
154 s = sgml_tag_re.sub ('', sgml_doc)
155 return len (space_re.split (s))
157 def tely_word_count (tely_doc):
159 Calculate word count of a Texinfo document node by node.
161 Take string tely_doc as an argument.
162 Return a list of integers.
164 Texinfo comments and @lilypond blocks are not included in word counts.
166 tely_doc = comments_re.sub ('', tely_doc)
167 tely_doc = lilypond_re.sub ('', tely_doc)
168 nodes = node_re.split (tely_doc)
169 return [len (space_re.split (n)) for n in nodes]
172 class TelyDocument (object):
173 def __init__ (self, filename):
174 self.filename = filename
175 self.contents = open (filename).read ()
177 ## record title and sectionning level of first Texinfo section
178 m = title_re.search (self.contents)
180 self.title = m.group (2)
181 self.level = texi_level [m.group (1)]
183 self.title = 'Untitled'
184 self.level = ('u', 1)
186 included_files = [os.path.join (os.path.dirname (filename), t)
187 for t in include_re.findall (self.contents)]
188 self.included_files = [p for p in included_files if os.path.exists (p)]
190 def print_title (self, section_number):
191 return section_number.increase (self.level) + self.title
194 class TranslatedTelyDocument (TelyDocument):
195 def __init__ (self, filename, masterdocument, parent_translation=None):
196 TelyDocument.__init__ (self, filename)
198 self.masterdocument = masterdocument
200 ## record authoring information
201 m = translators_re.search (self.contents)
203 self.translators = [n.strip () for n in m.group (1).split (',')]
205 self.translators = parent_translation.translators
206 m = checkers_re.search (self.contents)
208 self.checkers = [n.strip () for n in m.group (1).split (',')]
209 elif isinstance (parent_translation, TranslatedTelyDocument):
210 self.checkers = parent_translation.checkers
214 ## check whether translation is pre- or post-GDP
215 m = status_re.search (self.contents)
217 self.post_gdp = bool (post_gdp_re.search (m.group (1)))
219 self.post_gdp = False
221 ## record which parts (nodes) of the file are actually translated
222 self.partially_translated = not skeleton_str in self.contents
223 nodes = node_re.split (self.contents)
224 self.translated_nodes = [not untranslated_node_str in n for n in nodes]
226 ## calculate translation percentage
227 master_total_word_count = sum (masterdocument.word_count)
228 translation_word_count = \
229 sum ([masterdocument.word_count[k] * self.translated_nodes[k]
230 for k in range (min (len (masterdocument.word_count),
231 len (self.translated_nodes)))])
232 self.translation_percentage = \
233 100 * translation_word_count / master_total_word_count
235 ## calculate how much the file is outdated
236 (diff_string, error) = \
237 buildlib.check_translated_doc (masterdocument.filename, self.contents)
239 sys.stderr.write ('warning: %s: %s' % (self.filename, error))
240 self.uptodate_percentage = None
242 diff = diff_string.splitlines ()
243 insertions = sum ([len (l) - 1 for l in diff
244 if l.startswith ('+')
245 and not l.startswith ('+++')])
246 deletions = sum ([len (l) - 1 for l in diff
247 if l.startswith ('-')
248 and not l.startswith ('---')])
249 outdateness_percentage = 50.0 * (deletions + insertions) / \
250 (masterdocument.size + 0.5 * (deletions - insertions))
251 self.uptodate_percentage = 100 - int (outdateness_percentage)
252 if self.uptodate_percentage > 100:
254 progress ("%s: strange uptodateness percentage %d %%, \
255 setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
256 self.uptodate_percentage = alternative
257 elif self.uptodate_percentage < 1:
259 progress ("%s: strange uptodateness percentage %d %%, \
260 setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
261 self.uptodate_percentage = alternative
263 def completeness (self, formats=['long']):
264 if isinstance (formats, str):
266 p = self.translation_percentage
268 status = 'not translated'
270 status = 'fully translated'
272 status = 'partially translated'
273 return dict ([(f, format_table[status][f] % locals()) for f in formats])
275 def uptodateness (self, formats=['long']):
276 if isinstance (formats, str):
278 p = self.uptodate_percentage
282 status = 'up to date'
287 if f == 'color' and p != None:
288 l['color'] = percentage_color (p)
290 l[f] = format_table[status][f] % locals ()
293 def gdp_status (self, translation=lambda s: s):
295 return translation (format-table['post-GDP'])
297 return translation (format-table['pre-GDP'])
299 def short_html_status (self):
301 if self.partially_translated:
302 s += '<br>\n '.join (self.translators) + '<br>\n'
305 '<br>\n '.join (self.checkers) + '</small><br>\n'
307 c = self.completeness (['color', 'long'])
308 s += ' <span style="background-color: #%(color)s">\
309 %(long)s</span><br>\n' % c
311 if self.partially_translated:
312 u = self.uptodateness (['vague', 'color'])
313 s += ' <span style="background-color: #%(color)s">\
314 %(vague)s</span><br>\n' % u
319 def text_status (self):
320 s = self.completeness ('abbr')['abbr'] + ' '
322 if self.partially_translated:
323 s += self.uptodateness ('abbr')['abbr'] + ' '
326 def html_status (self):
330 class MasterTelyDocument (TelyDocument):
333 parent_translations=dict ([(lang, None)
334 for lang in langdefs.LANGDICT])):
335 TelyDocument.__init__ (self, filename)
336 self.size = len (self.contents)
337 self.word_count = tely_word_count (self.contents)
338 translations = dict ([(lang, os.path.join (lang, filename))
339 for lang in langdefs.LANGDICT])
340 self.translations = \
342 TranslatedTelyDocument (translations[lang],
343 self, parent_translations.get (lang)))
344 for lang in langdefs.LANGDICT
345 if os.path.exists (translations[lang])])
346 if self.translations:
347 self.includes = [MasterTelyDocument (f, self.translations)
348 for f in self.included_files]
352 def update_word_counts (self, s):
353 s = update_word_count (s, self.filename, sum (self.word_count))
354 for i in self.includes:
355 s = i.update_word_counts (s)
358 def html_status (self, numbering=SectionNumber ()):
359 if self.title == 'Untitled' or not self.translations:
361 if self.level[1] == 0: # if self is a master document
362 s = '''<table align="center" border="2">
364 <th>%s</th>''' % self.print_title (numbering)
365 s += ''.join ([' <th>%s</th>\n' % l for l in self.translations])
367 s += ' <tr align="left">\n <td>Section titles<br>(%d)</td>\n' \
368 % sum (self.word_count)
371 s = ' <tr align="left">\n <td>%s<br>(%d)</td>\n' \
372 % (self.print_title (numbering), sum (self.word_count))
374 s += ''.join ([t.short_html_status ()
375 for t in self.translations.values ()])
377 s += ''.join ([i.html_status (numbering) for i in self.includes])
379 if self.level[1] == 0:
380 s += '</table>\n<p></p>\n'
383 def text_status (self, numbering=SectionNumber (), colspec=[48,12]):
384 if self.title == 'Untitled' or not self.translations:
388 if self.level[1] == 0: # if self is a master document
389 s += (self.print_title (numbering) + ' ').ljust (colspec[0])
390 s += ''.join (['%s'.ljust (colspec[1]) % l
391 for l in self.translations])
393 s += ('Section titles (%d)' % \
394 sum (self.word_count)).ljust (colspec[0])
398 % (self.print_title (numbering), sum (self.word_count))
399 s = s.ljust (colspec[0])
401 s += ''.join ([t.text_status ().ljust(colspec[1])
402 for t in self.translations.values ()])
404 s += ''.join ([i.text_status (numbering) for i in self.includes])
406 if self.level[1] == 0:
411 update_category_word_counts_re = re.compile (r'(?ms)^-(\d+)-(.*?\n)\d+ *total')
413 counts_re = re.compile (r'(?m)^(\d+) ')
415 def update_category_word_counts_sub (m):
416 return '-' + m.group (1) + '-' + m.group (2) + \
418 for c in counts_re.findall (m.group (2))])).ljust (6) + \
422 progress ("Reading documents...")
425 buildlib.read_pipe ("find -maxdepth 2 -name '*.tely'")[0].splitlines ()
426 master_docs = [MasterTelyDocument (os.path.normpath (filename))
427 for filename in tely_files]
428 master_docs = [doc for doc in master_docs if doc.translations]
430 main_status_page = open ('translations.template.html.in').read ()
433 #per_lang_status_pages = \
434 # dict ([(l, open (os.path.join (l, 'translations.template.html')). read ())
435 # for l in langdefs.LANGDICT
436 # if langdefs.LANGDICT[l].enabled])
438 progress ("Generating status pages...")
440 date_time = buildlib.read_pipe ('LANG= date -u')[0]
442 main_status_html = ' <p><i>Last updated %s</i></p>\n' % date_time
443 main_status_html += '\n'.join ([doc.html_status () for doc in master_docs])
445 html_re = re.compile ('<html>', re.I)
446 end_body_re = re.compile ('</body>', re.I)
448 main_status_page = html_re.sub ('''<html>
449 <!-- This page is automatically generated by translation-status.py from
450 translations.template.html.in; DO NOT EDIT !-->''', main_status_page)
452 main_status_page = end_body_re.sub (main_status_html + '\n</body>',
455 open ('translations.html.in', 'w').write (main_status_page)
457 main_status_txt = '''Documentation translations status
460 FT = fully translated
464 main_status_txt += '\n'.join ([doc.text_status () for doc in master_docs])
466 status_txt_file = 'out/translations-status.txt'
467 progress ("Writing %s..." % status_txt_file)
468 open (status_txt_file, 'w').write (main_status_txt)
470 translation_instructions_file = 'TRANSLATION'
471 progress ("Updating %s..." % translation_instructions_file)
472 translation_instructions = open (translation_instructions_file).read ()
474 for doc in master_docs:
475 translation_instructions = doc.update_word_counts (translation_instructions)
477 for html_file in re.findall (r'(?m)^\d+ *(\S+?\.html\S*?)(?: |$)',
478 translation_instructions):
479 word_count = sgml_word_count (open (html_file).read ())
480 translation_instructions = update_word_count (translation_instructions,
484 for po_file in re.findall (r'(?m)^\d+ *(\S+?\.po\S*?)(?: |$)',
485 translation_instructions):
486 word_count = po_word_count (open (po_file).read ())
487 translation_instructions = update_word_count (translation_instructions,
491 translation_instructions = \
492 update_category_word_counts_re.sub (update_category_word_counts_sub,
493 translation_instructions)
495 open (translation_instructions_file, 'w').write (translation_instructions)