build/buildutils/extractJavaLocFiles_qt.py
changeset 23 98ccebc37403
child 25 9ac0a0a7da70
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 #
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 #!/usr/bin/python
       
    17 #
       
    18 # This script creates java specific loc zip files from S60 loc zip files
       
    19 # and also creates a "resources.jar" file where all the java loc files
       
    20 # are collected.
       
    21 #
       
    22 # Usage: extractJavaLocFiles_qt.py <input_dir> <output_dir> <resources_jar>
       
    23 #
       
    24 # Script reads S60 loc zip files from <input_dir> directory and writes
       
    25 # "resources.jar" to <output_dir> directory.
       
    26 #
       
    27 # This script uses external "jar" command for handling zip files and
       
    28 # thus is not optimized for speed.
       
    29 #
       
    30 
       
    31 import os, re, shutil, sys, glob, codecs
       
    32 import traceback
       
    33 import xml.etree.ElementTree as et
       
    34 from optparse import OptionParser
       
    35 
       
    36 # Base names for java loc files.
       
    37 JAVA_LOC_FILES = [
       
    38     'javafileconnection',
       
    39     'javainstallation',
       
    40     'javasensor',
       
    41     'javassl',
       
    42     'javausermessages',
       
    43     'javaapplicationsettings',
       
    44     'javaruntimeapplicationsettings'
       
    45     ]
       
    46 
       
    47 # Path for java loc files inside RESOURCES_FILE.
       
    48 RESOURCES_PATH = os.path.join("resources", "com", "nokia", "mj", "impl")
       
    49 
       
    50 def main():
       
    51     parser = OptionParser(
       
    52         usage = "Usage: %prog [args] <input_dir> <output_dir> <resources_jar>")
       
    53     (opts, args) = parser.parse_args()
       
    54 
       
    55     try:
       
    56         inputDir = args[0]
       
    57         outputDir = args[1]
       
    58         resourcesFilename = os.path.join(outputDir, args[2])
       
    59 
       
    60         # Use temporary directory
       
    61         tmpDir = "tmpdir"
       
    62         if os.path.exists(tmpDir):
       
    63             shutil.rmtree(tmpDir)
       
    64         locDir = os.path.join(tmpDir, RESOURCES_PATH)
       
    65         os.makedirs(locDir)
       
    66 
       
    67         print "Processing loc files from %s to %s" % (inputDir, resourcesFilename)
       
    68         locFileCount = 0;
       
    69 
       
    70         # Go through all Java loc file names, converting the .ts to .loc
       
    71         for name in JAVA_LOC_FILES:
       
    72 
       
    73             # Find all existing ts-files
       
    74             for ts in glob.glob("%s/%s_*.ts" % (inputDir, name)):
       
    75                 path, filename = os.path.split(ts)
       
    76                 base, ext = os.path.splitext(filename)
       
    77                 loc = os.path.join(locDir, base + ".loc")
       
    78                 print "Converting %s" % ts
       
    79                 writeLoc(loc, readTs(ts))
       
    80                 locFileCount = locFileCount + 1
       
    81 
       
    82         # Update the target jar with loc-files
       
    83         if os.path.isfile(resourcesFilename):
       
    84             os.system("jar ufM " + resourcesFilename + " -C " + tmpDir + " .")
       
    85         else:
       
    86             os.system("jar cfM " + resourcesFilename + " -C " + tmpDir + " .")
       
    87 
       
    88         print "Processed %d loc files" % (locFileCount)
       
    89         shutil.rmtree(tmpDir)
       
    90 
       
    91     except:
       
    92         print "Usage: %s <input_dir> <output_dir>" % sys.argv[0]
       
    93         traceback.print_exc()
       
    94         sys.exit(1)
       
    95 
       
    96 def readTs(filename):
       
    97     messages = []
       
    98     tree = et.parse(filename).getroot()
       
    99     for message in tree.find("context").findall("message"):
       
   100         id = message.get("id")
       
   101         translation = message.find("translation")
       
   102 
       
   103         # Sanity check - we have no good plurality support for qt-localisation
       
   104         if translation.find("numerusform") != None:
       
   105             raise Exception("Conversion error at %s / %s: numerus form (qt plurality) is not supported" % (filename, id))
       
   106 
       
   107         lengthvariant = translation.find("lengthvariant")
       
   108         if lengthvariant != None:
       
   109             text = lengthvariant.text
       
   110         else:
       
   111             text = translation.text
       
   112 
       
   113         # Sanity check - no newlines in text allowed
       
   114         if "\n" in text:
       
   115             raise Exception("Conversion error in %s / %s: newline found" % (filename, id))
       
   116         
       
   117         messages.append((id, text))
       
   118 
       
   119     return messages
       
   120 
       
   121 def writeLoc(filename, messages):
       
   122     loc = codecs.open(filename, "w", "UTF-8")
       
   123     loc.write(u"CHARACTER_SET UTF8\n")
       
   124     for id, message in messages:
       
   125         loc.write(u'#define %s "%s"\n' % (id, message))
       
   126     loc.close()
       
   127 
       
   128 if __name__ == "__main__":
       
   129     main()