Adding 'update' command

master
Lertsenem 2015-08-20 01:07:51 +02:00
parent 33b0d49970
commit dcc0625b3c
1 changed files with 59 additions and 7 deletions

View File

@ -177,11 +177,12 @@ parser_help = subparsers.add_parser("help", help="Print this help")
# Package # Package
parser_package = subparsers.add_parser("package", help="Package a persoconf repository") parser_package = subparsers.add_parser("package", help="Package a persoconf repository")
parser_package.add_argument( "apps", type=str, help="The apps to package. Use all to make a global package.", nargs="*", default=["all"] ) parser_package.add_argument( "apps", type=str, help="The apps to package ; defaults to all apps", nargs="*", default=[] )
# Backup # Update
parser_backup = subparsers.add_parser("backup", help="Backup an app configuration in the persoconf directory with the configuration in use now") parser_update = subparsers.add_parser("update", help="Backup an app configuration in the persoconf directory with the configuration in use now")
parser_backup.add_argument( 'apps', help='The apps to backup', type=str, nargs="*", default=["all"]) parser_update.add_argument( 'app', help='The apps to backup', type=str )
parser_update.add_argument( 'files', help='The files to update ; default to all added files', type=str, nargs="*", default=[])
# Add # Add
parser_add = subparsers.add_parser("add", help="Add an app to the persoconf directory, or add a config file to an existing app") parser_add = subparsers.add_parser("add", help="Add an app to the persoconf directory, or add a config file to an existing app")
@ -321,8 +322,59 @@ if args.command == "add":
log.error("Unable to save json Metafile for app %s : no path defined" % appmeta.name) log.error("Unable to save json Metafile for app %s : no path defined" % appmeta.name)
exit(1) exit(1)
# UPDATE COMMAND
########################################
if args.command == "update":
# TODO app == None => update all apps
# Load app META file
try:
appmeta = Metafile(json_path = "/".join([args.rootdir, args.app, args.metafile]))
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (args.metafile, args.app))
appmeta = Metafile( name=args.app )
appmeta.json_path = "/".join( [args.rootdir, args.app, args.metafile] )
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
exit(1)
# TODO check that the app exists
# The filenames can be confnames or real files names. For each we'll assume
# first that it's a confname, then that it's a file name if that fails.
if args.files != []:
dstdic = appmeta.get_files_dst2src()
for f in args.files:
# It's probably a confname
if f in appmeta.files:
if copy_file_or_directory(log, path_dst='/'.join([args.rootdir, args.app, f]), path_src=appmeta.files[f]["dest"]):
log.info("Updated %s from %s" % (f, appmeta.files[f]["dest"]))
else:
log.warning("Failed to update %s from %s ; ignoring" % (f, appmeta.files[f]["dest"]))
# If not, it must be a real filename
else:
absf = contractuser( os.path.abspath(os.path.expanduser(f)) )
if absf in dstdic:
if copy_file_or_directory(log, path_dst='/'.join([args.rootdir, args.app, dstdic[absf]]), path_src=absf):
log.info("Updated %s from %s" % (dstdic[absf], absf))
else:
log.warning("Failed to update %s from %s ; ignoring" % (dstdic[absf], absf))
# Otherwise, it's nothing.
else:
log.warning("Cannot find file %s in app data; ignoring it")
# If they were no 'file' args, it means we need to update all files in the app
else:
for f in appmeta.files:
if copy_file_or_directory(log, path_dst='/'.join([args.rootdir, args.app, f]), path_src=appmeta.files[f]["dest"]):
log.info("Updated %s from %s" % (f, appmeta.files[f]["dest"]))
else:
log.warning("Failed to update %s from %s ; ignoring" % (f, appmeta.files[f]["dest"]))
######################################## ########################################
print(args)