CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / tools / l10n / l10n.py
blob23deeeda0761ba22388452a6d43888195fef9ce1
1 #!/bin/env python
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
16 # The Original Code is l10n test automation.
18 # The Initial Developer of the Original Code is
19 # Mozilla Foundation
20 # Portions created by the Initial Developer are Copyright (C) 2007
21 # the Initial Developer. All Rights Reserved.
23 # Contributor(s):
24 # Axel Hecht <l10n@mozilla.com>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
40 import logging
41 from optparse import OptionParser
42 import os
43 import os.path
44 import re
45 from subprocess import Popen, PIPE
46 from shutil import copy2
47 import sys
49 def walk(base):
50 for root, dirs, files in os.walk(base):
51 try:
52 dirs.remove('CVS')
53 except ValueError:
54 pass
55 yield (os.path.normpath(root[len(base)+1:]), files)
57 def createLocalization(source, dest, apps, exceptions = {}):
58 '''
59 Creates a new localization.
61 @type source: string
62 @param source: path to the mozilla sources to use
64 @type dest: string
65 @param dest: path to the localization to create or update
67 @type apps: array of strings
68 @param apps: the applications for which to create or update the
69 localization
71 @type exceptions: mapping
72 @param exceptions: stuff to ignore
73 '''
75 assert os.path.isdir(source), "source directory is not a directory"
76 clientmk = os.path.join(source,'client.mk')
77 assert os.path.isfile(clientmk), "client.mk missing"
79 if not apps or not len(apps):
80 apps=['browser']
82 if not os.path.isdir(dest):
83 os.makedirs(dest)
85 assert os.path.isdir(dest), "target should be a directory"
87 # get the directories to iterate over
88 dirs = set()
89 cmd = ['make', '-f', clientmk] + \
90 ['echo-variable-LOCALES_' + app for app in apps]
91 p = Popen(cmd, stdout = PIPE)
92 for ln in p.stdout.readlines():
93 dirs.update(ln.strip().split())
94 dirs = sorted(list(dirs))
96 for d in dirs:
97 assert os.path.isdir(os.path.join(source, d)), \
98 "expecting source directory %s" % d
100 for d in dirs:
101 logging.debug('processing %s' % d)
102 if d in exceptions and exceptions[d] == 'all':
103 continue
105 basepath = os.path.join(source, d, 'locales', 'en-US')
107 ign_mod = {}
108 if d in exceptions:
109 ign_mod = exceptions[d]
110 logging.debug('using exceptions: %s' % str(ign_mod))
112 l10nbase = os.path.join(dest, d)
113 if not os.path.isdir(l10nbase):
114 os.makedirs(l10nbase)
116 for root, files in walk(basepath):
117 ignore = None
118 if root in ign_mod:
119 if ign_mod[root] == '.':
120 continue
121 ignore = re.compile(ign_mod[root])
122 l10npath = os.path.join(l10nbase, root)
123 if not os.path.isdir(l10npath):
124 os.mkdir(l10npath)
126 for f in files:
127 if ignore and ignore.search(f):
128 # ignoring some files
129 continue
130 if not os.path.exists(os.path.join(l10npath,f)):
131 copy2(os.path.join(basepath, root, f), l10npath)
133 if __name__ == '__main__':
134 p = OptionParser()
135 p.add_option('--source', default = '.',
136 help='Mozilla sources')
137 p.add_option('--dest', default = '../l10n',
138 help='Localization target directory')
139 p.add_option('--app', action="append",
140 help='Create localization for this application ' + \
141 '(multiple applications allowed, default: browser)')
142 p.add_option('-v', '--verbose', action="store_true", default=False,
143 help='report debugging information')
144 (opts, args) = p.parse_args()
145 if opts.verbose:
146 logging.basicConfig(level=logging.DEBUG)
147 assert len(args) == 1, "language code expected"
149 # hardcoding exceptions, work for both trunk and 1.8 branch
150 exceptions = {'browser':
151 {'searchplugins': '\\.xml$'},
152 'extensions/spellcheck': 'all'}
153 createLocalization(opts.source, os.path.join(opts.dest, args[0]),
154 opts.app, exceptions=exceptions)