]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/check_translation.py
090b1fbe8a73811af2dd98c0d2c74f2078ca6aa3
[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) \
40         = buildlib.check_translated_doc (original,
41                                          file_name,
42                                          translated_contents,
43                                          color=use_colors and not update_mode)
44
45     if error:
46         sys.stderr.write ('warning: %s: %s' % (file_name, error))
47
48     if update_mode:
49         if error or len (diff_string) >= os.path.getsize (original):
50             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
51         elif diff_string:
52             diff_file = original + '.diff'
53             f = open (diff_file, 'w')
54             f.write (diff_string)
55             f.close ()
56             buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
57             os.remove (diff_file)
58     else:
59         sys.stdout.write (diff_string)
60
61 def usage ():
62     sys.stdout.write (r'''
63 Usage:
64 check-translation [--language=LANG] [--verbose] [--update] FILE...
65
66 This script is licensed under the GNU GPL.
67 ''')
68
69 def do_options ():
70     global lang, verbose, update_mode, use_colors
71
72     p = optparse.OptionParser (usage="check-translation [--language=LANG] [--verbose] FILE...",
73                                description="This script is licensed under the GNU GPL.")
74     p.add_option ("--language",
75                   action='store',
76                   default='site',
77                   dest="language")
78     p.add_option ("--no-color",
79                   action='store_false',
80                   default=True,
81                   dest="color",
82                   help="do not print ANSI-cooured output")
83     p.add_option ("--verbose",
84                   action='store_true',
85                   default=False,
86                   dest="verbose",
87                   help="print details, including executed shell commands")
88     p.add_option ('-u', "--update",
89                   action='store_true',
90                   default=False,
91                   dest='update_mode',
92                   help='call $EDITOR to update the translation')
93     
94     (options, files) = p.parse_args ()
95     verbose = options.verbose
96     lang = options.language
97     use_colors = options.color
98     update_mode = options.update_mode
99     
100     return files
101
102 def main ():
103     global update_mode, text_editor
104
105     files = do_options ()
106     if 'EDITOR' in os.environ:
107         text_editor = os.environ['EDITOR']
108     else:
109         update_mode = False
110     
111     buildlib.verbose = verbose
112
113     for i in files:
114         do_file (i, langdefs.LANGDICT.keys (), buildlib)
115
116 if __name__ == '__main__':
117     main ()