From dcc0625b3cf306c2e0b2790f85c742a6c4c47f74 Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Thu, 20 Aug 2015 01:07:51 +0200 Subject: [PATCH] Adding 'update' command --- persoconf/main.py | 66 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/persoconf/main.py b/persoconf/main.py index f17e54b..53ce1a5 100755 --- a/persoconf/main.py +++ b/persoconf/main.py @@ -177,11 +177,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. 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 -parser_backup = subparsers.add_parser("backup", 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"]) +# 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( 'files', help='The files to update ; default to all added files', type=str, nargs="*", default=[]) # Add 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) 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) - -