]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/check_translation.py
Merge master into nested-bookparts
[lilypond.git] / buildscripts / check_translation.py
1 #!/usr/bin/env python
2
3 import __main__
4 import optparse
5 import os
6 import sys
7
8 import langdefs
9 import buildlib
10
11 verbose = 0
12 use_colors = False
13 lang = 'C'
14 C = lang
15
16 def dir_lang (file, lang, lang_dir_index):
17     path_components = file.split ('/')
18     path_components[lang_dir_index] = lang
19     return os.path.join (*path_components)
20
21 def do_file (file_name, lang_codes, buildlib):
22     if verbose:
23         sys.stderr.write ('%s...\n' % file_name)
24     split_file_name = file_name.split ('/')
25     d1, d2 = split_file_name[0:2]
26     if d1 in lang_codes:
27         check_lang = d1
28         lang_dir_index = 0
29     elif d2 in lang_codes:
30         check_lang = d2
31         lang_dir_index = 1
32     else:
33         check_lang = lang
34     if check_lang == C:
35         raise Exception ('cannot determine language for ' + file_name)
36     
37     original = dir_lang (file_name, '', lang_dir_index)
38     translated_contents = open (file_name).read ()
39     (diff_string, error) = buildlib.check_translated_doc (original, translated_contents, color=use_colors and not update_mode)
40
41     if error:
42         sys.stderr.write ('warning: %s: %s' % (file_name, error))
43
44     if update_mode:
45         if error or len (diff_string) >= os.path.getsize (original):
46             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
47         elif diff_string:
48             diff_file = original + '.diff'
49             f = open (diff_file, 'w')
50             f.write (diff_string)
51             f.close ()
52             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
53             os.remove (diff_file)
54     else:
55         sys.stdout.write (diff_string)
56
57 def usage ():
58     sys.stdout.write (r'''
59 Usage:
60 check-translation [--language=LANG] [--verbose] [--update] FILE...
61
62 This script is licensed under the GNU GPL.
63 ''')
64
65 def do_options ():
66     global lang, verbose, update_mode, use_colors
67
68     p = optparse.OptionParser (usage="check-translation [--language=LANG] [--verbose] FILE...",
69                                description="This script is licensed under the GNU GPL.")
70     p.add_option ("--language",
71                   action='store',
72                   default='site',
73                   dest="language")
74     p.add_option ("--no-color",
75                   action='store_false',
76                   default=True,
77                   dest="color",
78                   help="do not print ANSI-cooured output")
79     p.add_option ("--verbose",
80                   action='store_true',
81                   default=False,
82                   dest="verbose",
83                   help="print details, including executed shell commands")
84     p.add_option ('-u', "--update",
85                   action='store_true',
86                   default=False,
87                   dest='update_mode',
88                   help='call $EDITOR to update the translation')
89     
90     (options, files) = p.parse_args ()
91     verbose = options.verbose
92     lang = options.language
93     use_colors = options.color
94     update_mode = options.update_mode
95     
96     return files
97
98 def main ():
99     global update_mode, text_editor
100
101     files = do_options ()
102     if 'EDITOR' in os.environ:
103         text_editor = os.environ['EDITOR']
104     else:
105         update_mode = False
106     
107     buildlib.verbose = verbose
108
109     for i in files:
110         do_file (i, langdefs.LANGDICT.keys (), buildlib)
111
112 if __name__ == '__main__':
113     main ()