]> git.donarmstrong.com Git - lilypond.git/commitdiff
Improve translation checking and share duplicate buildscripts code
authorJohn Mandereau <john.mandereau@gmail.com>
Sun, 23 Mar 2008 14:32:46 +0000 (15:32 +0100)
committerJohn Mandereau <john.mandereau@gmail.com>
Sun, 23 Mar 2008 14:32:46 +0000 (15:32 +0100)
- share translations git diff code in buildscripts/buildlib.py

- check_translation.py: add --update option to call EDITOR
  automatically

- improve check_translation user interface (Documentation/GNUmakefile
  targets)

Documentation/GNUmakefile
buildscripts/buildlib.py [new file with mode: 0644]
buildscripts/check_translation.py
buildscripts/translations-status.py

index 8f7228053ec2bc593c38b2a0408839f28c8f567f..f43e344b6d055cdf2e12d83cef86d308c2da8fb2 100644 (file)
@@ -49,8 +49,7 @@ new-lang:
        cp po/lilypond-doc.pot po/$(ISOLANG).po
        @echo "***  Please add a language definition for $(ISOLANG) in buildscripts/langdefs.py  ***"
 
-check-translation:
-       find $(ISOLANG)/user/ -maxdepth 1 -name '*.*te??' | xargs $(PYTHON) $(buildscript-dir)/check_translation.py $(buildscript-dir) $(ISOLANG)/index.html.in
+CHECKED_FILES = $(ISOLANG)/index.html.in $(shell find $(ISOLANG)/user/ -maxdepth 1 -name '*.*te??')
 
 TELY_FILES = $(call src-wildcard,$(ISOLANG)/user/*.tely)
 skeleton-update:
@@ -61,6 +60,12 @@ snippet-update:
        $(PYTHON) $(buildscript-dir)/update-snippets.py user $(ISOLANG)/user '*.itely'
 endif
 
+check-translation:
+       $(PYTHON) $(buildscript-dir)/check_translation.py $(buildscript-dir) $(CHECKED_FILES)
+
+update-translation:
+       $(PYTHON) $(buildscript-dir)/check_translation.py --update $(buildscript-dir) $(CHECKED_FILES)
+
 translation-status:
        make -C po out=www messages
        $(PYTHON) $(buildscript-dir)/translations-status.py $(buildscript-dir) po/out-www
diff --git a/buildscripts/buildlib.py b/buildscripts/buildlib.py
new file mode 100644 (file)
index 0000000..11dbf96
--- /dev/null
@@ -0,0 +1,41 @@
+#!@PYTHON@
+
+import subprocess
+import re
+
+verbose = False
+
+def read_pipe (command):
+    child = subprocess.Popen (command,
+                              stdout = subprocess.PIPE,
+                              stderr = subprocess.PIPE,
+                              shell = True)
+    (output, error) = child.communicate ()
+    code = str (child.wait ())
+    if not child.stdout or child.stdout.close ():
+        print "pipe failed: %(command)s" % locals ()
+    if code != '0':
+        error = code + ' ' + error
+    return (output, error)
+
+revision_re = re.compile ('GIT [Cc]ommittish: ([a-f0-9]+)')
+vc_diff_cmd = 'git diff %(color_flag)s %(revision)s HEAD -- %(original)s | cat'
+
+def check_translated_doc (original, translated_contents, color=False):
+    m = revision_re.search (translated_contents)
+    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)
+
+    if color:
+        color_flag = '--color'
+    else:
+        color_flag = '--no-color'
+    c = vc_diff_cmd % vars ()
+    if verbose:
+        sys.stderr.write ('running: ' + c)
+    return read_pipe (c)
index 55a384ee99b9c120f8e9884d9e76a6117eb48e5c..6952b2c34fcf53bdb661c3f30ddec43db19b95fc 100644 (file)
@@ -2,9 +2,7 @@
 
 import __main__
 import optparse
-import gettext
 import os
-import re
 import sys
 
 verbose = 0
@@ -16,27 +14,7 @@ def dir_lang (file, lang, lang_dir_index):
     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]+)')
-CVS_DIFF = 'git diff %(revision)s HEAD -- %(original)s | cat'
-
-def check_file (original, translated):
-    s = open (translated).read ()
-    m = REVISION_RE.search (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, lang_codes):
+def do_file (file_name, lang_codes, buildlib):
     if verbose:
         sys.stderr.write ('%s...\n' % file_name)
     split_file_name = file_name.split ('/')
@@ -53,19 +31,35 @@ def do_file (file_name, lang_codes):
         raise Exception ('cannot determine language for ' + file_name)
     
     original = dir_lang (file_name, '', lang_dir_index)
-    translated = file_name
-    check_file (original, translated)
+    translated_contents = open (file_name).read ()
+    (diff_string, error) = buildlib.check_translated_doc (original, translated_contents, color=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] BUILDSCRIPT-DIR FILE...
 
 This script is licensed under the GNU GPL.
 ''')
 
 def do_options ():
-    global lang, verbose
+    global lang, verbose, update_mode
 
     p = optparse.OptionParser (usage="check-translation [--language=LANG] [--verbose] FILE...",
                                description="This script is licensed under the GNU GPL.")
@@ -78,21 +72,35 @@ def do_options ():
                   default=False,
                   dest="verbose",
                   help="the GIT directory to merge.")
+    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
+    update_mode = options.update_mode
     
     return (files[0], files[1:])
 
 def main ():
+    global update_mode, text_editor
+
     import_path, files = do_options ()
+    if 'EDITOR' in os.environ.keys ():
+        text_editor = os.environ['EDITOR']
+    else:
+        update_mode = False
     
     sys.path.append (import_path)
     import langdefs
+    import buildlib
+    buildlib.verbose = verbose
 
     for i in files:
-        do_file (i, langdefs.LANGDICT.keys())
+        do_file (i, langdefs.LANGDICT.keys(), buildlib)
 
 if __name__ == '__main__':
     main ()
index b9f62a706f84e0964649259062b53051f850a54c..7a8d8bb225330c1232fb5239160c8e1ce65802c9 100644 (file)
@@ -18,7 +18,6 @@ import re
 import string
 import os
 import gettext
-import subprocess
 
 def progress (str):
     sys.stderr.write (str + '\n')
@@ -32,6 +31,7 @@ _doc = lambda s: s
 
 sys.path.append (buildscript_dir)
 import langdefs
+import buildlib
 
 # load gettext messages catalogs
 translation = {}
@@ -39,18 +39,6 @@ for l in langdefs.LANGUAGES:
     if l.enabled and l.code != 'en':
         translation[l.code] = gettext.translation('lilypond-doc', localedir, [l.code]).gettext
 
-def read_pipe (command):
-    child = subprocess.Popen (command,
-                              stdout = subprocess.PIPE,
-                              stderr = subprocess.PIPE,
-                              shell = True)
-    (output, error) = child.communicate ()
-    code = str (child.wait ())
-    if not child.stdout or child.stdout.close ():
-        print "pipe failed: %(command)s" % locals ()
-    if code != '0':
-        error = code + ' ' + error
-    return (output, error)
 
 comments_re = re.compile (r'^@ignore\n(.|\n)*?\n@end ignore$|@c .*?$', re.M)
 space_re = re.compile (r'\s+', re.M)
@@ -59,7 +47,6 @@ node_re = re.compile ('^@node .*?$', re.M)
 title_re = re.compile ('^@(top|chapter|(?:sub){0,2}section|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?) (.*?)$', re.M)
 include_re = re.compile ('^@include (.*?)$', re.M)
 
-committish_re = re.compile ('GIT [Cc]ommittish: ([a-f0-9]+)')
 translators_re = re.compile (r'^@c\s+Translators\s*:\s*(.*?)$', re.M | re.I)
 checkers_re = re.compile (r'^@c\s+Translation\s*checkers\s*:\s*(.*?)$', re.M | re.I)
 status_re = re.compile (r'^@c\s+Translation\s*status\s*:\s*(.*?)$', re.M | re.I)
@@ -67,8 +54,6 @@ post_gdp_re = re.compile ('post.GDP', re.I)
 untranslated_node_str = 'UNTRANSLATED NODE: IGNORE ME'
 skeleton_str = '-- SKELETON FILE --'
 
-diff_cmd = 'git diff --no-color %(committish)s HEAD -- %(original)s | cat'
-
 format_table = {
     'not translated': {'color':'d0f0f8', 'short':_doc ('no'), 'abbr':'NT',
                        'long':_doc ('not translated')},
@@ -239,14 +224,7 @@ class TranslatedTelyDocument (TelyDocument):
         self.translation_percentage = 100 * translation_word_count / master_total_word_count
 
         ## calculate how much the file is outdated
-        m = committish_re.search (self.contents)
-        if not m:
-            sys.stderr.write ('error: ' + filename + \
-                                  ": 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)
-        (diff_string, error) = read_pipe (diff_cmd % {'committish':m.group (1), 'original':masterdocument.filename})
+        (diff_string, error) = buildlib.check_translated_doc (masterdocument.filename, self.contents)
         if error:
             sys.stderr.write ('warning: %s: %s' % (self.filename, error))
             self.uptodate_percentage = None
@@ -410,7 +388,7 @@ def update_category_word_counts_sub (m):
 
 progress ("Reading documents...")
 
-tely_files = read_pipe ("find -maxdepth 2 -name '*.tely'")[0].splitlines ()
+tely_files = buildlib.read_pipe ("find -maxdepth 2 -name '*.tely'")[0].splitlines ()
 master_docs = [MasterTelyDocument (os.path.normpath (filename)) for filename in tely_files]
 master_docs = [doc for doc in master_docs if doc.translations]
 
@@ -423,7 +401,7 @@ main_status_page = open ('translations.template.html.in').read ()
 
 progress ("Generating status pages...")
 
-date_time = read_pipe ('LANG= date -u')[0]
+date_time = buildlib.read_pipe ('LANG= date -u')[0]
 
 main_status_html = ' <p><i>Last updated %s</i></p>\n' % date_time
 main_status_html += '\n'.join ([doc.html_status () for doc in master_docs])