From 20b0632d0b3f4ef60caba4955ae70d39a1908749 Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Sat, 12 Sep 2015 18:54:29 +0200 Subject: [PATCH] Basic package command for 'tgz' --- persoconf/main.py | 51 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/persoconf/main.py b/persoconf/main.py index 3887468..ab3e8c5 100755 --- a/persoconf/main.py +++ b/persoconf/main.py @@ -4,6 +4,8 @@ import argparse import logging import os, os.path import shutil +import time +import tarfile from metafile import Metafile, MalformedMetafileError, NoPathDefinedError @@ -178,11 +180,12 @@ parser_help = subparsers.add_parser("help", help="Print this help") # Package parser_package = subparsers.add_parser("package", help="Package a persoconf repository") parser_package.add_argument( "apps", type=str, help="The apps to package ; defaults to all apps", nargs="*", default=[] ) -parser_package.add_argument( "type", type=str, help="The type of package to use", nargs="?", default="tar", choices = [ "tar" ] ) +parser_package.add_argument( "--pkgtype", "-t", type=str, help="The type of package to use", nargs="?", default="tar", choices = [ "tar" ] ) +parser_package.add_argument( "--pkgname", "-p", type=str, help="The name of the package to create", nargs="?", default="persoconf." + (time.strftime("%Y%m%d")) ) # Update parser_update = subparsers.add_parser("update", help="Backup an app configuration in the persoconf directory with the configuration in use now") -parser_update.add_argument( 'app', help='The apps to backup', type=str ) +parser_update.add_argument( 'app', help='The app to update', type=str ) parser_update.add_argument( 'files', help='The files to update ; default to all added files', type=str, nargs="*", default=[]) # Add @@ -380,5 +383,49 @@ if args.command == "update": # PACKAGE COMMAND ######################################## +if args.command == "package": + + pkgname = args.pkgname + + # TODO app == None => package all apps + # Load app META file + try: + appmeta = Metafile(json_path = "/".join([args.rootdir, args.apps[0], args.metafile])) + + except FileNotFoundError: + log.error("Metafile %s does not exist for app %s" % (args.metafile, args.apps[0])) + exit(1) + + except MalformedMetafileError: + log.error("Malformed metafile %s in app %s" % (args.metafile, args.apps[0]) ) + exit(1) + + # TODO check that the app exists + pkgname += "." + pkgname += appmeta.name + + if args.pkgtype == "tar": + + pkgname += ".tgz" + + # Create a tar containing the files in the right place + pkg = tarfile.open(pkgname, "w:gz") + + for f in appmeta.files: + # TODO remove leading '~' + # TODO save and restore owners and permissions + try: + pkg.add(args.rootdir +"/"+ appmeta.name +"/"+ f, arcname=appmeta.files[f]["dest"]) + log.info("Adding %s to package" % f) + except: + log.warning("Failed to add %s to tar package %s" % (f, pkgname)) + + + pkg.close() + + + + + ########################################