]> git.donarmstrong.com Git - lilypond.git/blob - python/auxiliar/buildlib.py
lilypond-manuals.css: edit color scheme and some spacing
[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     'usage.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 # FIXME: Hardcoded file names!?
32 manuals_subdirectories_re = \
33     re.compile ('(usage|automated-engraving|changes|essay|extending|web|learning|notation)/')
34
35 def add_old_name (file_path):
36     for new_path in renames_map:
37         if file_path.endswith (new_path):
38             old_file_path = file_path.replace (new_path,
39                                                renames_map[new_path])
40             break
41     else:
42         if file_path.endswith ('macros.itexi'):
43             old_file_path = file_path.replace ('macros.itexi',
44                                                'user/macros.itexi')
45         elif file_path.endswith ('.itely'):
46             old_file_path = manuals_subdirectories_re.sub ('user/',
47                                                            file_path)
48         elif 'snippets/' in file_path:
49             old_file_path = file_path.replace ('snippets/',
50                                                '../input/lsr/')
51         else:
52             return file_path
53     return file_path + ' ' + old_file_path
54
55 revision_re = re.compile ('GIT [Cc]ommittish:\s+([a-f0-9]+)')
56 vc_diff_cmd = 'git diff -M %(color_flag)s %(revision)s \
57 %(upper_revision)s -- %(original_with_old_name)s | cat'
58 no_committish_fatal_error = """error: %s: no 'GIT committish: <hash>' found.
59 Please check the whole file against the original in English, then
60 fill in HEAD committish in the header.
61 """
62
63 def check_translated_doc (original, translated_file, translated_contents,
64                           color=False, upper_revision='HEAD'):
65     m = revision_re.search (translated_contents)
66     if not m:
67         sys.stderr.write (no_committish_fatal_error % translated_file)
68         sys.exit (1)
69     revision = m.group (1)
70     if revision == '0':
71         return '', 0
72
73     if color:
74         color_flag = '--color --color-words'
75     else:
76         color_flag = '--no-color'
77     original_with_old_name = add_old_name (original)
78     c = vc_diff_cmd % vars ()
79     if verbose:
80         sys.stderr.write ('running: ' + c)
81     return read_pipe (c)