~extension-hackers/globalmenu-extension/3.5

« back to all changes in this revision

Viewing changes to toolkit/mozapps/installer/windows/nsis/preprocess-locale.py

  • Committer: Chris Coulson
  • Date: 2011-08-05 17:37:02 UTC
  • Revision ID: chrisccoulson@ubuntu.com-20110805173702-n11ykbt0tdp5u07q
Refresh build system from FIREFOX_6_0b5_BUILD1

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# --convert-utf8-utf16le.
13
13
 
14
14
from codecs import BOM_UTF16_LE
15
 
from os.path import join
 
15
from os.path import join, isfile
16
16
import sys
17
 
 
18
 
def preprocess_locale(argv):
19
 
    """
20
 
    Validates command line arguments and displays usage if necessary
21
 
    """
22
 
    if len(argv) < 1 or (argv[0] != '--convert-utf8-utf16le' and argv[0] != '--preprocess-locale'):
23
 
        sys.stderr.write("""
24
 
preprocess-locale.py
25
 
 
26
 
Commands:
27
 
 --convert-utf8-utf16le - preprocesses installer locale properties files and
28
 
                          creates a basic NSIS nlf file
29
 
 --preprocess-locale    - converts a UTF-8 file to a new UTF-16LE file
30
 
 
31
 
use "preprocess-locale.py <command>" to see the usage for each command
32
 
""")
33
 
        sys.exit(1)
34
 
 
35
 
    if argv[0] == '--convert-utf8-utf16le':
36
 
        if len(argv) != 3:
37
 
            sys.stderr.write("""
38
 
Converts a UTF-8 file to UTF-16LE
39
 
 
40
 
Usage: preprocess-locale.py --convert-utf8-utf16le <src> <dest>
41
 
 
42
 
Arguments:
43
 
 <src> \tthe path to the UTF-8 source file to convert
44
 
 <dest>\tthe path to the UTF-16LE destination file to create
45
 
""")
46
 
            sys.exit(1)
47
 
        convert_utf8_utf16le(argv[1], argv[2])
48
 
 
49
 
    if argv[0] == '--preprocess-locale':
50
 
        if len(argv) != 5:
51
 
            sys.stderr.write("""
52
 
Preprocesses the installer localized properties files into the format
53
 
required by NSIS and creates a basic NSIS nlf file.
54
 
 
55
 
Usage: preprocess-locale.py --preprocess-locale <src> <locale> <code> <dest>
56
 
 
57
 
Arguments:
58
 
 <src>   \tthe path to top source directory for the toolkit source
59
 
 <locale>\tthe path to the installer's locale files
60
 
 <code>  \tthe locale code
61
 
 <dest>  \tthe path to the destination directory
62
 
""")
63
 
            sys.exit(1)
64
 
        preprocess_locale_files(argv[1], argv[2], argv[3], argv[4])
 
17
from optparse import OptionParser
65
18
 
66
19
def open_utf16le_file(path):
67
20
    """
108
61
    fp.close()
109
62
    return output
110
63
 
111
 
def preprocess_locale_files(moz_dir, locale_dir, ab_cd, config_dir):
 
64
def lookup(path, l10ndirs):
 
65
    for d in l10ndirs:
 
66
        if isfile(join(d, path)):
 
67
            return join(d, path)
 
68
    return join(l10ndirs[-1], path)
 
69
 
 
70
def preprocess_locale_files(moz_dir, ab_cd, config_dir, l10ndirs):
112
71
    """
113
72
    Preprocesses the installer localized properties files into the format
114
73
    required by NSIS and creates a basic NSIS nlf file.
115
74
 
116
75
    Parameters:
117
76
    moz_dir    - the path to top source directory for the toolkit source
118
 
    locale_dir - the path to the installer's locale files
119
77
    ab_cd      - the locale code
120
78
    config_dir - the path to the destination directory
 
79
    l10ndirs  - list of paths to search for installer locale files
121
80
    """
122
81
 
123
82
    # Set the language ID to 0 to make this locale the default locale. An
157
116
 
158
117
    # Create the main NSIS language file
159
118
    fp = open_utf16le_file(join(config_dir, "overrideLocale.nsh"))
160
 
    locale_strings = get_locale_strings(join(locale_dir, "override.properties"),
 
119
    locale_strings = get_locale_strings(lookup("override.properties", l10ndirs),
161
120
                                        "LangString ^", " " + lang_id + " ", False)
162
121
    fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
163
122
    fp.close()
170
129
!insertmacro MOZ_MUI_LANGUAGEFILE_BEGIN \"baseLocale\"
171
130
!define MUI_LANGNAME \"baseLocale\"
172
131
""" % (lang_id)).encode("utf-16-le"))
173
 
    locale_strings = get_locale_strings(join(locale_dir, "mui.properties"),
 
132
    locale_strings = get_locale_strings(lookup("mui.properties", l10ndirs),
174
133
                                        "!define ", " ", True)
175
134
    fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
176
135
    fp.write(u"!insertmacro MOZ_MUI_LANGUAGEFILE_END\n".encode("utf-16-le"))
178
137
 
179
138
    # Create the custom language file for our custom strings
180
139
    fp = open_utf16le_file(join(config_dir, "customLocale.nsh"))
181
 
    locale_strings = get_locale_strings(join(locale_dir, "custom.properties"),
 
140
    locale_strings = get_locale_strings(lookup("custom.properties", l10ndirs),
182
141
                        "LangString ", " " + lang_id + " ", True)
183
142
    fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
184
143
    fp.close()
198
157
    out_fp.close()
199
158
 
200
159
if __name__ == '__main__':
201
 
    sys.exit(preprocess_locale(sys.argv[1:]))
 
160
    usage = """usage: %prog command <args>
 
161
 
 
162
Commands:
 
163
 --convert-utf8-utf16le - preprocesses installer locale properties files and
 
164
                          creates a basic NSIS nlf file
 
165
 --preprocess-locale    - Preprocesses the installer localized properties files
 
166
                          into the format required by NSIS and creates a basic
 
167
                          NSIS nlf file.
 
168
 
 
169
preprocess-locale.py --preprocess-locale <src> <locale> <code> <dest>
 
170
 
 
171
Arguments:
 
172
 <src>   \tthe path to top source directory for the toolkit source
 
173
 <locale>\tthe path to the installer's locale files
 
174
 <code>  \tthe locale code
 
175
 <dest>  \tthe path to the destination directory
 
176
 
 
177
 
 
178
preprocess-locale.py --convert-utf8-utf16le <src> <dest>
 
179
 
 
180
Arguments:
 
181
 <src> \tthe path to the UTF-8 source file to convert
 
182
 <dest>\tthe path to the UTF-16LE destination file to create
 
183
"""
 
184
    p = OptionParser(usage=usage)
 
185
    p.add_option("--preprocess-locale", action="store_true", default=False,
 
186
                 dest='preprocess')
 
187
    p.add_option("--l10n-dir", action="append", default=[],
 
188
                 dest="l10n_dirs",
 
189
                 help="Add directory to lookup for locale files")
 
190
    p.add_option("--convert-utf8-utf16le", action="store_true", default=False,
 
191
                 dest='convert')
 
192
 
 
193
    options, args = p.parse_args()
 
194
 
 
195
    if ((not (options.preprocess or options.convert)) or
 
196
        (options.preprocess and options.convert)):
 
197
        p.error("You need to specify either --preprocess-locale or --convert-utf-utf16le")
 
198
 
 
199
    if options.preprocess:
 
200
        if len(args) not in (3,4):
 
201
            p.error("--preprocess-locale needs all of <src> <locale> <code> <dest>")
 
202
        pargs = args[:]
 
203
        if len(args) == 4:
 
204
            l10n_dirs = [args[1]]
 
205
            del pargs[1]
 
206
        else:
 
207
            if not options.l10n_dirs:
 
208
                p.error("--preprocess-locale needs either <locale> or --l10ndir")
 
209
            l10n_dirs = options.l10n_dirs
 
210
 
 
211
        pargs.append(l10n_dirs)
 
212
        preprocess_locale_files(*pargs)
 
213
    else:
 
214
        if len(args) != 2:
 
215
            p.error("--convert-utf8-utf16le needs both of <src> <dest>")
 
216
        convert_utf8_utf16le(*args)