]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/check_translation.py
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / scripts / auxiliar / check_translation.py
1 #!/usr/bin/env python
2
3 import __main__
4 import optparse
5 import os
6 import sys
7 import re
8
9 import langdefs
10 import buildlib
11
12 verbose = 0
13 use_colors = False
14 lang = 'C'
15 C = lang
16
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)
21
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'
24
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)
29
30
31 def do_file (file_name, lang_codes):
32     if verbose:
33         sys.stderr.write ('%s...\n' % file_name)
34     split_file_name = file_name.split ('/')
35     d1, d2 = split_file_name[0:2]
36     if d1 in lang_codes:
37         check_lang = d1
38         lang_dir_index = 0
39     elif d2 in lang_codes:
40         check_lang = d2
41         lang_dir_index = 1
42     else:
43         check_lang = lang
44     if check_lang == C:
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']
50         else:
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 ()
58                 break
59         else:
60             return
61         original = file_name.replace ('texidocs' + os.path.sep, 'lsr' + os.path.sep, 1)
62         original = original.replace ('.texidoc', '.ly', 1)
63
64         # URG dirty .texidoc files manipulation in a dirty way
65         if touch_committishes and buildlib.check_translated_doc (original,
66                                          file_name,
67                                          translated_contents,
68                                          color=use_colors and not update_mode)[1]:
69             (estimated_revision, error) = buildlib.read_pipe (vc_last_texidoc_revision % vars ())
70             if error:
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:])
79             return
80
81     else:
82         original = dir_lang (file_name, '', lang_dir_index)
83         translated_contents = open (file_name).read ()
84
85     (diff_string, error) \
86         = buildlib.check_translated_doc (original,
87                                          file_name,
88                                          translated_contents,
89                                          color=use_colors and not update_mode)
90
91     if error:
92         sys.stderr.write ('warning: %s: %s' % (file_name, error))
93
94     if update_mode:
95         if error or len (diff_string) >= os.path.getsize (original):
96             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
97         elif diff_string:
98             diff_file = original + '.diff'
99             f = open (diff_file, 'w')
100             f.write (diff_string)
101             f.close ()
102             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
103             os.remove (diff_file)
104     else:
105         sys.stdout.write (diff_string)
106
107 def usage ():
108     sys.stdout.write (r'''
109 Usage:
110 check-translation [--language=LANG] [--verbose] [--update] FILE...
111
112 This script is licensed under the GNU GPL.
113 ''')
114
115 def do_options ():
116     global lang, verbose, update_mode, touch_committishes, use_colors
117
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",
121                   action='store',
122                   default=C,
123                   dest="language")
124     p.add_option ("--no-color",
125                   action='store_false',
126                   default=True,
127                   dest="color",
128                   help="do not print ANSI-colored output")
129     p.add_option ("--verbose",
130                   action='store_true',
131                   default=False,
132                   dest="verbose",
133                   help="print details, including executed shell commands")
134     p.add_option ('-t',
135                   action='store_true',
136                   default=False,
137                   dest="touch_committishes",
138                   help=optparse.SUPPRESS_HELP)
139     p.add_option ('-u', "--update",
140                   action='store_true',
141                   default=False,
142                   dest='update_mode',
143                   help='call $EDITOR to update the translation')
144     
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
151     
152     return files
153
154 def main ():
155     global update_mode, text_editor
156
157     files = do_options ()
158     if 'EDITOR' in os.environ:
159         text_editor = os.environ['EDITOR']
160     else:
161         update_mode = False
162     
163     buildlib.verbose = verbose
164
165     for i in files:
166         do_file (i, langdefs.LANGDICT.keys ())
167
168 if __name__ == '__main__':
169     main ()