From 625adfe5ef2efe97ac1e0991e2c063b09469a6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Touceda?= Date: Wed, 4 Jan 2012 13:17:51 -0300 Subject: Add helper scripts to improve package creation ConfigCLI: handles package and bundle config creation ThpHelper: handles creating thp config files --- lib/thandy/ConfigCLI.py | 118 +++++++++++++++++++++++++++++++++++++++++++++ lib/thandy/ThpHelper.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 lib/thandy/ConfigCLI.py create mode 100644 lib/thandy/ThpHelper.py diff --git a/lib/thandy/ConfigCLI.py b/lib/thandy/ConfigCLI.py new file mode 100644 index 0000000..a89039e --- /dev/null +++ b/lib/thandy/ConfigCLI.py @@ -0,0 +1,118 @@ +from string import Template +import sys +import getopt + +package_template = Template("""name = "$app_name" +version = [$version_list] +location = "$location" +relpath = "$data" +ShortDesc('en', "$short_desc") +LongDesc('en', +\"\"\"$long_desc\"\"\") +format = "$format" +thp_name = "$app_name" +thp_version = "$version" +thp_dest = "$dest" """) + +bundle_template = Template("""name = "$bundle_name" +version = [$version_list] +location = "$bundle_location" +os = "$os" +arch = "$arch" +$packages +ShortGloss("en", "$short_gloss") +LongGloss("en", "$long_gloss") """) + +package_list_template = Template("""Package(name="$app_name", + order=(10,10,10), + optional=False)""") + +def packageconfig(args): + optlist, args = getopt.getopt(args, '', ["app_name=", + "version_list=", + "location=", + "short_desc=", + "long_desc=", + "dest="]) + + mapping = {} + for key, val in optlist: + mapping[key[2:]] = val + + mapping["version"] = ".".join(mapping["version_list"].split(",")) + mapping["data"] = "/data/%s-%s.thp" % (mapping["app_name"], + mapping["version"]) + mapping["format"] = "thp" + + out = open("%s-%s_package.cfg" % (mapping["app_name"], mapping["version"]), "w") + try: + out.write(package_template.substitute(mapping)) + except KeyError, e: + print "You are missing the following parameter:", e + sys.exit(1) + + out.close() + +def bundleconfig(args): + optlist, args = getopt.getopt(args, '', ["bundle_name=", + "version_list=", + "bundle_location=", + "os=", + "arch=", + "pkg_names=", + "short_gloss=", + "long_gloss="]) + + mapping = {} + pkg_names = [] + for key, val in optlist: + if key == "--pkg_names": + pkg_names = val.split(",") + continue + mapping[key[2:]] = val + + packages = "" + for pkg in pkg_names: + packages += "%s\n" % package_list_template.substitute({'app_name': pkg}) + + mapping["packages"] = packages + + out = open("%s-%s_bundle.cfg" % (mapping["bundle_name"], ".".join(mapping["version_list"].split(","))), "w") + try: + out.write(bundle_template.substitute(mapping)) + except KeyError, e: + print "You are missing the following parameter:", e + sys.exit(1) + + out.close() + +def usage(): + print "Known commands:" + print " packageconfig --app_name=AppName" + print " --version_list=1,2,3" + print " --location=/path/to/package.txt" + print " --short_desc=\"Short description\"" + print " --long_desc=\"Long description\"" + print " --dest=\"/relative/path/to/install/\"" + print " bundleconfig --bundle_name=BundleName" + print " --version_list=1,2,3" + print " --bundle_location=/path/to/bundle.txt" + print " --os=lin" + print " --arch=x86" + print " --pkg_names=\"package1,package2\"" + print " --short_gloss=\"This is the short gloss\"" + print " --long_gloss=\"This is the large glossary\"" + sys.exit(1) + +def main(): + if len(sys.argv) < 2: + usage() + cmd = sys.argv[1] + args = sys.argv[2:] + if cmd in [ "packageconfig", "bundleconfig" ]: + globals()[cmd](args) + else: + usage() + +if __name__ == '__main__': + main() diff --git a/lib/thandy/ThpHelper.py b/lib/thandy/ThpHelper.py new file mode 100644 index 0000000..44ccb23 --- /dev/null +++ b/lib/thandy/ThpHelper.py @@ -0,0 +1,125 @@ +import os +import sys +import getopt + +from difflib import unified_diff +from string import Template + +thp_template = Template("""format_version = 1 +package_name = "$thp_name" +package_version = "$version" +package_version_tuple = [$version_list] +files = [ +$files +] + +additional_files = [ ] +install_order = 50 +options = { "cycle-install" : False } +platform = { "os" : "$os", + "arch" : "$arch" } +require_features = [ "pythonscripts" ] +require_packages = [] +scripts = { "python2" : + [ $scripts ] + } +""") + +def usage(): + print "Known commands:" + print " thpconfig --thp_name=AppName" + print " --version_list=1,2,3" + print " --scan=path/to/folder/" + print " --os=linux" + print " --arch=x86" + print " --scripts=\"['script1.py', ['preinst', 'postinst']],['script2.py', ['postinst']]\"" + print " --old_file_list=path/to/oldfiles" + print " --generate_file_list=0|1" + print " --config_file_list=path/to/configfiles" + sys.exit(1) + +def get_files(top, configs): + ready = [] + raw = [] + for root, dirs, files in os.walk(top): + for f in files: + is_config = "False" + f_value = "/".join([root, f]).replace(top, "") + if f_value in configs: + is_config = "True" + ready.append("(\"%s\", %s)," % (f_value, is_config)) + raw.append("/".join([root, f]).replace(top, "")) + return ready, raw + +def thpconfig(args): + optlist, args = getopt.getopt(args, '', ["thp_name=", + "version_list=", + "scan=", + "os=", + "arch=", + "scripts=", + "old_file_list=", + "generate_file_list=", + "config_file_list="]) + + mapping = {} + scan_dir = "" + old_file_list = "" + config_file_list = "" + generate_file_list = True + for key, val in optlist: + if key == "--scan": + scan_dir = val + continue + if key == "--generate_file_list": + generate_file_list = (val == "1") + continue + if key == "--old_file_list": + old_file_list = val + continue + if key == "--config_file_list": + config_file_list = val + continue + mapping[key[2:]] = val + + mapping["version"] = ".".join(mapping["version_list"].split(",")) + + configs = [] + if len(config_file_list) != 0: + configs = open(config_file_list, "r").read().split("\n") + + files, raw = get_files(scan_dir, configs) + mapping["files"] = "\n".join(files) + + out = open("%s-%s_thp.cfg" % (mapping["thp_name"], mapping["version"]), "w") + try: + out.write(thp_template.substitute(mapping)) + except KeyError, e: + print "You are missing the following parameter:", e + sys.exit(1) + out.close() + + if generate_file_list: + file_list = open("%s-%s_thp.filelist" % (mapping["thp_name"], mapping["version"]), "w") + file_list.write("\n".join(raw)) + file_list.close() + + if len(old_file_list) != 0: + old = open(old_file_list, "r").read().split("\n") + new = raw + for line in unified_diff(old, new): + print line + +def main(): + if len(sys.argv) < 2: + usage() + cmd = sys.argv[1] + args = sys.argv[2:] + if cmd in [ "thpconfig" ]: + globals()[cmd](args) + else: + usage() + + +if __name__ == "__main__": + main() -- cgit v1.2.3