From 5f5afa7d021a71ca81a78774f7dede8f69bd5089 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Fri, 28 Jun 2013 12:32:52 +0200 Subject: [PATCH] Add script to generate picard.qrc from png images in images directory --- resources/makeqrc.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 resources/makeqrc.py diff --git a/resources/makeqrc.py b/resources/makeqrc.py new file mode 100644 index 000000000..c9aa8614e --- /dev/null +++ b/resources/makeqrc.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +import os, fnmatch +import re +from distutils import log +from distutils.dep_util import newer + +"""Build a Qt resources file with all png images found under images/ +It will update qrc file only if images newer than it are found +""" + +def tryint(s): + try: + return int(s) + except: + return s + +def natsort_key(s): + return [ tryint(c) for c in re.split('(\d+)', s) ] + + +def find_files(topdir, directory, pattern): + tdir = os.path.join(topdir, directory) + for root, dirs, files in os.walk(tdir): + for basename in files: + if fnmatch.fnmatch(basename, pattern): + filepath = os.path.join(root, basename) + filename = os.path.relpath(filepath, topdir) + yield filename + +def main(): + scriptdir = os.path.dirname(os.path.abspath(__file__)) + topdir = os.path.abspath(os.path.join(scriptdir, "..")) + resourcesdir = os.path.join(topdir, "resources") + qrcfile = os.path.join(resourcesdir, "picard.qrc") + images = [i for i in find_files(resourcesdir, 'images', '*.png')] + newimages = 0 + for filename in images: + filepath = os.path.join(resourcesdir, filename) + if newer(filepath, qrcfile): + newimages += 1 + if newimages: + log.info("%d images newer than %s found" % (newimages, qrcfile)) + with open(qrcfile, 'wb+') as f: + f.write('\r\n\r\n') + n = 0 + for filename in sorted(images, key=natsort_key): + f.write(' %s\r\n' % filename) + n += 1 + f.write('\r\n\r\n') + log.info("File %s written, %d images" % (qrcfile, n)) + + +if __name__ == "__main__": + log.set_verbosity(1) + main()