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