Code

Added script contributed by Mark Cave-Ayland
authorcajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 5 Aug 2011 12:11:38 +0000 (12:11 +0000)
committercajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 5 Aug 2011 12:11:38 +0000 (12:11 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6@20964 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/contrib/build-gosa-from-dirs [new file with mode: 0644]

diff --git a/gosa-core/contrib/build-gosa-from-dirs b/gosa-core/contrib/build-gosa-from-dirs
new file mode 100644 (file)
index 0000000..1fbaa9d
--- /dev/null
@@ -0,0 +1,153 @@
+#!/usr/bin/env python
+#-*- coding: utf-8 -*-
+import os
+import re
+import sys
+import shutil
+import tarfile
+import subprocess
+import logging
+from optparse import OptionParser
+from os.path import join
+from tempfile import mkdtemp
+
+# Version setter
+VERSION = "1.0"
+methods = {}
+
+def tar_extract(name, target):
+    tar = tarfile.open(name, "r:gz")
+    tar.extractall(target)
+    tar.close()
+
+def tar_create(name, source):
+    tar = tarfile.open(name, "w:gz")
+    tar.add(source)
+    tar.close()
+
+def ignore_svn(path, names):
+    return ['.svn']
+
+def build(target="/tmp/gosa", key_id=None, cleanup=True):
+
+    # Absolutize target
+    target_dir = os.path.abspath(target)
+
+    # Create target and temporary directory
+    try:
+        tmp_dir = mkdtemp()
+        os.makedirs(target_dir)
+
+        shutil.copytree("../../gosa-core", join(tmp_dir, "gosa-core"), ignore=ignore_svn)
+        shutil.copytree("../../gosa-plugins", join(tmp_dir, "gosa-plugins"), ignore=ignore_svn)
+        shutil.copytree("../../gosa-si", join(tmp_dir, "gosa-si"), ignore=ignore_svn)
+
+        # Prepare gosa-core for archiving
+        core_dir = join(tmp_dir, "gosa", "gosa-core")
+        os.makedirs(join(tmp_dir, "gosa"))
+        shutil.move(join(tmp_dir, "gosa-core"), join(tmp_dir, "gosa"))
+        shutil.move(join(core_dir, "debian"), join(join(target_dir, "gosa"), "debian"))
+
+        # Remove all but .php files provided by GOsa
+        smarty_dir = join(core_dir, "include", "smarty")
+        for rfile in [f for f in os.listdir(join(smarty_dir, "plugins"))
+            if not f in ["block.render.php", "block.t.php", "function.msgPool.php",
+            "function.factory.php"]]:
+
+            os.remove(join(smarty_dir, "plugins", rfile))
+
+        for rfile in [join(smarty_dir, f) for f in os.listdir(smarty_dir)]:
+            if os.path.basename(rfile) != "plugins":
+                if os.path.isdir(rfile):
+                    shutil.rmtree(rfile)
+                else:
+                    os.remove(rfile)
+
+        # Get target version number
+        version = None
+        with open(join(core_dir, "Changelog")) as f:
+            for line in f.readlines():
+                if line.startswith("* gosa"):
+                    version = line.split(" ")[2].strip()
+                    break
+
+        # Build gosa-core tar file
+        os.chdir(tmp_dir)
+        tar_create(join(target_dir, "gosa_%s.orig.tar.gz" % version), "gosa")
+
+        # Clean up and build plugin archives
+        tars = []
+        os.chdir(join(tmp_dir, "gosa-plugins"))
+        for f in os.listdir(join(tmp_dir, "gosa-plugins")):
+            pth = join(target_dir, "gosa_%s.orig-%s.tar.gz" % (version, f))
+            if os.path.exists(join(tmp_dir, "gosa-plugins", f, "plugin.dsc")):
+                os.remove(join(tmp_dir, "gosa-plugins", f, "plugin.dsc"))
+            tar_create(pth, f)
+            tars.append(pth)
+
+        # Stage build directory
+        tar_extract(join(target_dir, "gosa_%s.orig.tar.gz" % version), target_dir)
+        for tar_file in tars:
+            tar_extract(tar_file, join(target_dir, "gosa"))
+
+        # Build packages
+        print("Building packages to %s" % target_dir)
+        os.chdir(join(target_dir, "gosa"))
+        if key_id:
+            status = subprocess.call(["dpkg-buildpackage", "-k", key_id, "-rfakeroot"])
+        else:
+            status = subprocess.call(["dpkg-buildpackage", "-uc", "-us", "-rfakeroot"])
+
+        # Bail out?
+        if status:
+            raise Exception("Build failed: exit code %s" % status)
+
+    except Exception as detail:
+        print("Error:", str(detail))
+
+    # Cleanup
+    finally:
+        if cleanup:
+            if os.path.exists(tmp_dir):
+                shutil.rmtree(tmp_dir)
+            if os.path.exists(join(target_dir, "gosa")):
+                shutil.rmtree(join(target_dir, "gosa"))
+
+def main():
+    # Sanity check
+    for cli, pkg in [("/usr/bin/dpkg-buildpackage", "dpkg-dev"),
+        ("/usr/bin/fakeroot", "fakeroot"),
+        ("/usr/bin/dch", "devscripts"),
+        ("/usr/bin/dh", "debhelper")]:
+
+        if not os.path.exists(cli):
+            print("Error: %s is missing, please install %s" % (cli, pkg))
+            exit(1)
+
+    # Parse commandline options
+    op = OptionParser(usage="%prog - GOsa packaging aid",
+        version="%prog " + VERSION)
+    op.add_option("-t", "--target", dest="target",
+        default="/tmp/gosa",
+        help="target directory [%default]", metavar="PATH")
+    op.add_option("-n", "--no-cleanup", action="store_false",
+        dest="cleanup", default=True,
+        help="don't clean up temporary files")
+    op.add_option("-k", "--key-id", dest="key_id",
+        default=None,
+        help="GPG key ID used for signing if applicable", metavar="KEY_ID")
+
+    (options, args) = op.parse_args()
+
+    # Start build
+    build(options.target, options.key_id, options.cleanup)
+
+
+""" Main GOsa packaging aid """
+if __name__ == '__main__':
+    if not sys.stdout.encoding:
+        sys.stdout = codecs.getwriter('utf8')(sys.stdout)
+    if not sys.stderr.encoding:
+        sys.stderr = codecs.getwriter('utf8')(sys.stderr)
+
+    main()