bin/sync.py
changeset 1 28e8d4c0e55e
parent 0 4ee42872ac7b
--- a/bin/sync.py	Fri Mar 19 09:40:18 2010 +0200
+++ b/bin/sync.py	Mon Apr 19 14:02:15 2010 +0300
@@ -1,39 +1,55 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
 #
-# ============================================================================
-#  Name        : sync.py
-#  Part of     : Hb
-#  Description : Hb themes sync script
-#  Version     : %version: 1 %
+# Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
+# All rights reserved.
+# This component and the accompanying materials are made available
+# under the terms of "Eclipse Public License v1.0"
+# which accompanies this distribution, and is available
+# at the URL "http://www.eclipse.org/legal/epl-v10.html".
 #
-#  Copyright (c) 2008-2009 Nokia.  All rights reserved.
-#  This material, including documentation and any related computer
-#  programs, is protected by copyright controlled by Nokia.  All
-#  rights are reserved.  Copying, including reproducing, storing,
-#  adapting or translating, any or all of this material requires the
-#  prior written consent of Nokia.  This material also contains
-#  confidential information which may not be disclosed to others
-#  without the prior written consent of Nokia.
-# ============================================================================
+# Initial Contributors:
+# Nokia Corporation - initial contribution.
+#
+# Contributors:
+#
+# Description:  Hb themes sync script
+#
 
 import os
+import re
+import sys
+import time
 import shutil
+import fnmatch
 import zipfile
 import optparse
+import tempfile
+if sys.version_info[0] == 2 and sys.version_info[1] < 4:
+    # for scratchbox compatibility
+    import popen2
+else:
+    import subprocess
 
 # ============================================================================
 # Globals
 # ============================================================================
 VERBOSE = False
-EXTRACT = False
 ARCHIVES = False
+INCLUDE = None
+EXCLUDE = None
 INPUT_DIR = os.getcwd()
 OUTPUT_DIR = os.getcwd()
 IBY_SOURCE_PREFIX = "ZRESOURCE/hb/themes"
 IBY_TARGET_PREFIX = "RESOURCE_FILES_DIR/hb/themes"
-BLD_TARGET_PREFIX = "/epoc32/data/z/resource/hb/themes"
-BLD_2ND_TARGET_PREFIX = "/epoc32/winscw/c/resource/hb/themes"
+BLD_HW_TARGET_PREFIX = "/epoc32/data/z/resource/hb/themes"
+BLD_EMU_TARGET_PREFIX = "/epoc32/winscw/c/resource/hb/themes"
+BLD_TARGET_PREFIXES = []
+SYMBIAN = False
+EXIT_STATUS = 0
+NAME = "themes"
+THEME_COMMON = "themecommon"
+THEME_SETTINGS_FILE = "theme.theme"
+ENCODER = "SVGTBinEncode.exe"
+NVG = False
 
 # ============================================================================
 # OptionParser
@@ -45,16 +61,26 @@
                         help="print verbose information about each step of the sync process")
         self.add_option("-q", "--quiet", action="store_false", dest="verbose",
                         help="do not print information about each step of the sync process")
+        self.add_option("-n", "--name", dest="name", metavar="name",
+                        help="specify the package <name> (default %s)" % NAME)
+        self.add_option("--symbian", action="store_true", dest="symbian",
+                        help="work in Symbian mode")
+        self.add_option("--nvg", action="store_true", dest="nvg",
+                        help="do convert svg to nvg")
+        self.add_option("--no-nvg", action="store_false", dest="nvg",
+                        help="do not convert svg to nvg")
 
         group = optparse.OptionGroup(self, "Input/output options")
         self.add_option("-i", "--input", dest="input", metavar="dir",
                         help="specify the input <dir> (default %s)" % INPUT_DIR)
         self.add_option("-o", "--output", dest="output", metavar="dir",
                         help="specify the output <dir> (default %s)" % OUTPUT_DIR)
-        self.add_option("-e", "--extract", action="store_true", dest="extract",
-                        help="extract archives for installation (default %s)" % EXTRACT)
         self.add_option("-a", "--archives", action="store_true", dest="archives",
                         help="export/install archives (default %s)" % ARCHIVES)
+        self.add_option("--include", dest="include", action="append", metavar="pattern",
+                        help="specify the include <pattern> (default %s)" % INCLUDE)
+        self.add_option("--exclude", dest="exclude", action="append", metavar="pattern",
+                        help="specify the exclude <pattern> (default %s)" % EXCLUDE)
         self.add_option_group(group)
 
         group = optparse.OptionGroup(self, "Prefix options")
@@ -62,8 +88,12 @@
                         help="specify the iby source <prefix> (default %s)" % IBY_SOURCE_PREFIX)
         self.add_option("--iby-target-prefix", dest="ibytargetprefix", metavar="prefix",
                         help="specify the iby target <prefix> (default %s)" % IBY_TARGET_PREFIX)
-        self.add_option("--bld-target-prefix", dest="bldtargetprefix", metavar="prefix",
-                        help="specify the bld target <prefix> (default %s)" % BLD_TARGET_PREFIX)
+        self.add_option("--bld-hw-target-prefix", dest="bldhwtargetprefix", metavar="prefix",
+                        help="specify the bld harware target <prefix> (default %s)" % BLD_HW_TARGET_PREFIX)
+        self.add_option("--bld-emu-target-prefix", dest="bldemutargetprefix", metavar="prefix",
+                        help="specify the bld emulator target <prefix> (default %s)" % BLD_EMU_TARGET_PREFIX)
+        self.add_option("--bld-target-prefix", dest="bldtargetprefixes", action="append", metavar="prefix",
+                        help="specify an additional bld target <prefix>")
         self.add_option_group(group)
 
 # ============================================================================
@@ -83,87 +113,173 @@
         return abspath[i:]
     os.path.relpath = relpath
 
-def extract(path, filepath):
-    if not os.path.exists(path):
-        os.makedirs(path)
-    
-    files = list()
-    if VERBOSE:
-        if EXTRACT:
-            print "Extracting: %s" % filepath
+def run_process(command, cwd=None):
+    code = 0
+    output = ""
+    try:
+        if cwd != None:
+            oldcwd = os.getcwd()
+            os.chdir(cwd)
+        if sys.version_info[0] == 2 and sys.version_info[1] < 4:
+            process = popen2.Popen4(command)
+            code = process.wait()
+            output = process.fromchild.read()
         else:
-            print "Reading: %s" % filepath
-    archive = zipfile.ZipFile(filepath)
-    for entry in archive.namelist():
-        if entry.endswith("/"):
-            if EXTRACT:
-                out = os.path.join(path, entry)
-                if not os.path.exists(out):
-                    os.makedirs(out)
-        else:
-            files.append(entry)
-            if EXTRACT:
-                out = open(os.path.join(path, entry), "w")
-                out.write(archive.read(entry))
-                out.close()
-    return files
+            process = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
+            (stdout, stderr) = process.communicate()
+            code = process.returncode
+            output = stdout + stderr
+        if cwd != None:
+            os.chdir(oldcwd)
+    except Exception, e:
+        print(e)
+        code = -1
+    return [code, output]
+
+def make_target(path):
+    # generate a compatible make target name from path
+    target = os.path.splitdrive(path)[1].strip("\\/")
+    return "_".join(re.split("[\\\/]+", target))
 
 class Theme:
     def __init__(self, name):
         self.name = name
         self.paths = []
-        self.archives = []
-        self.verbatims = []
-        self.sources = {}
-        self.targets = {}
+        self.files = {}
+        self.archives = {}
 
     def initialize(self):
         for path in self.paths:
             for root, dirs, files in os.walk(path):
                 for file in files:
                     filepath = os.path.join(root, file)
-                    extension = os.path.splitext(filepath)[1]
-                    if os.path.isfile(filepath) and extension == ".zip":
-                        self.archives.append(filepath)
-                    if os.path.isfile(filepath) and (extension in ['.css', '.theme']):
-                        self.verbatims.append(filepath)
-                        if VERBOSE:
-                            print "Found: %s" % filepath
-        for archive in self.archives:
-            path = os.path.dirname(archive)
-            if path not in self.sources:
-                self.sources[path] = list()
-            self.sources[path] += extract(path, archive)
-        for verbatim in self.verbatims:
-            path = os.path.dirname(verbatim)
-            if path not in self.sources:
-                self.sources[path] = list()
-            file = os.path.split(verbatim)[1]
-            filelist = list()
-            filelist.append(file)
-            self.sources[path] += filelist
-        for path, files in self.sources.iteritems():
+                    if self._include(filepath):
+                        extension = os.path.splitext(filepath)[1]
+                        if extension == ".zip":
+                            if root not in self.archives:
+                                self.archives[root] = list()
+                            self.archives[root].append(filepath)
+                        else:
+                            if root not in self.files:
+                                self.files[root] = list()
+                            self.files[root].append(filepath)
+
+    def _write_zip_entry(self, archive, filepath):
+        path, filename = os.path.split(filepath)
+        oldcwd = os.getcwd()
+        os.chdir(path)
+        archive.write(filename)
+        os.chdir(oldcwd)
+
+    def encode(self):
+        print "Encoding: %s" % self.name
+        for path, archives in self.archives.iteritems():
             relpath = os.path.relpath(path, INPUT_DIR)
-            if relpath not in self.targets:
-                self.targets[relpath] = list()
-            self.targets[relpath] = files
+            if not relpath.startswith("icons"):
+                continue
+            for archive in archives:
+                # ensure that output dir exists
+                outpath = os.path.join(OUTPUT_DIR, relpath)
+                if not os.path.exists(outpath):
+                    os.makedirs(outpath)
+
+                # extract to a temp dir
+                tempdir = tempfile.mkdtemp()
+                zip = zipfile.ZipFile(archive)
+                for name in zip.namelist():
+                    file = open(os.path.join(tempdir, name),'w')
+                    file.write(zip.read(name))
+                    file.close()
 
-    def write_iby(self, filepath):
-        global IBY_SOURCE_PREFIX, IBY_TARGET_PREFIX
-        out = open(filepath, "w")
+                # convert & re-archive
+                total = 0
+                converted = 0
+                tmpfile, tmpfilepath = tempfile.mkstemp(".zip")
+                tmparchive = zipfile.ZipFile(tmpfilepath, 'w')
+                for root, dirs, files in os.walk(tempdir):
+                    for file in files:
+                        filepath = os.path.join(root, file)
+                        basepath, extension = os.path.splitext(filepath)
+                        if extension == ".svg":
+                            total += 1
+                            res = run_process([ENCODER, "-v", "6", filepath, "-e", ".nvg"])[0]
+                            exists = os.path.exists(basepath + ".nvg")
+                            if not exists:
+                                self._write_zip_entry(tmparchive, filepath)
+                            else:
+                                converted += 1
+                                self._write_zip_entry(tmparchive, basepath + ".nvg")
+       
+                # cleanup
+                tmparchive.close()
+                os.close(tmpfile)
+                if converted > 0:
+                    shutil.move(tmpfilepath, os.path.join(outpath, os.path.basename(archive)))
+                else:
+                    os.remove(tmpfilepath)
+                shutil.rmtree(tempdir, True)
+                print "          %s (%s/%s)" % (os.path.join(relpath, os.path.basename(archive)), converted, total)
+
+    def write_iby(self, ibypath):
+        global IBY_SOURCE_PREFIX, IBY_TARGET_PREFIX, EXIT_STATUS
+        outpath = os.path.dirname(ibypath)
+        if not os.path.exists(outpath):
+            os.makedirs(outpath)
+        out = open(ibypath, "w")
         out.write("#ifndef __%s_IBY__\n" % self.name.upper())
         out.write("#define __%s_IBY__\n" % self.name.upper())
         out.write("\n")
         out.write("#include <bldvariant.hrh>\n")
         out.write("\n")
-        for path, entries in self.targets.iteritems():
-            for entry in entries:
-                entry = os.path.join(path, entry)
-                out.write("data=%s/%s\t%s/%s\n" % (IBY_SOURCE_PREFIX, entry, IBY_TARGET_PREFIX, entry))
+        out.write("data=%s/%s.themeindex\t%s/%s.themeindex\n" % (IBY_SOURCE_PREFIX, self.name, IBY_TARGET_PREFIX, self.name))
+        written_entries = list()
+        for path, files in self.files.iteritems():
+            relpath = os.path.relpath(path, INPUT_DIR)
+            for filepath in files:
+                filename = os.path.basename(filepath)
+                entry = os.path.join(relpath, filename)
+                if entry not in written_entries:
+                    written_entries.append(filepath)
+                    out.write("data=%s/%s\t%s/%s\n" % (IBY_SOURCE_PREFIX, entry, IBY_TARGET_PREFIX, entry))
+                else:
+                    print "ERROR: %s duplicate entry %s" % (ibypath, entry)
+                    EXIT_STATUS = -1
+        for path, archives in self.archives.iteritems():
+            relpath = os.path.relpath(path, INPUT_DIR)
+            for archive in archives:
+                files = self._list_files(archive)
+                for filepath in files:
+                    entry = os.path.join(relpath, filepath)
+                    if entry not in written_entries:
+                        written_entries.append(entry)
+                        out.write("data=%s/%s\t%s/%s\n" % (IBY_SOURCE_PREFIX, entry, IBY_TARGET_PREFIX, entry))
+                    else:
+                        print "ERROR: %s duplicate entry %s" % (ibypath, entry)
+                        EXIT_STATUS = -1
         out.write("\n")
         out.write("#endif __%s_IBY__\n" % self.name.upper())
         out.close()
 
+    def _list_files(self, filepath):
+        files = list()
+        archive = zipfile.ZipFile(filepath)
+        for entry in archive.namelist():
+            if not entry.endswith("/"):
+                files.append(entry)
+        return files
+
+    def _include(self, filepath):
+        result = True
+        if INCLUDE != None:
+            for pattern in INCLUDE:
+                if not fnmatch.fnmatch(filepath, pattern):
+                    result = False
+        if EXCLUDE != None:
+            for pattern in EXCLUDE:
+                if fnmatch.fnmatch(filepath, pattern):
+                    result = False
+        return result
+
 def lookup_themes(path):
     themes = {}
     # base: effects, icons...
@@ -179,88 +295,187 @@
                     themes[theme].paths.append(themepath)
     return themes
 
-def write_pri(filepath, themes):
+def write_txt(filepath, themes, prefixes):
+    outpath = os.path.dirname(filepath)
+    if not os.path.exists(outpath):
+        os.makedirs(outpath)
+    out = open(filepath, "w")
+    for name, theme in themes.iteritems():
+        for prefix in prefixes:
+            path = os.path.normpath("%s/icons/%s" % (prefix, name))
+            out.write("%s %s %s\n" % (name, path, prefix))
+    out.close()
+
+def write_pri(filepath, themes, settingsfile_exists):
+    outpath = os.path.dirname(filepath)
+    if not os.path.exists(outpath):
+        os.makedirs(outpath)
+    outpath = os.path.splitdrive(OUTPUT_DIR)[1]
     out = open(filepath, "w")
     out.write("symbian {\n")
     out.write("\tBLD_INF_RULES.prj_exports += \"$${LITERAL_HASH}include <platform_paths.hrh>\"\n")
+    # TODO: temp workaround to include pre-generated .themeindex files
+    rompath = os.path.join(os.getcwd(), "rom")
+    for entry in os.listdir(rompath):
+        filepath = os.path.join(rompath, entry)
+        if os.path.isfile(filepath) and os.path.splitext(filepath)[1] == ".themeindex":
+            filepath = os.path.splitdrive(filepath)[1]
+            filename = os.path.basename(filepath)
+            out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_HW_TARGET_PREFIX, filename))
+            out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_EMU_TARGET_PREFIX, filename))
+
+    if settingsfile_exists:
+        # exporting theme settings file
+        settingsPath = os.path.splitdrive(os.path.join(INPUT_DIR,THEME_SETTINGS_FILE))[1]
+        out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (settingsPath, BLD_HW_TARGET_PREFIX, THEME_SETTINGS_FILE))
+        out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (settingsPath, BLD_EMU_TARGET_PREFIX, THEME_SETTINGS_FILE))
+        out.write("\tBLD_INF_RULES.prj_exports += \"%s.iby\tCORE_MW_LAYER_IBY_EXPORT_PATH(%s.iby)\"\n" % (os.path.join(outpath, THEME_COMMON), THEME_COMMON))
+
     for name, theme in themes.iteritems():
         ibyfile = "%s.iby" % name
-        out.write("\tBLD_INF_RULES.prj_exports += \"%s\tCORE_MW_LAYER_IBY_EXPORT_PATH(%s)\"\n" % (ibyfile, ibyfile))
-        for verbatim in theme.verbatims:
-            filename = os.path.basename(verbatim)
-            relpath = os.path.relpath(os.path.dirname(verbatim), INPUT_DIR)
-            verbatim = os.path.splitdrive(verbatim)[1]
-            out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (verbatim, BLD_TARGET_PREFIX, os.path.join(relpath, filename)))
-            out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (verbatim, BLD_2ND_TARGET_PREFIX, os.path.join(relpath, filename)))
-        for archive in theme.archives:
-            filename = os.path.basename(archive)
-            relpath = os.path.relpath(os.path.dirname(archive), INPUT_DIR)
-            archive = os.path.splitdrive(archive)[1]
-            if ARCHIVES:
-                out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (archive, BLD_TARGET_PREFIX, os.path.join(relpath, filename)))
-                out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (archive, BLD_2ND_TARGET_PREFIX, os.path.join(relpath, filename)))
-            else:
-                out.write("\tBLD_INF_RULES.prj_exports += \":zip %s\t%s/%s\"\n" % (archive, BLD_TARGET_PREFIX, relpath))
-                out.write("\tBLD_INF_RULES.prj_exports += \":zip %s\t%s/%s\"\n" % (archive, BLD_2ND_TARGET_PREFIX, relpath))
+        out.write("\tBLD_INF_RULES.prj_exports += \"%s\tCORE_MW_LAYER_IBY_EXPORT_PATH(%s)\"\n" % (os.path.join(outpath, ibyfile), ibyfile))
+        for path, files in theme.files.iteritems():
+            relpath = os.path.relpath(path, INPUT_DIR)
+            for filepath in files:
+                filepath = os.path.splitdrive(filepath)[1]
+                filename = os.path.basename(filepath)
+                out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_HW_TARGET_PREFIX, os.path.join(relpath, filename)))
+                out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_EMU_TARGET_PREFIX, os.path.join(relpath, filename)))
+        for path, archives in theme.archives.iteritems():
+            relpath = os.path.relpath(path, INPUT_DIR)
+            for filepath in archives:
+                filepath = os.path.splitdrive(filepath)[1]
+                filename = os.path.basename(filepath)
+                if ARCHIVES:
+                    out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_HW_TARGET_PREFIX, os.path.join(relpath, filename)))
+                    out.write("\tBLD_INF_RULES.prj_exports += \"%s\t%s/%s\"\n" % (filepath, BLD_EMU_TARGET_PREFIX, os.path.join(relpath, filename)))
+                else:
+                    out.write("\tBLD_INF_RULES.prj_exports += \":zip %s\t%s/%s\"\n" % (filepath, BLD_HW_TARGET_PREFIX, relpath))
+                    out.write("\tBLD_INF_RULES.prj_exports += \":zip %s\t%s/%s\"\n" % (filepath, BLD_EMU_TARGET_PREFIX, relpath))
     out.write("} else {\n")
+    out.write("\tisEmpty(QMAKE_UNZIP):QMAKE_UNZIP = unzip -u -o\n")
+
+    if settingsfile_exists:
+    # installing theme settings file
+        settingsPath = os.path.join(INPUT_DIR,THEME_SETTINGS_FILE)
+        out.write("\t%s.path += $$(HB_THEMES_DIR)/themes\n" % THEME_COMMON)
+        out.write("\t%s.files += %s\n" % (THEME_COMMON, settingsPath))
+        out.write("\tINSTALLS += %s\n" % THEME_COMMON)
+
     for name, theme in themes.iteritems():
-        if ARCHIVES:
-            i = 1
-            for archive in theme.archives:
-                relpath = os.path.relpath(os.path.dirname(archive), INPUT_DIR)
-                out.write("\t%s%i.path = $$(HB_THEMES_DIR)/themes/%s\n" % (name, i, relpath))
-                out.write("\t%s%i.files += %s\n" % (name, i, archive))
-                out.write("\tINSTALLS += %s%i\n" % (name, i))
-                i += 1
-        else:
-            i = 1
-            for path, files in theme.sources.iteritems():
-                relpath = os.path.relpath(path, INPUT_DIR)
-                out.write("\t%s%i.path = $$(HB_THEMES_DIR)/themes/%s\n" % (name, i, relpath))
-                for file in files:
-                    out.write("\t%s%i.files += %s\n" % (name, i, os.path.join(path, file)))
-                out.write("\tINSTALLS += %s%i\n" % (name, i))
-                i += 1
+        for path, files in theme.files.iteritems():
+            target = make_target(path)
+            relpath = os.path.relpath(path, INPUT_DIR)
+            out.write("\t%s.path += $$(HB_THEMES_DIR)/themes/%s\n" % (target, relpath))
+            out.write("\t%s.files += %s\n" % (target, " ".join(files)))
+            out.write("\tINSTALLS += %s\n" % target)
+        for path, archives in theme.archives.iteritems():
+            target = make_target(path)
+            relpath = os.path.relpath(path, INPUT_DIR)
+            out.write("\t%s_zip.path += $$(HB_THEMES_DIR)/themes/%s\n" % (target, relpath))
+            if ARCHIVES:
+                out.write("\t%s_zip.files += %s\n" % (target, " ".join(archives)))
+            else:
+                commands = []
+                for archive in archives:
+                    commands.append("$$QMAKE_UNZIP %s -d $$(HB_THEMES_DIR)/themes/%s" % (archive, relpath))
+                out.write("\t%s_zip.commands += %s\n" % (target, " && ".join(commands)))
+            out.write("\tINSTALLS += %s_zip\n" % target)
     out.write("}\n")
     out.close()
 
+
+def write_common_iby(path):
+    global VERBOSE, IBY_SOURCE_PREFIX, IBY_TARGET_PREFIX, OUTPUT_DIR, INPUT_DIR 
+    global THEME_COMMON, THEME_SETTINGS_FILE
+
+    # Create iby file for theme.theme if it is there
+    theme_theme = os.path.join(INPUT_DIR,THEME_SETTINGS_FILE)
+    if os.path.isfile(theme_theme):
+        if VERBOSE:
+            print "Writing:  %s.iby" % THEME_COMMON
+        ibypath = os.path.join(OUTPUT_DIR, THEME_COMMON + ".iby")
+        outpath = os.path.dirname(ibypath)
+        if not os.path.exists(outpath):
+            os.makedirs(outpath)
+        out = open(ibypath, "w")
+        out.write("#ifndef __%s_IBY__\n" % THEME_COMMON.upper())
+        out.write("#define __%s_IBY__\n" % THEME_COMMON.upper())
+        out.write("\n")
+        out.write("#include <bldvariant.hrh>\n")
+        out.write("\n")
+        out.write("data=%s/%s\t%s/%s\n" % (IBY_SOURCE_PREFIX, THEME_SETTINGS_FILE, IBY_TARGET_PREFIX, THEME_SETTINGS_FILE))
+        out.write("\n")
+        out.write("#endif __%s_IBY__\n" % THEME_COMMON.upper())
+        return True
+
+    # theme common iby not written, return false
+    return False
+
 # ============================================================================
 # main()
 # ============================================================================
 def main():
-    global VERBOSE, EXTRACT, ARCHIVES, INPUT_DIR, OUTPUT_DIR
-    global IBY_SOURCE_PREFIX, IBY_TARGET_PREFIX, BLD_TARGET_PREFIX
+    global VERBOSE, ARCHIVES, INPUT_DIR, OUTPUT_DIR, INCLUDE, EXCLUDE, SYMBIAN, NAME, NVG
+    global IBY_SOURCE_PREFIX, IBY_TARGET_PREFIX
+    global BLD_HW_TARGET_PREFIX, BLD_EMU_TARGET_PREFIX, BLD_TARGET_PREFIXES
 
     parser = OptionParser()
     (options, args) = parser.parse_args()
 
-    if options.verbose:
+    if options.verbose != None:
         VERBOSE = options.verbose
-    if options.extract:
-        EXTRACT = options.extract
-    if options.archives:
+    if options.symbian != None:
+        SYMBIAN = options.symbian
+    if options.nvg != None:
+        NVG = options.nvg
+    if options.name != None:
+        NAME = options.name
+    if options.archives != None:
         ARCHIVES = options.archives
-    if options.input:
+    if options.include != None:
+        INCLUDE = options.include
+    if options.exclude != None:
+        EXCLUDE = options.exclude
+    if options.input != None:
         INPUT_DIR = options.input
-    if options.output:
+    if options.output != None:
         OUTPUT_DIR = options.output
-    if options.ibysourceprefix:
+    if options.ibysourceprefix != None:
         IBY_SOURCE_PREFIX = options.ibysourceprefix
-    if options.ibytargetprefix:
+    if options.ibytargetprefix != None:
         IBY_TARGET_PREFIX = options.ibytargetprefix
-    if options.bldtargetprefix:
-        BLD_TARGET_PREFIX = options.bldtargetprefix
+    if options.bldhwtargetprefix != None:
+        BLD_HW_TARGET_PREFIX = options.bldhwtargetprefix
+    if options.bldemutargetprefix != None:
+        BLD_EMU_TARGET_PREFIX = options.bldemutargetprefix
+    if options.bldtargetprefixes != None:
+        BLD_TARGET_PREFIXES = options.bldtargetprefixes
+
+    settingsfile_exists = write_common_iby(INPUT_DIR)
 
     themes = lookup_themes(INPUT_DIR)
     for name, theme in themes.iteritems():
         theme.initialize()
+        if SYMBIAN and NVG:
+            theme.encode()
         if VERBOSE:
-            print "Writing: %s.iby" % name
+            print "Writing:  %s.iby" % name
         theme.write_iby(os.path.join(OUTPUT_DIR, "%s.iby" % name))
 
     if VERBOSE:
-        print "Writing: themes.pri"
-    write_pri(os.path.join(OUTPUT_DIR, "themes.pri"), themes)
+        print "Writing:  %s.pri" % NAME
+    write_pri(os.path.join(OUTPUT_DIR, "%s.pri" % NAME), themes, settingsfile_exists)
+    if VERBOSE:
+        print "Writing:  %s.txt" % NAME
+    if SYMBIAN:
+        prefixes = [BLD_HW_TARGET_PREFIX, BLD_EMU_TARGET_PREFIX]
+        prefixes += BLD_TARGET_PREFIXES
+        write_txt(os.path.join(OUTPUT_DIR, "%s.txt" % NAME), themes, prefixes)
+    else:
+        write_txt(os.path.join(OUTPUT_DIR, "%s.txt" % NAME), themes, [os.path.join(os.environ["HB_THEMES_DIR"], "themes")])
+
+    return EXIT_STATUS
 
 if __name__ == "__main__":
-    main()
+    sys.exit(main())