]> git.donarmstrong.com Git - lilypond.git/blob - python/auxiliar/buildlib.py
Docs: handle file renames in translation checking
[lilypond.git] / python / auxiliar / buildlib.py
1 #!@PYTHON@
2
3 import subprocess
4 import re
5 import sys
6
7 verbose = False
8
9 def read_pipe (command):
10     child = subprocess.Popen (command,
11                               stdout = subprocess.PIPE,
12                               stderr = subprocess.PIPE,
13                               shell = True)
14     (output, error) = child.communicate ()
15     code = str (child.wait ())
16     if not child.stdout or child.stdout.close ():
17         print "pipe failed: %(command)s" % locals ()
18     if code != '0':
19         error = code + ' ' + error
20     return (output, error)
21
22 ### Renamed files map to ensure continuity of file history
23 ## Map of new_name: old_name
24 renames_map = {
25     'application.tely': 'user/lilypond-program.tely',
26     'notation.tely': 'user/lilypond.tely',
27     'learning.tely': 'user/lilypond-learning.tely',
28     'changes.tely': 'topdocs/NEWS.tely',
29 }
30
31 manuals_subdirectories_re = \
32     re.compile ('(application|essay|learning|notation)/')
33
34 def add_old_name (file_path):
35     for new_path in renames_map:
36         if file_path.endswith (new_path):
37             old_file_path = file_path.replace (new_path,
38                                                renames_map[new_path])
39             break
40     else:
41         if file_path.endswith ('macros.itexi'):
42             old_file_path = file_path.replace ('macros.itexi',
43                                                'user/macros.itexi')
44         elif file_path.endswith ('.itely'):
45             old_file_path = manuals_subdirectories_re.sub ('user/',
46                                                            file_path)
47         elif 'snippets/' in file_path:
48             old_file_path = file_path.replace ('snippets/',
49                                                '../input/lsr/')
50         else:
51             return file_path
52     return file_path + ' ' + old_file_path
53
54 revision_re = re.compile ('GIT [Cc]ommittish:\s+([a-f0-9]+)')
55 vc_diff_cmd = 'git diff -M %(color_flag)s %(revision)s \
56 %(upper_revision)s -- %(original_with_old_name)s | cat'
57 no_committish_fatal_error = """error: %s: no 'GIT committish: <hash>' found.
58 Please check the whole file against the original in English, then
59 fill in HEAD committish in the header.
60 """
61
62 def check_translated_doc (original, translated_file, translated_contents,
63                           color=False, upper_revision='HEAD'):
64     m = revision_re.search (translated_contents)
65     if not m:
66         sys.stderr.write (no_committish_fatal_error % translated_file)
67         sys.exit (1)
68     revision = m.group (1)
69
70     if color:
71         color_flag = '--color --color-words'
72     else:
73         color_flag = '--no-color'
74     original_with_old_name = add_old_name (original)
75     c = vc_diff_cmd % vars ()
76     if verbose:
77         sys.stderr.write ('running: ' + c)
78     return read_pipe (c)