From eab5121d97a93de91f31d734f9f3b17f3f0fcfec Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Mon, 7 Dec 2015 16:48:01 +0100 Subject: [PATCH] Move package command in commands module --- persoconf/commands/package.py | 90 +++++++++++++++++++++++++++++++++++ persoconf/main.py | 80 ++----------------------------- 2 files changed, 93 insertions(+), 77 deletions(-) create mode 100644 persoconf/commands/package.py diff --git a/persoconf/commands/package.py b/persoconf/commands/package.py new file mode 100644 index 0000000..15ed770 --- /dev/null +++ b/persoconf/commands/package.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import time +import tarfile + +from metafile import Metafile, MalformedMetafileError + +# -------------------------------------- +def init(parser): + + parser.add_argument( "apps" , + type=str , + nargs="*" , + default=[] , + help="The apps to package ; defaults to all " \ + "apps" ) + + parser.add_argument( "--pkgtype", "-t" , + type=str , + nargs="?" , + default="tgz" , + choices = [ "tgz" ] , + help="The type of package to use" ) + + parser.add_argument( "--pkgname", "-p" , + type=str , + nargs="?" , + default="persoconf." + (time.strftime("%Y%m%d")) , + help="The name of the package to create" ) + +# -------------------------------------- +def run(args, persoconf, logger): + + pkgname = args.pkgname + + # If no app is given: package all apps + if args.apps == []: + logger.info("Packaging all apps") + args.apps = persoconf.list_apps(logger=logger) + + # Some apps were specified, so we list them with their files + appmetas = [] + for app in args.apps : + # Load app META file + try: + appmetas.append( Metafile(json_path = "/".join([persoconf.path, app, persoconf.metafile])) ) + + except FileNotFoundError: + logger.warning("Metafile %s does not exist for app %s; ignoring" % (persoconf.metafile, app)) + continue + + except MalformedMetafileError: + logger.warning("Malformed metafile %s in app %s; ignoring" % (persoconf.metafile, app) ) + continue + + # TODO check that the app exists + + # Adds the app name if there is only one packaged + if len(appmetas) == 1: + pkgname += ("." + appmetas[0].name) + + # Switch according to the pkgtype + if args.pkgtype == "tgz": + + pkgname += ".tgz" + + # Create a tar containing the files in the right place + pkg = tarfile.open(pkgname, "w:gz") + + for appmeta in appmetas: + for f in appmeta.files: + filename = persoconf.path +"/"+ appmeta.name +"/"+ f + filedest = appmeta.files[f]["dest"] + + # Remove possible 'home'. The final targz should be extracted + # at 'home. + if filedest.startswith("~/"): + filedest = filedest[2:] + + # TODO save and restore owners and permissions + try: + pkg.add(filename, arcname=filedest) + logger.info("Adding %s to package" % f) + except: + logger.warning("Failed to add %s to tar package %s" % (f, pkgname)) + + + pkg.close() + diff --git a/persoconf/main.py b/persoconf/main.py index 6a482ce..3f562ef 100755 --- a/persoconf/main.py +++ b/persoconf/main.py @@ -15,7 +15,7 @@ from metafile import Metafile, MalformedMetafileError, NoPathDefinedError from persoconf import Persoconf, FileInsteadOfDirError, \ PersoconfRootDoesNotExistError -import commands.list +import commands.list, commands.package @@ -54,26 +54,7 @@ commands.list.init(parser_list) # Package parser_package = subparsers.add_parser( "package" , help="Package a persoconf repository" ) - -parser_package.add_argument( "apps" , - type=str , - nargs="*" , - default=[] , - help="The apps to package ; defaults to all " \ - "apps" ) - -parser_package.add_argument( "--pkgtype", "-t" , - type=str , - nargs="?" , - default="tgz" , - choices = [ "tgz" ] , - help="The type of package to use" ) - -parser_package.add_argument( "--pkgname", "-p" , - type=str , - nargs="?" , - default="persoconf." + (time.strftime("%Y%m%d")) , - help="The name of the package to create" ) +commands.package.init(parser_package) # Update parser_update = subparsers.add_parser( "update" , @@ -503,62 +484,7 @@ if args.command == "update": # PACKAGE COMMAND ######################################## if args.command == "package": - - pkgname = args.pkgname - - # If no app is given: package all apps - if args.apps == []: - log.info("Packaging all apps") - args.apps = persoconf.list_apps(logger=log) - - # Some apps were specified, so we list them with their files - appmetas = [] - for app in args.apps : - # Load app META file - try: - appmetas.append( Metafile(json_path = "/".join([persoconf.path, app, persoconf.metafile])) ) - - except FileNotFoundError: - log.warning("Metafile %s does not exist for app %s; ignoring" % (persoconf.metafile, app)) - continue - - except MalformedMetafileError: - log.warning("Malformed metafile %s in app %s; ignoring" % (persoconf.metafile, app) ) - continue - - # TODO check that the app exists - - # Adds the app name if there is only one packaged - if len(appmetas) == 1: - pkgname += ("." + appmeta[0].name) - - # Switch according to the pkgtype - if args.pkgtype == "tgz": - - pkgname += ".tgz" - - # Create a tar containing the files in the right place - pkg = tarfile.open(pkgname, "w:gz") - - for appmeta in appmetas: - for f in appmeta.files: - filename = persoconf.path +"/"+ appmeta.name +"/"+ f - filedest = appmeta.files[f]["dest"] - - # Remove possible 'home'. The final targz should be extracted - # at 'home. - if filedest.startswith("~/"): - filedest = filedest[2:] - - # TODO save and restore owners and permissions - try: - pkg.add(filename, arcname=filedest) - log.info("Adding %s to package" % f) - except: - log.warning("Failed to add %s to tar package %s" % (f, pkgname)) - - - pkg.close() + commands.package.run(args, persoconf, log)