##// END OF EJS Templates
scripts/i18n: add command 'normalized-merge' for use with Mercurial's 'merge-tool' option...
Thomas De Schampheleire -
r8185:46e78e58 default
parent child Browse files
Show More
@@ -1,61 +1,118 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 import os
18 import shutil
17 19 import sys
18 20
19 21 import click
20 22
21 23 import i18n_utils
22 24
23 25
24 26 """
25 27 Tool for maintenance of .po and .pot files
26 28
27 29 Normally, the i18n-related files contain for each translatable string a
28 30 reference to all the source code locations where this string is found. This
29 31 meta data is useful for translators to assess how strings are used, but is not
30 32 relevant for normal development nor for running Kallithea. Such meta data, or
31 33 derived data like kallithea.pot, will inherently be outdated, and create
32 34 unnecessary churn and repository growth, making it harder to spot actual and
33 35 important changes.
34 36 """
35 37
36 38 @click.group()
37 39 @click.option('--debug/--no-debug', default=False)
38 40 def cli(debug):
39 41 if (debug):
40 42 i18n_utils.do_debug = True
41 43 pass
42 44
43 45 @cli.command()
44 46 @click.argument('po_files', nargs=-1)
45 47 def normalize_po_files(po_files):
46 48 """Normalize the specified .po and .pot files.
47 49
48 50 Only actual translations and essential headers will be preserved.
49 51 """
50 52 for po_file in po_files:
51 53 i18n_utils._normalize_po_file(po_file, strip=True)
52 54
53 55 @cli.command()
56 @click.argument('local')
57 @click.argument('base')
58 @click.argument('other')
59 @click.argument('output')
60 def normalized_merge(local, base, other, output):
61 """Merge tool for use with 'hg merge/rebase/graft --tool'
62
63 Merging i18n files with a standard merge tool could yield merge conflicts
64 when one side is normalized and the other is not. In such cases, it may be
65 better to first normalize all sides, then proceed with a standard merge.
66 This command does exactly that, and can be used as 'merge-tool' in
67 Mercurial commands like merge, rebase and graft.
68
69 Add the following to your user or repository-specific .hgrc file to use it:
70 [merge-tools]
71 i18n.executable = /path/to/scripts/i18n
72 i18n.args = normalized-merge $local $base $other $output
73
74 and then invoke merge/rebase/graft with the additional argument '--tool i18n'.
75 """
76 from mercurial import (
77 context,
78 simplemerge,
79 ui as uimod,
80 )
81
82 print('i18n normalized-merge: merging file %s' % output)
83
84 i18n_utils._normalize_po_file(local, strip=True)
85 i18n_utils._normalize_po_file(base, strip=True)
86 i18n_utils._normalize_po_file(other, strip=True)
87 i18n_utils._normalize_po_file(output, strip=True)
88
89 # simplemerge will write markers to 'local' if it fails, keep a copy without markers
90 localkeep = local + '.keep'
91 shutil.copyfile(local, localkeep)
92
93 ret = simplemerge.simplemerge(uimod.ui.load(),
94 context.arbitraryfilectx(local.encode('utf-8')),
95 context.arbitraryfilectx(base.encode('utf-8')),
96 context.arbitraryfilectx(other.encode('utf-8'))
97 )
98 shutil.copyfile(local, output) # simplemerge wrote to local
99 if ret:
100 basekeep = base + '.keep'
101 otherkeep = other + '.keep'
102 shutil.copyfile(base, basekeep)
103 shutil.copyfile(other, otherkeep)
104 sys.stderr.write("Error: simple merge failed. Run a merge tool manually to resolve conflicts, then use 'hg resolve -m'.\n")
105 sys.stderr.write('Resolve with e.g.: kdiff3 %s %s %s -o %s\n' % (basekeep, localkeep, otherkeep, output))
106 sys.exit(ret)
107
108 os.remove(localkeep)
109
110 @cli.command()
54 111 @click.argument('file1')
55 112 @click.argument('file2')
56 113 def normalized_diff(file1, file2):
57 114 """Compare two files while transparently normalizing them."""
58 115 sys.exit(i18n_utils._normalized_diff(file1, file2, strip=True))
59 116
60 117 if __name__ == '__main__':
61 118 cli()
General Comments 0
You need to be logged in to leave comments. Login now