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