SVN maintenance.
[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
29 GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
30 CURRENT_DIR = os.path.dirname(__file__)
31 SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
32 DOMAIN = "blender"
33
34 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
35 FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")
36
37
38 def main():
39     cmd = (GETTEXT_XGETTEXT_EXECUTABLE,
40            "--files-from=%s" % os.path.join(SOURCE_DIR, "po", "POTFILES.in"),
41            "--keyword=_",
42            "--keyword=N_",
43            "--directory=%s" % SOURCE_DIR,
44            "--output=%s" % os.path.join(SOURCE_DIR, "po", "%s.pot" % DOMAIN),
45            "--from-code=utf-8",
46            )
47
48     print(" ".join(cmd))
49     process = subprocess.Popen(cmd)
50     process.wait()
51
52     def stripeol(s):
53         return s.rstrip("\n\r")
54
55     pot_messages = {}
56     reading_message = False
57     message = ""
58     with open(FILE_NAME_POT, 'r') as handle:
59         while True:
60             line = handle.readline()
61
62             if not line:
63                 break
64
65             line = stripeol(line)
66             if line.startswith("msgid"):
67                 reading_message = True
68                 message = line[7:-1]
69             elif line.startswith("msgstr"):
70                 reading_message = False
71                 pot_messages[message] = True
72             elif reading_message:
73                 message += line[1:-1]
74
75     # add messages collected automatically from RNA
76     with open(FILE_NAME_POT, "a") as pot_handle:
77         with open(FILE_NAME_MESSAGES, 'r') as handle:
78             while True:
79                 line = handle.readline()
80
81                 if not line:
82                     break
83
84                 line = stripeol(line)
85                 line = line.replace("\\", "\\\\")
86                 line = line.replace("\"", "\\\"")
87
88                 if not pot_messages.get(line):
89                     pot_handle.write("\n#: Automatically collected from RNA\n")
90                     pot_handle.write("msgid \"%s\"\n" % (line))
91                     pot_handle.write("msgstr \"\"\n")
92
93
94 if __name__ == "__main__":
95     print("\n\n *** Running %r *** \n" % __file__)
96     main()