Basic package command for 'tgz'

master
Lertsenem 2015-09-12 18:54:29 +02:00
parent 631a0b8028
commit 20b0632d0b
1 changed files with 49 additions and 2 deletions

View File

@ -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()
########################################