Back to index

enigmail  1.4.3
preprocess-locale.py
Go to the documentation of this file.
00001 # preprocess-locale.py
00002 # Any copyright is dedicated to the Public Domain.
00003 # http://creativecommons.org/publicdomain/zero/1.0/
00004 
00005 # preprocess-locale.py provides two functions depending on the arguments passed
00006 # to it when invoked.
00007 #
00008 # Preprocesses installer locale properties files and creates a basic NSIS nlf
00009 # file when invoked with --preprocess-locale.
00010 # 
00011 # Converts a UTF-8 file to a new UTF-16LE file when invoked with
00012 # --convert-utf8-utf16le.
00013 
00014 from codecs import BOM_UTF16_LE
00015 from os.path import join, isfile
00016 import sys
00017 from optparse import OptionParser
00018 
00019 def open_utf16le_file(path):
00020     """
00021     Returns an opened file object with a a UTF-16LE byte order mark.
00022     """
00023     fp = open(path, "w+b")
00024     fp.write(BOM_UTF16_LE)
00025     return fp
00026 
00027 def get_locale_strings(path, prefix, middle, add_cr):
00028     """
00029     Returns a string created by converting an installer locale properties file
00030     into the format required by NSIS locale files.
00031 
00032     Parameters:
00033     path   - the path to the installer locale properties file to preprocess
00034     prefix - a string to prefix each line with
00035     middle - a string to insert between the name and value for each line
00036     add_cr - boolean for whether to add an NSIS carriage return before NSIS
00037              linefeeds when there isn't one already
00038     """
00039     output = ""
00040     fp = open(path, "r")
00041     for line in fp:
00042         line = line.strip()
00043         if line == "" or line[0] == "#":
00044             continue
00045 
00046         name, value = line.split("=", 1)
00047         value = value.strip() # trim whitespace from the start and end
00048         if value[-1] == "\"" and value[0] == "\"":
00049             value = value[1:-1] # remove " from the start and end
00050 
00051         if add_cr:
00052             value = value.replace("\\n", "\\r\\n") # prefix $\n with $\r
00053             value = value.replace("\\r\\r", "\\r") # replace $\r$\r with $\r
00054 
00055         value = value.replace("\"", "$\\\"") # prefix " with $\
00056         value = value.replace("\\r", "$\\r") # prefix \r with $
00057         value = value.replace("\\n", "$\\n") # prefix \n with $
00058         value = value.replace("\\t", "$\\t") # prefix \t with $
00059 
00060         output += prefix + name.strip() + middle + " \"" + value + "\"\n"
00061     fp.close()
00062     return output
00063 
00064 def lookup(path, l10ndirs):
00065     for d in l10ndirs:
00066         if isfile(join(d, path)):
00067             return join(d, path)
00068     return join(l10ndirs[-1], path)
00069 
00070 def preprocess_locale_files(moz_dir, ab_cd, config_dir, l10ndirs):
00071     """
00072     Preprocesses the installer localized properties files into the format
00073     required by NSIS and creates a basic NSIS nlf file.
00074 
00075     Parameters:
00076     moz_dir    - the path to top source directory for the toolkit source
00077     ab_cd      - the locale code
00078     config_dir - the path to the destination directory
00079     l10ndirs  - list of paths to search for installer locale files
00080     """
00081 
00082     # Set the language ID to 0 to make this locale the default locale. An
00083     # actual ID will need to be used to create a multi-language installer
00084     # (e.g. for CD distributions, etc.).
00085     lang_id = "0"
00086     rtl = "-"
00087 
00088     # Check whether the locale is right to left from locales.nsi.
00089     fp = open(join(moz_dir, "toolkit/mozapps/installer/windows/nsis/locales.nsi"), "r")
00090     for line in fp:
00091         line = line.strip()
00092         if line == "!define " + ab_cd + "_rtl":
00093             rtl = "RTL"
00094             break
00095 
00096     fp.close()
00097 
00098     # Create the main NSIS language file with RTL for right to left locales
00099     # along with the default codepage, font name, and font size represented
00100     # by the '-' character.
00101     fp = open_utf16le_file(join(config_dir, "baseLocale.nlf"))
00102     fp.write((u"""# Header, don't edit
00103 NLF v6
00104 # Start editing here
00105 # Language ID
00106 %s
00107 # Font and size - dash (-) means default
00108 -
00109 -
00110 # Codepage - dash (-) means ANSI code page
00111 -
00112 # RTL - anything else than RTL means LTR
00113 %s
00114 """ % (lang_id, rtl)).encode("utf-16-le"))
00115     fp.close()
00116 
00117     # Create the main NSIS language file
00118     fp = open_utf16le_file(join(config_dir, "overrideLocale.nsh"))
00119     locale_strings = get_locale_strings(lookup("override.properties", l10ndirs),
00120                                         "LangString ^", " " + lang_id + " ", False)
00121     fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
00122     fp.close()
00123 
00124     # Create the Modern User Interface language file
00125     fp = open_utf16le_file(join(config_dir, "baseLocale.nsh"))
00126     fp.write((u""";NSIS Modern User Interface - Language File
00127 ;Compatible with Modern UI 1.68
00128 ;Language: baseLocale (%s)
00129 !insertmacro MOZ_MUI_LANGUAGEFILE_BEGIN \"baseLocale\"
00130 !define MUI_LANGNAME \"baseLocale\"
00131 """ % (lang_id)).encode("utf-16-le"))
00132     locale_strings = get_locale_strings(lookup("mui.properties", l10ndirs),
00133                                         "!define ", " ", True)
00134     fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
00135     fp.write(u"!insertmacro MOZ_MUI_LANGUAGEFILE_END\n".encode("utf-16-le"))
00136     fp.close()
00137 
00138     # Create the custom language file for our custom strings
00139     fp = open_utf16le_file(join(config_dir, "customLocale.nsh"))
00140     locale_strings = get_locale_strings(lookup("custom.properties", l10ndirs),
00141                         "LangString ", " " + lang_id + " ", True)
00142     fp.write(unicode(locale_strings, "utf-8").encode("utf-16-le"))
00143     fp.close()
00144 
00145 def convert_utf8_utf16le(in_file_path, out_file_path):
00146     """
00147     Converts a UTF-8 file to a new UTF-16LE file
00148 
00149     Arguments:
00150     in_file_path  - the path to the UTF-8 source file to convert
00151     out_file_path - the path to the UTF-16LE destination file to create
00152     """
00153     in_fp = open(in_file_path, "r")
00154     out_fp = open_utf16le_file(out_file_path)
00155     out_fp.write(unicode(in_fp.read(), "utf-8").encode("utf-16-le"))
00156     in_fp.close()
00157     out_fp.close()
00158 
00159 if __name__ == '__main__':
00160     usage = """usage: %prog command <args>
00161 
00162 Commands:
00163  --convert-utf8-utf16le - preprocesses installer locale properties files and
00164                           creates a basic NSIS nlf file
00165  --preprocess-locale    - Preprocesses the installer localized properties files
00166                           into the format required by NSIS and creates a basic
00167                           NSIS nlf file.
00168 
00169 preprocess-locale.py --preprocess-locale <src> <locale> <code> <dest>
00170 
00171 Arguments:
00172  <src>   \tthe path to top source directory for the toolkit source
00173  <locale>\tthe path to the installer's locale files
00174  <code>  \tthe locale code
00175  <dest>  \tthe path to the destination directory
00176 
00177 
00178 preprocess-locale.py --convert-utf8-utf16le <src> <dest>
00179 
00180 Arguments:
00181  <src> \tthe path to the UTF-8 source file to convert
00182  <dest>\tthe path to the UTF-16LE destination file to create
00183 """
00184     p = OptionParser(usage=usage)
00185     p.add_option("--preprocess-locale", action="store_true", default=False,
00186                  dest='preprocess')
00187     p.add_option("--l10n-dir", action="append", default=[],
00188                  dest="l10n_dirs",
00189                  help="Add directory to lookup for locale files")
00190     p.add_option("--convert-utf8-utf16le", action="store_true", default=False,
00191                  dest='convert')
00192 
00193     options, args = p.parse_args()
00194 
00195     if ((not (options.preprocess or options.convert)) or
00196         (options.preprocess and options.convert)):
00197         p.error("You need to specify either --preprocess-locale or --convert-utf-utf16le")
00198 
00199     if options.preprocess:
00200         if len(args) not in (3,4):
00201             p.error("--preprocess-locale needs all of <src> <locale> <code> <dest>")
00202         pargs = args[:]
00203         if len(args) == 4:
00204             l10n_dirs = [args[1]]
00205             del pargs[1]
00206         else:
00207             if not options.l10n_dirs:
00208                 p.error("--preprocess-locale needs either <locale> or --l10ndir")
00209             l10n_dirs = options.l10n_dirs
00210 
00211         pargs.append(l10n_dirs)
00212         preprocess_locale_files(*pargs)
00213     else:
00214         if len(args) != 2:
00215             p.error("--convert-utf8-utf16le needs both of <src> <dest>")
00216         convert_utf8_utf16le(*args)