]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/check_translation.py
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scripts / auxiliar / check_translation.py
1 #!/usr/bin/env python
2
3 # This file is part of LilyPond, the GNU music typesetter.
4 #
5 # LilyPond is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # LilyPond is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 import __main__
19 import optparse
20 import os
21 import sys
22 import re
23
24 import langdefs
25 import buildlib
26
27 verbose = 0
28 use_colors = False
29 lang = 'C'
30 C = lang
31
32 def dir_lang (file, lang, lang_dir_index):
33     path_components = file.split ('/')
34     path_components[lang_dir_index] = lang
35     return os.path.join (*path_components)
36
37 # ugh, this is complicated; where has the good old 'git rev-parse' gone?
38 vc_revision_parse = 'git log -1 --pretty=format:%%H %s'
39
40 def do_file (file_name, lang_codes):
41     if verbose:
42         sys.stderr.write ('%s...\n' % file_name)
43     split_file_name = file_name.split ('/')
44     d1, d2 = split_file_name[0:2]
45     if d1 in lang_codes:
46         check_lang = d1
47         lang_dir_index = 0
48     elif d2 in lang_codes:
49         check_lang = d2
50         lang_dir_index = 1
51     else:
52         check_lang = lang
53     if check_lang == C:
54         raise Exception ('cannot determine language for ' + file_name)
55     else:
56         if os.path.splitext (file_name)[1] == '.texidoc':
57             original = file_name.replace (os.path.join (check_lang, 'texidocs'), 'snippets', 1)
58             original = original.replace ('.texidoc', '.ly', 1)
59         else:
60             original = dir_lang (file_name, '', lang_dir_index)
61         translated_contents = open (file_name).read ()
62
63         ## experimental feature
64         if not touch_committishes in (current_revision, 'HEAD'):
65             (changes_in_original, error) = \
66                 buildlib.check_translated_doc (original,
67                                                file_name,
68                                                translated_contents,
69                                                upper_revision=touch_committishes)
70             if not error and not changes_in_original in ('', '\n'):
71                 translated_contents = \
72                     buildlib.revision_re.sub ('GIT committish: ' + current_revision,
73                                               translated_contents, 1)
74                 f = open (file_name, 'w').write (translated_contents)
75                 return
76     (diff_string, error) \
77         = buildlib.check_translated_doc (original,
78                                          file_name,
79                                          translated_contents,
80                                          color=use_colors and not update_mode)
81
82     if error:
83         sys.stderr.write ('warning: %s: %s' % (file_name, error))
84
85     if update_mode:
86         if error or len (diff_string) >= os.path.getsize (original):
87             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
88         elif diff_string:
89             diff_file = original + '.diff'
90             f = open (diff_file, 'w')
91             f.write (diff_string)
92             f.close ()
93             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
94             os.remove (diff_file)
95     else:
96         sys.stdout.write (diff_string)
97
98 def usage ():
99     sys.stdout.write (r'''
100 Usage:
101 check-translation [--language=LANG] [--verbose] [--update] [-t COMMIT] FILE...
102 ''')
103
104 def do_options ():
105     global lang, verbose, update_mode, touch_committishes, use_colors
106
107     p = optparse.OptionParser (usage=\
108 "check-translation [--language=LANG] [--verbose] [--update] [-t COMMIT] FILE...",
109                                description="")
110     p.add_option ("--language",
111                   action='store',
112                   default=C,
113                   dest="language",
114                   metavar='LL',
115                   help="assume document language ISO 639 code LL by default")
116     p.add_option ("--no-color",
117                   action='store_false',
118                   default=True,
119                   dest="color",
120                   help="do not print ANSI-colored output")
121     p.add_option ("--verbose",
122                   action='store_true',
123                   default=False,
124                   dest="verbose",
125                   help="print details, including executed shell commands")
126     p.add_option ('-t',
127                   action='store',
128                   default='HEAD',
129                   dest="touch_committishes",
130                   metavar='COMMIT',
131                   help='[EXPERIMENTAL] update committishes of all files that were up to \
132 date at commit COMMIT')
133     p.add_option ('-u', "--update",
134                   action='store_true',
135                   default=False,
136                   dest='update_mode',
137                   help='call $EDITOR to update the translation')
138     
139     (options, files) = p.parse_args ()
140     verbose = options.verbose
141     lang = options.language
142     use_colors = options.color
143     update_mode = options.update_mode
144     touch_committishes = options.touch_committishes
145     
146     return files
147
148 def main ():
149     global update_mode, text_editor, touch_committishes, current_revision
150
151     files = do_options ()
152     if 'EDITOR' in os.environ:
153         text_editor = os.environ['EDITOR']
154     else:
155         update_mode = False
156     buildlib.verbose = verbose
157     (parsed_revision, error) = buildlib.read_pipe (vc_revision_parse % touch_committishes)
158     if error:
159         sys.stderr.write ('warning: %s' % error)
160     else:
161         touch_committishes = parsed_revision.strip ()
162     current_revision = buildlib.read_pipe (vc_revision_parse % 'HEAD')[0]
163
164     for i in files:
165         do_file (i, langdefs.LANGDICT.keys ())
166
167 if __name__ == '__main__':
168     main ()