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