]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/check_translation.py
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / buildscripts / check_translation.py
index 9760133a8bfd9e7411472d7efcf51592a7aaecb6..090b1fbe8a73811af2dd98c0d2c74f2078ca6aa3 100644 (file)
@@ -1,65 +1,73 @@
-#!@PYTHON@
+#!/usr/bin/env python
 
 import __main__
 import optparse
-import gettext
 import os
-import re
 import sys
 
+import langdefs
+import buildlib
+
 verbose = 0
-lang = 'site'
+use_colors = False
+lang = 'C'
 C = lang
 
-def dir_lang (file, lang):
-    path_components = [lang] + file.split ('/')[1:]
+def dir_lang (file, lang, lang_dir_index):
+    path_components = file.split ('/')
+    path_components[lang_dir_index] = lang
     return os.path.join (*path_components)
 
-##     Translation of GIT Commit: <hash>
-REVISION_RE = re.compile ('.*GIT [Cc]ommittish: ([a-f0-9]+)', re.DOTALL)
-CVS_DIFF = 'git diff %(revision)s HEAD -- %(original)s | cat'
-
-def check_file (original, translated):
-    s = open (translated).read ()
-    m = REVISION_RE.match (s)
-    if not m:
-        sys.stderr.write ('error: ' + translated + \
-                          ": no 'GIT committish: <hash>' found.\nPlease check " + \
-                          'the whole file against the original in English, then ' + \
-                          'fill in HEAD committish in the header.\n')
-        sys.exit (1)
-    revision = m.group (1)
-
-    c = CVS_DIFF % vars ()
-    if verbose:
-        sys.stderr.write ('running: ' + c)
-    os.system (c)
-
-def do_file (file_name, langdefs):
+def do_file (file_name, lang_codes, buildlib):
     if verbose:
         sys.stderr.write ('%s...\n' % file_name)
-    file_lang = file_name.split ('/')[0]
-    if file_lang in langdefs.LANGDICT.keys():
-        check_lang = file_lang
+    split_file_name = file_name.split ('/')
+    d1, d2 = split_file_name[0:2]
+    if d1 in lang_codes:
+        check_lang = d1
+        lang_dir_index = 0
+    elif d2 in lang_codes:
+        check_lang = d2
+        lang_dir_index = 1
     else:
         check_lang = lang
     if check_lang == C:
-        raise 'cannot determine language for: ' + file_name
+        raise Exception ('cannot determine language for ' + file_name)
     
-    original = dir_lang (file_name, '')
-    translated = dir_lang (file_name, check_lang)
-    check_file (original, translated)
+    original = dir_lang (file_name, '', lang_dir_index)
+    translated_contents = open (file_name).read ()
+    (diff_string, error) \
+        = buildlib.check_translated_doc (original,
+                                         file_name,
+                                         translated_contents,
+                                         color=use_colors and not update_mode)
+
+    if error:
+        sys.stderr.write ('warning: %s: %s' % (file_name, error))
+
+    if update_mode:
+        if error or len (diff_string) >= os.path.getsize (original):
+            buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
+        elif diff_string:
+            diff_file = original + '.diff'
+            f = open (diff_file, 'w')
+            f.write (diff_string)
+            f.close ()
+            buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
+            os.remove (diff_file)
+    else:
+        sys.stdout.write (diff_string)
 
 def usage ():
     sys.stdout.write (r'''
 Usage:
-check-translation [--language=LANG] [--verbose] BUILDSCRIPT-DIR FILE...
+check-translation [--language=LANG] [--verbose] [--update] FILE...
 
 This script is licensed under the GNU GPL.
 ''')
 
 def do_options ():
-    global lang, verbose
+    global lang, verbose, update_mode, use_colors
 
     p = optparse.OptionParser (usage="check-translation [--language=LANG] [--verbose] FILE...",
                                description="This script is licensed under the GNU GPL.")
@@ -67,26 +75,43 @@ def do_options ():
                   action='store',
                   default='site',
                   dest="language")
+    p.add_option ("--no-color",
+                  action='store_false',
+                  default=True,
+                  dest="color",
+                  help="do not print ANSI-cooured output")
     p.add_option ("--verbose",
                   action='store_true',
                   default=False,
                   dest="verbose",
-                  help="the GIT directory to merge.")
+                  help="print details, including executed shell commands")
+    p.add_option ('-u', "--update",
+                  action='store_true',
+                  default=False,
+                  dest='update_mode',
+                  help='call $EDITOR to update the translation')
     
     (options, files) = p.parse_args ()
     verbose = options.verbose
     lang = options.language
+    use_colors = options.color
+    update_mode = options.update_mode
     
-    return (files[0], files[1:])
+    return files
 
 def main ():
-    import_path, files = do_options ()
+    global update_mode, text_editor
+
+    files = do_options ()
+    if 'EDITOR' in os.environ:
+        text_editor = os.environ['EDITOR']
+    else:
+        update_mode = False
     
-    sys.path.append (import_path)
-    import langdefs
+    buildlib.verbose = verbose
 
     for i in files:
-        do_file (i, langdefs)
+        do_file (i, langdefs.LANGDICT.keys (), buildlib)
 
 if __name__ == '__main__':
     main ()