bin/resourcifier.py
changeset 0 16d8024aca5e
child 34 ed14f46c0e55
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 #
       
     4 #############################################################################
       
     5 ##
       
     6 ## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     7 ## All rights reserved.
       
     8 ## Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     9 ##
       
    10 ## This file is part of the UI Extensions for Mobile.
       
    11 ##
       
    12 ## GNU Lesser General Public License Usage
       
    13 ## This file may be used under the terms of the GNU Lesser General Public
       
    14 ## License version 2.1 as published by the Free Software Foundation and
       
    15 ## appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    16 ## Please review the following information to ensure the GNU Lesser General
       
    17 ## Public License version 2.1 requirements will be met:
       
    18 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    19 ##
       
    20 ## In addition, as a special exception, Nokia gives you certain additional
       
    21 ## rights.  These rights are described in the Nokia Qt LGPL Exception
       
    22 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    23 ##
       
    24 ## If you have questions regarding the use of this file, please contact
       
    25 ## Nokia at developer.feedback@nokia.com.
       
    26 ##
       
    27 #############################################################################
       
    28 
       
    29 import os
       
    30 import sys
       
    31 import fnmatch
       
    32 import optparse
       
    33 
       
    34 # ============================================================================
       
    35 # Globals
       
    36 # ============================================================================
       
    37 INPUT = os.getcwd()
       
    38 OUTPUT = sys.stdout
       
    39 INCLUDE = None
       
    40 EXCLUDE = None
       
    41 INDENTATION = "    "
       
    42 
       
    43 # ============================================================================
       
    44 # OptionParser
       
    45 # ============================================================================
       
    46 class OptionParser(optparse.OptionParser):
       
    47     def __init__(self):
       
    48         optparse.OptionParser.__init__(self)
       
    49         self.add_option("-i", "--input", dest="input", metavar="dir",
       
    50                         help="specify the input <dir> (default %s)" % INPUT)
       
    51         self.add_option("-o", "--output", dest="output", metavar="file",
       
    52                         help="specify the output <file> (default stdout)")
       
    53         self.add_option("--include", dest="include", action="append", metavar="pattern",
       
    54                         help="specify the include <pattern> (default %s)" % INCLUDE)
       
    55         self.add_option("--exclude", dest="exclude", action="append", metavar="pattern",
       
    56                         help="specify the exclude <pattern> (default %s)" % EXCLUDE)
       
    57         self.add_option("--indentation", dest="indentation", metavar="level",
       
    58                         help="specify the indentation <level> (default '%s')" % INDENTATION)
       
    59         self.set_defaults(input=INPUT)
       
    60         self.set_defaults(output=OUTPUT)
       
    61         self.set_defaults(include=INCLUDE)
       
    62         self.set_defaults(exclude=EXCLUDE)
       
    63         self.set_defaults(indentation=INDENTATION)
       
    64 
       
    65 # ============================================================================
       
    66 # Utils
       
    67 # ============================================================================
       
    68 def index_resources():
       
    69     result = []
       
    70     for root, dirst, files in os.walk(os.curdir):
       
    71         for name in files:
       
    72             include = True
       
    73             filepath = os.path.normpath(os.path.join(root, name))
       
    74             if INCLUDE != None:
       
    75                 for pattern in INCLUDE:
       
    76                     if not fnmatch.fnmatch(filepath, pattern):
       
    77                         include = False
       
    78             if EXCLUDE != None:
       
    79                 for pattern in EXCLUDE:
       
    80                     if fnmatch.fnmatch(filepath, pattern):
       
    81                         include = False
       
    82             if include:
       
    83                 result.append(filepath)
       
    84     return result
       
    85 
       
    86 def write_qrc(out, resources):
       
    87     out.write("<!DOCTYPE RCC><RCC version=\"1.0\">\n")
       
    88     out.write("<qresource prefix=\"/\">\n")
       
    89     for resource in resources:
       
    90         out.write("%s<file>%s</file>\n" % (INDENTATION, resource))
       
    91     out.write("</qresource>\n")
       
    92     out.write("</RCC>\n")
       
    93 
       
    94 # ============================================================================
       
    95 # main()
       
    96 # ============================================================================
       
    97 def main():
       
    98     global INPUT, OUTPUT, INCLUDE, EXCLUDE, INDENTATION
       
    99 
       
   100     parser = OptionParser()
       
   101     (options, args) = parser.parse_args()
       
   102 
       
   103     if options.input:
       
   104         INPUT = options.input
       
   105     if options.output != sys.stdout:
       
   106         outputdir = os.path.dirname(options.output)
       
   107         if len(outputdir) and not os.path.exists(outputdir):
       
   108             os.makedirs(outputdir)
       
   109         OUTPUT = open(options.output, "w")
       
   110     if options.include:
       
   111         INCLUDE = options.include
       
   112     if options.exclude:
       
   113         EXCLUDE = options.exclude
       
   114     if options.indentation:
       
   115         INDENTATION = options.indentation.replace("\\t", "\t")
       
   116 
       
   117     os.chdir(INPUT)
       
   118     resources = index_resources()
       
   119     return write_qrc(OUTPUT, resources)
       
   120 
       
   121 if __name__ == "__main__":
       
   122     sys.exit(main())