LEFT | RIGHT |
(no file at all) | |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # update-snippets.py | 2 # update-snippets.py |
3 | 3 |
4 # USAGE: update-snippets.py REFERENCE-DIR TARGET-DIR FILES | 4 # USAGE: update-snippets.py REFERENCE-DIR TARGET-DIR FILES |
5 # | 5 # |
6 # update ly snippets in TARGET-DIR/FILES with snippets from REFERENCE-DIR/FILES | 6 # update ly snippets in TARGET-DIR/FILES with snippets from REFERENCE-DIR/FILES |
7 # | 7 # |
8 # More precisely, each existing FILE in TARGET-DIR is matched to the FILE in | 8 # More precisely, each existing FILE in TARGET-DIR is matched to the FILE in |
9 # REFERENCE-DIR (it the latter does not exist, a warning is given). | 9 # REFERENCE-DIR (it the latter does not exist, a warning is given). |
10 # | 10 # |
11 # Shell wildcards expansion is performed on FILES. | 11 # Shell wildcards expansion is performed on FILES. |
12 # This script currently supports Texinfo format. | 12 # This script currently supports Texinfo format. |
13 # Ly snippets preceded with a line containing '@c KEEP LY' in TARGET-DIR/FILES | 13 # Ly snippets preceded with a line containing '@c KEEP LY' in TARGET-DIR/FILES |
14 # will not be updated. | 14 # will not be updated. |
15 # An error occurs if REFERENCE-DIR/FILE and TARGET-DIR/FILE do not have the | 15 # An error occurs if REFERENCE-DIR/FILE and TARGET-DIR/FILE do not have the |
16 # same snippets count. | 16 # same snippets count. |
17 | 17 |
18 import sys | 18 import sys |
19 import os | 19 import os |
20 import glob | 20 import glob |
21 import re | 21 import re |
| 22 from functools import reduce |
22 | 23 |
23 print "update-snippets.py" | 24 print("update-snippets.py") |
24 | 25 |
25 comment_re = re.compile (r'(?<!@)(@c(?:omment)? .*?\n|^@ignore\n.*?\n@end ignore
\n)', re.M | re.S) | 26 comment_re = re.compile (r'(?<!@)(@c(?:omment)? .*?\n|^@ignore\n.*?\n@end ignore
\n)', re.M | re.S) |
26 snippet_re = re.compile (r'^(@lilypond(?:file)?(?:\[.*?\])?\s*\{.+?\}|@lilypond(
?:\[.*?\])?(?:.|\n)+?@end lilypond)', re.M) | 27 snippet_re = re.compile (r'^(@lilypond(?:file)?(?:\[.*?\])?\s*\{.+?\}|@lilypond(
?:\[.*?\])?(?:.|\n)+?@end lilypond)', re.M) |
27 | 28 |
28 | 29 |
29 def snippet_split (l): | 30 def snippet_split (l): |
30 r = [] | 31 r = [] |
31 for s in [s for s in l if s]: | 32 for s in [s for s in l if s]: |
32 if s.startswith ('@c ') or s.startswith ('@ignore\n') or s.startswith ('
@comment '): | 33 if s.startswith ('@c ') or s.startswith ('@ignore\n') or s.startswith ('
@comment '): |
33 r.append(s) | 34 r.append(s) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 if j > 0 and (not target_source[j-1].startswith ('@c KEEP LY'))
and target_source[j] != ref_source[k]: | 94 if j > 0 and (not target_source[j-1].startswith ('@c KEEP LY'))
and target_source[j] != ref_source[k]: |
94 target_source[j] = ref_source[k] | 95 target_source[j] = ref_source[k] |
95 c += 1 | 96 c += 1 |
96 changed_snippets_count += 1 | 97 changed_snippets_count += 1 |
97 f = open (file, 'w') | 98 f = open (file, 'w') |
98 f.write (''.join (target_source)) | 99 f.write (''.join (target_source)) |
99 sys.stderr.write ('%s: %d/%d snippets updated\n' % (file, c, snippet_cou
nt)) | 100 sys.stderr.write ('%s: %d/%d snippets updated\n' % (file, c, snippet_cou
nt)) |
100 | 101 |
101 sys.stderr.write ('\nTotal: %d snippets, %d updated snippets.\n' % (total_snippe
t_count, changed_snippets_count)) | 102 sys.stderr.write ('\nTotal: %d snippets, %d updated snippets.\n' % (total_snippe
t_count, changed_snippets_count)) |
102 sys.exit (exit_code) | 103 sys.exit (exit_code) |
LEFT | RIGHT |