rename module to something less generic.
[blender.git] / release / scripts / modules / bl_i18n_utils / import_po_from_branches.py
1 #!/usr/bin/python3
2
3 # ***** BEGIN GPL LICENSE BLOCK *****
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19 # ***** END GPL LICENSE BLOCK *****
20
21 # <pep8 compliant>
22
23 # Import in trunk/po all po from branches translated above the given threshold.
24
25 import os
26 import shutil
27 import sys
28 import subprocess
29 from codecs import open
30
31 import settings
32 import utils
33 import rtl_preprocess
34
35
36 TRUNK_PO_DIR = settings.TRUNK_PO_DIR
37 BRANCHES_DIR = settings.BRANCHES_DIR
38
39 RTL_PREPROCESS_FILE = settings.RTL_PREPROCESS_FILE
40
41 PY3 = settings.PYTHON3_EXEC
42
43
44 def main():
45     import argparse
46     parser = argparse.ArgumentParser(description="Import advanced enough po’s " \
47                                                  "from branches to trunk.")
48     parser.add_argument('-t', '--threshold', type=int,
49                         help="Import threshold, as a percentage.")
50     parser.add_argument('-s', '--strict', action="store_true",
51                         help="Raise an error if a po is broken.")
52     parser.add_argument('langs', metavar='ISO_code', nargs='*',
53                         help="Restrict processed languages to those.")
54     args = parser.parse_args()
55
56
57     ret = 0
58
59     threshold = float(settings.IMPORT_MIN_LEVEL)/100.0
60     if args.threshold is not None:
61         threshold = float(args.threshold)/100.0
62
63     for lang in os.listdir(BRANCHES_DIR):
64         if args.langs and lang not in args.langs:
65             continue
66         po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
67         if os.path.exists(po):
68             po_is_rtl = os.path.join(BRANCHES_DIR, lang, RTL_PREPROCESS_FILE)
69             msgs, state, stats = utils.parse_messages(po)
70             tot_msgs = stats["tot_msg"]
71             trans_msgs = stats["trans_msg"]
72             lvl = 0.0
73             if tot_msgs:
74                 lvl = float(trans_msgs)/float(tot_msgs)
75             if lvl > threshold:
76                 if state["is_broken"] and args.strict:
77                     print("{:<10}: {:>6.1%} done, but BROKEN, skipped." \
78                           "".format(lang, lvl))
79                     ret = 1
80                 else:
81                     if os.path.exists(po_is_rtl):
82                         out_po = os.path.join(TRUNK_PO_DIR,
83                                               ".".join((lang, "po")))
84                         out_raw_po = os.path.join(TRUNK_PO_DIR,
85                                                   "_".join((lang, "raw.po")))
86                         keys = []
87                         trans = []
88                         for k, m in msgs.items():
89                             keys.append(k)
90                             trans.append("".join(m["msgstr_lines"]))
91                         trans = rtl_preprocess.log2vis(trans)
92                         for k, t in zip(keys, trans):
93                             # Mono-line for now...
94                             msgs[k]["msgstr_lines"] = [t]
95                         utils.write_messages(out_po, msgs, state["comm_msg"],
96                                              state["fuzzy_msg"])
97                         # Also copies org po!
98                         shutil.copy(po, out_raw_po)
99                         print("{:<10}: {:>6.1%} done, enough translated " \
100                               "messages, processed and copied to trunk." \
101                               "".format(lang, lvl))
102                     else:
103                         shutil.copy(po, TRUNK_PO_DIR)
104                         print("{:<10}: {:>6.1%} done, enough translated " \
105                               "messages, copied to trunk.".format(lang, lvl))
106             else:
107                 if state["is_broken"] and args.strict:
108                     print("{:<10}: {:>6.1%} done, BROKEN and not enough " \
109                           "translated messages, skipped".format(lang, lvl))
110                     ret = 1
111                 else:
112                     print("{:<10}: {:>6.1%} done, not enough translated " \
113                           "messages, skipped.".format(lang, lvl))
114     return ret
115
116
117 if __name__ == "__main__":
118     print("\n\n *** Running {} *** \n".format(__file__))
119     sys.exit(main())