17 def dir_lang (file, lang, lang_dir_index):
18 path_components = file.split ('/')
19 path_components[lang_dir_index] = lang
20 return os.path.join (*path_components)
22 vc_last_revision = 'git log --pretty=format:%%H %(file_name)s | head -1'
23 vc_last_texidoc_revision = 'git log -Stexidoc%(preferred_language)s --pretty=format:%%H %(file_name)s | head -1'
25 s = 'Translation of GIT [Cc]ommittish'
26 texidoc_chunk_re = re.compile (r'^((?:%+\s*' + s + \
27 r'.+)?\s*(?:texidoc|doctitle)([a-zA-Z]{2,4})\s+=(?:.|\n)*?)(?=%+\s*' + \
28 s + r'|\s*(?:texidoc|doctitle)(?:[a-zA-Z]{2,4})\s+=|$(?!.|\n))', re.M)
31 def do_file (file_name, lang_codes):
33 sys.stderr.write ('%s...\n' % file_name)
34 split_file_name = file_name.split ('/')
35 d1, d2 = split_file_name[0:2]
39 elif d2 in lang_codes:
45 if not os.path.splitext (file_name)[1] == '.texidoc':
46 raise Exception ('cannot determine language for ' + file_name)
47 translated_contents = open (file_name).read ()
48 if 'ISOLANG' in os.environ:
49 preferred_language = os.environ['ISOLANG']
51 raise Exception ('cannot determine language for ' + file_name)
52 for m in texidoc_chunk_re.finditer (translated_contents):
53 if m.group (2) == preferred_language:
54 full_translated_contents = translated_contents
55 translated_contents = m.group (1)
56 translated_contents_start = m.start ()
57 translated_contents_end = m.end ()
61 original = file_name.replace ('texidocs' + os.path.sep, 'lsr' + os.path.sep, 1)
62 original = original.replace ('.texidoc', '.ly', 1)
64 # URG dirty .texidoc files manipulation in a dirty way
65 if touch_committishes and buildlib.check_translated_doc (original,
68 color=use_colors and not update_mode)[1]:
69 (estimated_revision, error) = buildlib.read_pipe (vc_last_texidoc_revision % vars ())
71 sys.stderr.write ('warning: %s: %s' % (file_name, error))
72 estimated_revision = estimated_revision.strip ()
73 translated_contents = re.sub (r'(?m)^%+\s*Translation of GIT committish:.*\n', '', translated_contents)
74 f = open (file_name, 'w')
75 f.write (full_translated_contents[:translated_contents_start])
76 f.write ('%% Translation of GIT committish: ' + estimated_revision + '\n')
77 f.write (translated_contents)
78 f.write (full_translated_contents[translated_contents_end:])
82 original = dir_lang (file_name, '', lang_dir_index)
83 translated_contents = open (file_name).read ()
85 (diff_string, error) \
86 = buildlib.check_translated_doc (original,
89 color=use_colors and not update_mode)
92 sys.stderr.write ('warning: %s: %s' % (file_name, error))
95 if error or len (diff_string) >= os.path.getsize (original):
96 buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
98 diff_file = original + '.diff'
99 f = open (diff_file, 'w')
100 f.write (diff_string)
102 buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
103 os.remove (diff_file)
105 sys.stdout.write (diff_string)
108 sys.stdout.write (r'''
110 check-translation [--language=LANG] [--verbose] [--update] FILE...
112 This script is licensed under the GNU GPL.
116 global lang, verbose, update_mode, touch_committishes, use_colors
118 p = optparse.OptionParser (usage="check-translation [--language=LANG] [--verbose] FILE...",
119 description="This script is licensed under the GNU GPL.")
120 p.add_option ("--language",
124 p.add_option ("--no-color",
125 action='store_false',
128 help="do not print ANSI-colored output")
129 p.add_option ("--verbose",
133 help="print details, including executed shell commands")
137 dest="touch_committishes",
138 help=optparse.SUPPRESS_HELP)
139 p.add_option ('-u', "--update",
143 help='call $EDITOR to update the translation')
145 (options, files) = p.parse_args ()
146 verbose = options.verbose
147 lang = options.language
148 use_colors = options.color
149 update_mode = options.update_mode
150 touch_committishes = options.touch_committishes
155 global update_mode, text_editor
157 files = do_options ()
158 if 'EDITOR' in os.environ:
159 text_editor = os.environ['EDITOR']
163 buildlib.verbose = verbose
166 do_file (i, langdefs.LANGDICT.keys ())
168 if __name__ == '__main__':