Edgewall Software

source: trunk/contrib/l10n_revert_lineno_conflicts.py

Last change on this file was 17657, checked in by Jun Omae, 8 months ago

1.5.4dev: update copyright year to 2023 (refs #13402)

[skip ci]

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2013-2023 Edgewall Software
5# Copyright (C) 2013 Christian Boos <cboos@edgewall.org>
6# All rights reserved.
7#
8# This software is licensed as described in the file COPYING, which
9# you should have received as part of this distribution. The terms
10# are also available at https://trac.edgewall.org/wiki/TracLicense.
11#
12# This software consists of voluntary contributions made by many
13# individuals. For the exact contribution history, see the revision
14# history and logs, available at https://trac.edgewall.org/.
15
16"""
17
18L10N tool which takes a list of .po in conflicted state and revert
19ignorable changes.
20
21It resolve the conflicts for which "theirs" changes consist solely of
22line number changes, by reverting to the working copy content.
23
24This makes it easier to merge translation .po files across branches.
25
26"""
27
28import re
29
30ignore_lineno_re = re.compile(r'''
31 <<<< .* \n
32 ( (?: [^=] .* \n )+ ) # \1 == "working copy"
33 ==== .* \n
34 ( (?: \# .* \n )+ ) # \2 == comment only for "theirs"
35 >>>> .* \n
36 ''', re.MULTILINE | re.VERBOSE)
37
38HEADERS = '''
39Project-Id-Version Report-Msgid-Bugs-To POT-Creation-Date PO-Revision-Date
40Last-Translator Language-Team Plural-Forms MIME-Version Content-Type
41Content-Transfer-Encoding Generated-By
42'''.split()
43
44po_headers_re = re.compile(r'''
45 <<<< .* \n
46 ( (?: "(?:%(header)s): \s [^"]+" \n )+ ) # \1 == "working copy"
47 ==== .* \n
48 ( (?: "(?:%(header)s): \s [^"]+" \n )+ ) # \2 == another date for "theirs"
49 >>>> .* \n
50 ''' % dict(header='|'.join(HEADERS)), re. MULTILINE | re.VERBOSE)
51
52
53def sanitize_file(path):
54 with open(path, 'r+', encoding='utf-8') as f:
55 sanitized, nsub = ignore_lineno_re.subn(r'\1', f.read())
56 sanitized, nsub2 = po_headers_re.subn(r'\1', sanitized)
57 nsub += nsub2
58 if nsub:
59 print("reverted %d ignorable changes in %s" % (nsub, path))
60 f.seek(0)
61 f.write(sanitized)
62 f.truncate()
63 else:
64 print("no ignorable changes in %s" % (path,))
65
66if __name__ == '__main__':
67 import sys
68 for path in sys.argv[1:]:
69 sanitize_file(path)
Note: See TracBrowser for help on using the repository browser.