releasing/blocks/cclient/buildpackage.py
changeset 632 934f9131337b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/releasing/blocks/cclient/buildpackage.py	Thu Sep 02 15:02:14 2010 +0800
@@ -0,0 +1,173 @@
+#!/usr/bin/python
+
+#
+# Copyright (c) 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".
+#
+# Initial Contributors:
+# Nokia Corporation - initial contribution.
+#
+# Contributors:
+#
+# Description:
+# Builds installation packages
+#
+
+import zipfile
+import tarfile
+import os
+import sys
+import compileall
+import datetime
+from collections import defaultdict
+import itertools
+from optparse import OptionParser
+
+sys.path.append("blocks/python")
+import utils
+
+class FileList(object):
+    ''' File list for each OS '''
+    def __init__(self, listname):
+        self.files = defaultdict(set)
+        with open(listname) as f:
+            fileType = None
+            for line in (l.strip() for l in f):
+                if line != "":
+                    if line.startswith("[") and line.endswith("]"):
+                        fileType = line[1:-1].lower()
+                    elif fileType:
+                        line = os.path.normcase(line)
+                        self.files[fileType].add(line)
+
+    def popFile(self, path):
+        for k, paths in self.files.items():
+            if path in paths:
+                if k != "ignore":
+                    self.files[k].remove(path)
+                return k
+
+    def getAll(self):
+        all = []
+        for k, paths in self.files.iteritems():
+            if k != "ignore":
+                all.extend(itertools.izip_longest([k] * len(paths), paths))
+        return all
+
+def main():
+    parser = OptionParser(prog="build-package", usage="%prog [OPTIONS] package")
+    parser.add_option("-d", "--dryrun", action="store_true", help="just show what would happen")
+    parser.add_option("-n", "--newversion", action="store_true", help="increase version number for next release")
+    parser.add_option("-i", "--ignore-errors", action="store_true", help="ignore errors")
+    parser.set_defaults(dryrun=False, newversion=False)
+    (options, args) = parser.parse_args()
+    if args:
+        packageName = args[0]
+    else:
+        parser.print_help()
+        print
+        sys.exit("Package to build required as an argument")
+
+    error = ""
+    DIRECTORY = "blocks"
+    infoPath = "%s/python/%s_info.py" % (DIRECTORY, packageName)
+    version_info = {}
+
+    def writeInfo():
+        with open(infoPath, "w") as f:
+            for k, v in sorted(version_info.iteritems()):
+                if k != "__builtins__":
+                    f.write("%s = %s\n" % (k, v if isinstance(v, int) else '"%s"' % v))
+
+    execfile(infoPath, version_info)
+    version_info["VERSION_DATE"] = datetime.date.today().isoformat()
+    if not options.dryrun:
+        writeInfo()
+
+    if version_info["VERSION_PRE_RELEASE"] > 0:
+        version_string = packageName + "-%(VERSION_MAJOR)d.%(VERSION_MINOR)d.%(VERSION_REVISION)d%(VERSION_PRE_RELEASE_ID)s%(VERSION_PRE_RELEASE)d" % version_info
+    else:
+        version_string = packageName + "-%(VERSION_MAJOR)d.%(VERSION_MINOR)d.%(VERSION_REVISION)d" % version_info
+
+    print "Byte compiling..."
+    compileall.compile_dir(DIRECTORY, force=1)
+    print
+
+    filelist = FileList("%s_files" % packageName)
+    skipped_files = set()
+
+    print "Archiving..."
+    zipName = version_string + ".zip"
+    tarName = version_string + ".tar.gz"
+    if not options.dryrun:
+        zipArchive = zipfile.ZipFile(zipName, "w", zipfile.ZIP_DEFLATED)
+        tarArchive = tarfile.open(tarName, "w:gz")
+    for root, _, files in os.walk(DIRECTORY):
+        for name in (f for f in files if not f.endswith((".pyc", ".pyo", ".~py", ".bak"))):
+            path = os.path.join(root, name)
+            normpath = os.path.normcase(path)
+            fileType = filelist.popFile(normpath)
+            if fileType is None:
+                skipped_files.add(path)
+            elif fileType != "ignore":
+                if not options.dryrun:
+                    archName = path.replace(DIRECTORY, version_string, 1)
+                    if fileType == "windows" or fileType == "common":
+                        zipArchive.write(path, archName)
+                    if fileType == "linux" or fileType == "common":
+                        tarArchive.add(path, archName)
+                print path
+
+    if not options.dryrun:
+        zipArchive.close()
+        tarArchive.close()
+
+    leftovers = filelist.getAll()
+    if leftovers:
+        print
+        print "ERROR: Files that should have been packaged but not found:"
+        for ftype, name in sorted(leftovers):
+            print "%s: %s" % (ftype, name)
+
+        if not options.ignore_errors:
+            error += "All files were not packaged."
+            if not options.dryrun:
+                os.remove(tarName)
+                os.remove(zipName)
+
+    if skipped_files:
+        print
+        print "WARNING: Files found but not included in any package:"
+        for f in sorted(skipped_files):
+            print f
+
+    if error and options.newversion:
+        options.newversion = False
+        error += "\nSkipped version increase because of an error."
+
+    if options.newversion:
+        if version_info["VERSION_PRE_RELEASE"] != 0:
+            version_info["VERSION_PRE_RELEASE"] += 1
+        else:
+            version_info["VERSION_REVISION"] += 1
+
+    version_info["VERSION_DATE"] = ""
+    if not options.dryrun:
+        writeInfo()
+
+    utils.removeFilesRecursive(DIRECTORY, "*.pyc")
+
+    print
+    if error:
+        print "ERROR:", error
+    else:
+        print "Successfully created packages for %s:" % version_string
+        print zipName
+        print tarName
+
+if __name__ == "__main__":
+    main()
\ No newline at end of file