Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / tools / l10n / l10n.py
blobd7d7b21526469378ffa9f1b056f6aac4fdb227ac
1 #!/bin/env python
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 import logging
8 from optparse import OptionParser
9 import os
10 import os.path
11 import re
12 from subprocess import Popen, PIPE
13 from shutil import copy2
14 import sys
16 def walk(base):
17 for root, dirs, files in os.walk(base):
18 try:
19 dirs.remove('CVS')
20 except ValueError:
21 pass
22 yield (os.path.normpath(root[len(base)+1:]), files)
24 def createLocalization(source, dest, apps, exceptions = {}):
25 '''
26 Creates a new localization.
28 @type source: string
29 @param source: path to the mozilla sources to use
31 @type dest: string
32 @param dest: path to the localization to create or update
34 @type apps: array of strings
35 @param apps: the applications for which to create or update the
36 localization
38 @type exceptions: mapping
39 @param exceptions: stuff to ignore
40 '''
42 assert os.path.isdir(source), "source directory is not a directory"
43 clientmk = os.path.join(source,'client.mk')
44 assert os.path.isfile(clientmk), "client.mk missing"
46 if not apps or not len(apps):
47 apps=['browser']
49 if not os.path.isdir(dest):
50 os.makedirs(dest)
52 assert os.path.isdir(dest), "target should be a directory"
54 # get the directories to iterate over
55 dirs = set()
56 cmd = ['make', '-f', clientmk] + \
57 ['echo-variable-LOCALES_' + app for app in apps]
58 p = Popen(cmd, stdout = PIPE)
59 for ln in p.stdout.readlines():
60 dirs.update(ln.strip().split())
61 dirs = sorted(list(dirs))
63 for d in dirs:
64 assert os.path.isdir(os.path.join(source, d)), \
65 "expecting source directory %s" % d
67 for d in dirs:
68 logging.debug('processing %s' % d)
69 if d in exceptions and exceptions[d] == 'all':
70 continue
72 basepath = os.path.join(source, d, 'locales', 'en-US')
74 ign_mod = {}
75 if d in exceptions:
76 ign_mod = exceptions[d]
77 logging.debug('using exceptions: %s' % str(ign_mod))
79 l10nbase = os.path.join(dest, d)
80 if not os.path.isdir(l10nbase):
81 os.makedirs(l10nbase)
83 for root, files in walk(basepath):
84 ignore = None
85 if root in ign_mod:
86 if ign_mod[root] == '.':
87 continue
88 ignore = re.compile(ign_mod[root])
89 l10npath = os.path.join(l10nbase, root)
90 if not os.path.isdir(l10npath):
91 os.mkdir(l10npath)
93 for f in files:
94 if ignore and ignore.search(f):
95 # ignoring some files
96 continue
97 if not os.path.exists(os.path.join(l10npath,f)):
98 copy2(os.path.join(basepath, root, f), l10npath)
100 if __name__ == '__main__':
101 p = OptionParser()
102 p.add_option('--source', default = '.',
103 help='Mozilla sources')
104 p.add_option('--dest', default = '../l10n',
105 help='Localization target directory')
106 p.add_option('--app', action="append",
107 help='Create localization for this application ' + \
108 '(multiple applications allowed, default: browser)')
109 p.add_option('-v', '--verbose', action="store_true", default=False,
110 help='report debugging information')
111 (opts, args) = p.parse_args()
112 if opts.verbose:
113 logging.basicConfig(level=logging.DEBUG)
114 assert len(args) == 1, "language code expected"
116 # hardcoding exceptions, work for both trunk and 1.8 branch
117 exceptions = {'browser':
118 {'searchplugins': '\\.xml$'},
119 'extensions/spellcheck': 'all'}
120 createLocalization(opts.source, os.path.join(opts.dest, args[0]),
121 opts.app, exceptions=exceptions)