File size: 4,501 Bytes
f65fe85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!@PYTHON@
#
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 2008--2020 John Mandereau <[email protected]>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.


import subprocess
import re
import sys

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())
    (output, error) = (output.decode('utf-8'), error.decode('utf-8'))
    if code != '0':
        error = code + ' ' + error
    return (output, error)


# Renamed files map to ensure continuity of file history
# Map of new_name: old_name
# This is for handling 69f0ec47 ("Docs: reorganize documentation
# directory structure"), which removed the user/ dir and the topdocs/NEWS.tely file
renames_map = {
    'usage.tely': 'user/lilypond-program.tely',
    'notation.tely': 'user/lilypond.tely',
    'learning.tely': 'user/lilypond-learning.tely',
    'changes.tely': 'topdocs/NEWS.tely',
}

# FIXME: Hardcoded file names!?
manuals_subdirectories_re = \
    re.compile(
        '(usage|automated-engraving|changes|essay|extending|web|learning|notation)/')


def get_old_name(file_path):
    for new_path in renames_map:
        if file_path.endswith(new_path):
            old_file_path = file_path.replace(new_path,
                                              renames_map[new_path])
            break
    else:
        if file_path.endswith('macros.itexi'):
            old_file_path = file_path.replace('macros.itexi',
                                              'user/macros.itexi')
        elif file_path.endswith('.itely'):
            old_file_path = manuals_subdirectories_re.sub('user/',
                                                          file_path)
        elif 'snippets/' in file_path:
            old_file_path = file_path.replace('snippets/',
                                              '../input/lsr/')
        else:
            return file_path
    return old_file_path


def file_exists_at(revision, name):
    cmd = "git show %s:Documentation/%s" % (revision, name)
    if verbose:
        sys.stderr.write('running: %s\n' % cmd)

    child = subprocess.run(cmd,
                           stdout=subprocess.DEVNULL,
                           stderr=subprocess.DEVNULL,
                           shell=True)
    return not child.returncode


revision_re = re.compile(r'GIT [Cc]ommittish:\s+([a-f0-9]+)')

no_committish_fatal_error = """error: %s: no 'GIT committish: <hash>' found.
Please check the whole file against the original in English, then
fill in HEAD committish in the header.
"""

def check_translated_doc(original, translated_file, translated_contents,
                         color=False, upper_revision='HEAD'):
    """Returns the diff of the original relative to the last translation"""
    m = revision_re.search(translated_contents)
    if not m:
        sys.stderr.write(no_committish_fatal_error % translated_file)
        sys.exit(1)
    revision = m.group(1)
    if revision == '0':
        return '', 0

    if color:
        color_flag = '--color --color-words'
    else:
        color_flag = '--no-color'

    current_name = original

    if original.startswith("en/") and not file_exists_at(revision, original):
        # this is to handle the rename in 82d72b7 ("Merge branch doc-build")
        original = original[3:]
    if not file_exists_at(revision, original):
        original = get_old_name(original)

    c = 'git diff -M %(color_flag)s %(revision)s:Documentation/%(original)s \
%(upper_revision)s:Documentation/%(current_name)s' % vars()
    if verbose:
        sys.stderr.write('running: %s\n' % c)
    return read_pipe(c)