| 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 |
|
|---|
| 18 | L10N tool which takes a list of .po in conflicted state and revert
|
|---|
| 19 | ignorable changes.
|
|---|
| 20 |
|
|---|
| 21 | It resolve the conflicts for which "theirs" changes consist solely of
|
|---|
| 22 | line number changes, by reverting to the working copy content.
|
|---|
| 23 |
|
|---|
| 24 | This makes it easier to merge translation .po files across branches.
|
|---|
| 25 |
|
|---|
| 26 | """
|
|---|
| 27 |
|
|---|
| 28 | import re
|
|---|
| 29 |
|
|---|
| 30 | ignore_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 |
|
|---|
| 38 | HEADERS = '''
|
|---|
| 39 | Project-Id-Version Report-Msgid-Bugs-To POT-Creation-Date PO-Revision-Date
|
|---|
| 40 | Last-Translator Language-Team Plural-Forms MIME-Version Content-Type
|
|---|
| 41 | Content-Transfer-Encoding Generated-By
|
|---|
| 42 | '''.split()
|
|---|
| 43 |
|
|---|
| 44 | po_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 |
|
|---|
| 53 | def 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 |
|
|---|
| 66 | if __name__ == '__main__':
|
|---|
| 67 | import sys
|
|---|
| 68 | for path in sys.argv[1:]:
|
|---|
| 69 | sanitize_file(path)
|
|---|