]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/check_translation.py
Bump license to GPL v3+.
[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 # ugh, this is complicated; where has the good old 'git rev-parse' gone?
23 vc_revision_parse = 'git log -1 --pretty=format:%%H %s'
24
25 def do_file (file_name, lang_codes):
26     if verbose:
27         sys.stderr.write ('%s...\n' % file_name)
28     split_file_name = file_name.split ('/')
29     d1, d2 = split_file_name[0:2]
30     if d1 in lang_codes:
31         check_lang = d1
32         lang_dir_index = 0
33     elif d2 in lang_codes:
34         check_lang = d2
35         lang_dir_index = 1
36     else:
37         check_lang = lang
38     if check_lang == C:
39         raise Exception ('cannot determine language for ' + file_name)
40     else:
41         if os.path.splitext (file_name)[1] == '.texidoc':
42             original = file_name.replace (os.path.join (check_lang, 'texidocs'), 'snippets', 1)
43             original = original.replace ('.texidoc', '.ly', 1)
44         else:
45             original = dir_lang (file_name, '', lang_dir_index)
46         translated_contents = open (file_name).read ()
47
48         ## experimental feature
49         if not touch_committishes in (current_revision, 'HEAD'):
50             (changes_in_original, error) = \
51                 buildlib.check_translated_doc (original,
52                                                file_name,
53                                                translated_contents,
54                                                upper_revision=touch_committishes)
55             if not error and not changes_in_original in ('', '\n'):
56                 translated_contents = \
57                     buildlib.revision_re.sub ('GIT committish: ' + current_revision,
58                                               translated_contents, 1)
59                 f = open (file_name, 'w').write (translated_contents)
60                 return
61     (diff_string, error) \
62         = buildlib.check_translated_doc (original,
63                                          file_name,
64                                          translated_contents,
65                                          color=use_colors and not update_mode)
66
67     if error:
68         sys.stderr.write ('warning: %s: %s' % (file_name, error))
69
70     if update_mode:
71         if error or len (diff_string) >= os.path.getsize (original):
72             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
73         elif diff_string:
74             diff_file = original + '.diff'
75             f = open (diff_file, 'w')
76             f.write (diff_string)
77             f.close ()
78             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
79             os.remove (diff_file)
80     else:
81         sys.stdout.write (diff_string)
82
83 def usage ():
84     sys.stdout.write (r'''
85 Usage:
86 check-translation [--language=LANG] [--verbose] [--update] [-t COMMIT] FILE...
87 ''')
88
89 def do_options ():
90     global lang, verbose, update_mode, touch_committishes, use_colors
91
92     p = optparse.OptionParser (usage=\
93 "check-translation [--language=LANG] [--verbose] [--update] [-t COMMIT] FILE...",
94                                description="")
95     p.add_option ("--language",
96                   action='store',
97                   default=C,
98                   dest="language",
99                   metavar='LL',
100                   help="assume document language ISO 639 code LL by default")
101     p.add_option ("--no-color",
102                   action='store_false',
103                   default=True,
104                   dest="color",
105                   help="do not print ANSI-colored output")
106     p.add_option ("--verbose",
107                   action='store_true',
108                   default=False,
109                   dest="verbose",
110                   help="print details, including executed shell commands")
111     p.add_option ('-t',
112                   action='store',
113                   default='HEAD',
114                   dest="touch_committishes",
115                   metavar='COMMIT',
116                   help='[EXPERIMENTAL] update committishes of all files that were up to \
117 date at commit COMMIT')
118     p.add_option ('-u', "--update",
119                   action='store_true',
120                   default=False,
121                   dest='update_mode',
122                   help='call $EDITOR to update the translation')
123     
124     (options, files) = p.parse_args ()
125     verbose = options.verbose
126     lang = options.language
127     use_colors = options.color
128     update_mode = options.update_mode
129     touch_committishes = options.touch_committishes
130     
131     return files
132
133 def main ():
134     global update_mode, text_editor, touch_committishes, current_revision
135
136     files = do_options ()
137     if 'EDITOR' in os.environ:
138         text_editor = os.environ['EDITOR']
139     else:
140         update_mode = False
141     buildlib.verbose = verbose
142     (parsed_revision, error) = buildlib.read_pipe (vc_revision_parse % touch_committishes)
143     if error:
144         sys.stderr.write ('warning: %s' % error)
145     else:
146         touch_committishes = parsed_revision.strip ()
147     current_revision = buildlib.read_pipe (vc_revision_parse % 'HEAD')[0]
148
149     for i in files:
150         do_file (i, langdefs.LANGDICT.keys ())
151
152 if __name__ == '__main__':
153     main ()