|
1 #!/usr/bin/env python |
|
2 |
|
3 import optparse |
|
4 import os |
|
5 import sys |
|
6 |
|
7 try: |
|
8 set |
|
9 except NameError: |
|
10 from sets import Set as set # For Python 2.3 |
|
11 |
|
12 |
|
13 def compile_messages(locale=None): |
|
14 basedirs = (os.path.join('conf', 'locale'), 'locale') |
|
15 if os.environ.get('DJANGO_SETTINGS_MODULE'): |
|
16 from django.conf import settings |
|
17 basedirs += settings.LOCALE_PATHS |
|
18 |
|
19 # Gather existing directories. |
|
20 basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs))) |
|
21 |
|
22 if not basedirs: |
|
23 print "This script should be run from the Django SVN tree or your project or app tree, or with the settings module specified." |
|
24 sys.exit(1) |
|
25 |
|
26 for basedir in basedirs: |
|
27 if locale: |
|
28 basedir = os.path.join(basedir, locale, 'LC_MESSAGES') |
|
29 compile_messages_in_dir(basedir) |
|
30 |
|
31 def compile_messages_in_dir(basedir): |
|
32 for dirpath, dirnames, filenames in os.walk(basedir): |
|
33 for f in filenames: |
|
34 if f.endswith('.po'): |
|
35 sys.stderr.write('processing file %s in %s\n' % (f, dirpath)) |
|
36 pf = os.path.splitext(os.path.join(dirpath, f))[0] |
|
37 # Store the names of the .mo and .po files in an environment |
|
38 # variable, rather than doing a string replacement into the |
|
39 # command, so that we can take advantage of shell quoting, to |
|
40 # quote any malicious characters/escaping. |
|
41 # See http://cyberelk.net/tim/articles/cmdline/ar01s02.html |
|
42 os.environ['djangocompilemo'] = pf + '.mo' |
|
43 os.environ['djangocompilepo'] = pf + '.po' |
|
44 if sys.platform == 'win32': # Different shell-variable syntax |
|
45 cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"' |
|
46 else: |
|
47 cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"' |
|
48 os.system(cmd) |
|
49 |
|
50 def main(): |
|
51 parser = optparse.OptionParser() |
|
52 parser.add_option('-l', '--locale', dest='locale', |
|
53 help="The locale to process. Default is to process all.") |
|
54 parser.add_option('--settings', |
|
55 help='Python path to settings module, e.g. "myproject.settings". If provided, all LOCALE_PATHS will be processed. If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be checked as well.') |
|
56 options, args = parser.parse_args() |
|
57 if len(args): |
|
58 parser.error("This program takes no arguments") |
|
59 if options.settings: |
|
60 os.environ['DJANGO_SETTINGS_MODULE'] = options.settings |
|
61 compile_messages(options.locale) |
|
62 |
|
63 if __name__ == "__main__": |
|
64 main() |