making PO/POT/... update scripts py3.2 compatible + making it almost win compatible
[blender.git] / po / update_pot.py
1 #!/usr/bin/env python
2
3 # $Id$
4 # ***** BEGIN GPL LICENSE BLOCK *****
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #
20 # ***** END GPL LICENSE BLOCK *****
21
22 # <pep8 compliant>
23
24 # update the pot file according the POTFILES.in
25
26 import subprocess
27 import os
28 from codecs import open
29
30 GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
31 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
32 SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
33 DOMAIN = "blender"
34
35 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
36 FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")
37
38
39 def main():
40     cmd = (GETTEXT_XGETTEXT_EXECUTABLE,
41            "--files-from=%s" % os.path.join(SOURCE_DIR, "po", "POTFILES.in"),
42            "--keyword=_",
43            "--keyword=N_",
44            "--directory=%s" % SOURCE_DIR,
45            "--output=%s" % os.path.join(SOURCE_DIR, "po", "%s.pot" % DOMAIN),
46            "--from-code=utf-8",
47            )
48
49     print(" ".join(cmd))
50     process = subprocess.Popen(cmd)
51     process.wait()
52
53     def stripeol(s):
54         return s.rstrip("\n\r")
55
56     pot_messages = {}
57     reading_message = False
58     message = ""
59     with open(FILE_NAME_POT, 'r', "utf-8") as handle:
60         while True:
61             line = handle.readline()
62
63             if not line:
64                 break
65
66             line = stripeol(line)
67             if line.startswith("msgid"):
68                 reading_message = True
69                 message = line[7:-1]
70             elif line.startswith("msgstr"):
71                 reading_message = False
72                 pot_messages[message] = True
73             elif reading_message:
74                 message += line[1:-1]
75
76     # add messages collected automatically from RNA
77     with open(FILE_NAME_POT, "a", "utf-8") as pot_handle:
78         with open(FILE_NAME_MESSAGES, 'r', "utf-8") as handle:
79             while True:
80                 line = handle.readline()
81
82                 if not line:
83                     break
84
85                 line = stripeol(line)
86                 line = line.replace("\\", "\\\\")
87                 line = line.replace("\"", "\\\"")
88
89                 if not pot_messages.get(line):
90                     pot_handle.write("\n#: Automatically collected from RNA\n")
91                     pot_handle.write("msgid \"%s\"\n" % (line))
92                     pot_handle.write("msgstr \"\"\n")
93
94
95 if __name__ == "__main__":
96     print("\n\n *** Running %r *** \n" % __file__)
97     main()