##// END OF EJS Templates
scripts/i18n: let 'normalized-merge' leave 3-way conflict markers in output file...
Mads Kiilerich -
r8188:f8314738 default
parent child Browse files
Show More
@@ -1,137 +1,140 b''
1 1 #!/usr/bin/env python3
2 2
3 3 # -*- coding: utf-8 -*-
4 4 # This program is free software: you can redistribute it and/or modify
5 5 # it under the terms of the GNU General Public License as published by
6 6 # the Free Software Foundation, either version 3 of the License, or
7 7 # (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16
17 17 import os
18 18 import shutil
19 19 import sys
20 20
21 21 import click
22 22
23 23 import i18n_utils
24 24
25 25
26 26 """
27 27 Tool for maintenance of .po and .pot files
28 28
29 29 Normally, the i18n-related files contain for each translatable string a
30 30 reference to all the source code locations where this string is found. This
31 31 meta data is useful for translators to assess how strings are used, but is not
32 32 relevant for normal development nor for running Kallithea. Such meta data, or
33 33 derived data like kallithea.pot, will inherently be outdated, and create
34 34 unnecessary churn and repository growth, making it harder to spot actual and
35 35 important changes.
36 36 """
37 37
38 38 @click.group()
39 39 @click.option('--debug/--no-debug', default=False)
40 40 def cli(debug):
41 41 if (debug):
42 42 i18n_utils.do_debug = True
43 43 pass
44 44
45 45 @cli.command()
46 46 @click.argument('po_files', nargs=-1)
47 47 @click.option('--merge-pot-file', default=None)
48 48 @click.option('--strip/--no-strip', default=False)
49 49 def normalize_po_files(po_files, merge_pot_file, strip):
50 50 """Normalize the specified .po and .pot files.
51 51
52 52 By default, only actual translations and essential headers will be
53 53 preserved, just as we want it in the main branches with minimal noise.
54 54
55 55 If a .pot file is specified, the po files will instead be updated by
56 56 running GNU msgmerge with this .pot file, thus updating source code
57 57 references and preserving comments and outdated translations.
58 58 """
59 59 for po_file in po_files:
60 60 i18n_utils._normalize_po_file(po_file, merge_pot_file=merge_pot_file, strip=strip)
61 61
62 62 @cli.command()
63 63 @click.argument('local')
64 64 @click.argument('base')
65 65 @click.argument('other')
66 66 @click.argument('output')
67 67 @click.option('--merge-pot-file', default=None)
68 68 @click.option('--strip/--no-strip', default=False)
69 69 def normalized_merge(local, base, other, output, merge_pot_file, strip):
70 70 """Merge tool for use with 'hg merge/rebase/graft --tool'
71 71
72 72 i18n files are partially manually editored original source of content, and
73 73 partially automatically generated and updated. That create a lot of churn
74 74 and often cause a lot of merge conflicts.
75 75
76 76 To avoid that, this merge tool wrapper will normalize .po content before
77 77 running the merge tool.
78 78
79 79 By default, only actual translations and essential headers will be
80 80 preserved, just as we want it in the main branches with minimal noise.
81 81
82 82 If a .pot file is specified, the po files will instead be updated by
83 83 running GNU msgmerge with this .pot file, thus updating source code
84 84 references and preserving comments and outdated translations.
85 85
86 86 Add the following to your user or repository-specific .hgrc file to use it:
87 87 [merge-tools]
88 88 i18n.executable = /path/to/scripts/i18n
89 89 i18n.args = normalized-merge $local $base $other $output
90 90
91 91 and then invoke merge/rebase/graft with the additional argument '--tool i18n'.
92 92 """
93 93 from mercurial import (
94 94 context,
95 95 simplemerge,
96 96 ui as uimod,
97 97 )
98 98
99 99 print('i18n normalized-merge: merging file %s' % output)
100 100
101 101 i18n_utils._normalize_po_file(local, merge_pot_file=merge_pot_file, strip=strip)
102 102 i18n_utils._normalize_po_file(base, merge_pot_file=merge_pot_file, strip=strip)
103 103 i18n_utils._normalize_po_file(other, merge_pot_file=merge_pot_file, strip=strip)
104 104 i18n_utils._normalize_po_file(output, merge_pot_file=merge_pot_file, strip=strip)
105 105
106 106 # simplemerge will write markers to 'local' if it fails, keep a copy without markers
107 107 localkeep = local + '.keep'
108 108 shutil.copyfile(local, localkeep)
109 109
110 110 ret = simplemerge.simplemerge(uimod.ui.load(),
111 111 context.arbitraryfilectx(local.encode('utf-8')),
112 112 context.arbitraryfilectx(base.encode('utf-8')),
113 context.arbitraryfilectx(other.encode('utf-8'))
113 context.arbitraryfilectx(other.encode('utf-8')),
114 label=[b'local', b'other', b'base'],
115 mode='merge',
114 116 )
115 shutil.copyfile(local, output) # simplemerge wrote to local
117 shutil.copyfile(local, output) # simplemerge wrote to local - either resolved or with conflict markers
116 118 if ret:
119 shutil.copyfile(localkeep, local)
117 120 basekeep = base + '.keep'
118 121 otherkeep = other + '.keep'
119 122 shutil.copyfile(base, basekeep)
120 123 shutil.copyfile(other, otherkeep)
121 sys.stderr.write("Error: simple merge failed. Run a merge tool manually to resolve conflicts, then use 'hg resolve -m'.\n")
124 sys.stderr.write("Error: simple merge failed and %s is left with conflict markers. Resolve the conflics , then use 'hg resolve -m'.\n" % output)
122 125 sys.stderr.write('Resolve with e.g.: kdiff3 %s %s %s -o %s\n' % (basekeep, localkeep, otherkeep, output))
123 126 sys.exit(ret)
124 127
125 128 os.remove(localkeep)
126 129
127 130 @cli.command()
128 131 @click.argument('file1')
129 132 @click.argument('file2')
130 133 @click.option('--merge-pot-file', default=None)
131 134 @click.option('--strip/--no-strip', default=False)
132 135 def normalized_diff(file1, file2, merge_pot_file, strip):
133 136 """Compare two files while transparently normalizing them."""
134 137 sys.exit(i18n_utils._normalized_diff(file1, file2, merge_pot_file=merge_pot_file, strip=strip))
135 138
136 139 if __name__ == '__main__':
137 140 cli()
General Comments 0
You need to be logged in to leave comments. Login now