Move update command in commands module

Plus corrections (os.path.join)
master
Lertsenem 2015-12-07 17:24:59 +01:00
parent 20f000f3b8
commit cd848ff88a
2 changed files with 137 additions and 117 deletions

View File

@ -0,0 +1,134 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os.path
import utils
# --------------------------------------
def init(parser):
parser.add_argument( "app" ,
type=str ,
nargs="?" ,
help="The app to update" )
parser.add_argument( "files" ,
type=str ,
nargs="*" ,
default=[] ,
help="The files to update ; default to all " \
"added files" )
# --------------------------------------
def run(args, persoconf, logger):
# app == None => update all apps
apps_to_update = {}
if args.app is None:
apps_to_update = persoconf.list_apps(logger=logger)
else:
try:
appmeta = Metafile(
json_path = os.path.join( persoconf.path ,
args.app ,
persoconf.metafile )
)
apps_to_update[appmeta.name] = appmeta
except FileNotFoundError:
logger.error( "Metafile %s does not exist for app %s"
% (persoconf.metafile, args.app) )
exit(1)
except MalformedMetafileError:
logger.error( "Malformed metafile %s in app %s"
% (persoconf.metafile, args.app) )
exit(1)
for app in apps_to_update:
if args.files != []:
dstdic = apps_to_update[app].get_files_dst2src() # Get that here
# for optimization
# 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.
for f in args.files:
# 1) It's probably a confname
if f in apps_to_update[app].files:
if utils.copy_file_or_directory(
logger ,
path_dst = os.path.join( persoconf.path,
app ,
f ) ,
path_src = apps_to_update[app].files[f]["dest"]
):
logger.info( "Updated %s from %s"
% (f, apps_to_update[app].files[f]["dest"]) )
else:
logger.warning(
"Failed to update %s from %s ; ignoring"
% ( f, apps_to_update[app].files[f]["dest"])
)
continue
# 2) If not, it must be a real filename
else:
absf = contractuser(os.path.abspath(os.path.expanduser(f)))
if absf in dstdic:
if utils.copy_file_or_directory(
logger ,
path_dst = os.path.join( persoconf.path,
app ,
dstdic[absf] ) ,
path_src = absf
):
logger.info( "Updated %s from %s"
% (dstdic[absf], absf) )
else:
logger.warning( "Failed to update %s from %s ; " \
"ignoring"
% (dstdic[absf], absf) )
continue
# 3) Otherwise, no idea what it is
else:
logger.warning( "Cannot find file %s in app data; " \
"ignoring it"
% f )
continue
# If they were no 'file' args, it means we need to update all files in
# the app
else:
for f in apps_to_update[app].files:
if utils.copy_file_or_directory(
logger ,
path_dst = os.path.join( persoconf.path,
app ,
f ) ,
path_src = apps_to_update[app].files[f]["dest"]
):
logger.info( "Updated %s from %s"
% (f, apps_to_update[app].files[f]["dest"]) )
else:
logger.warning( "Failed to update %s from %s ; ignoring"
% ( f ,
apps_to_update[app].files[f]["dest"] )
)

View File

@ -15,7 +15,7 @@ from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
from persoconf import Persoconf, FileInsteadOfDirError, \
PersoconfRootDoesNotExistError
import commands.list, commands.package
import commands.list, commands.package, commands.update
@ -61,18 +61,7 @@ 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" ,
type=str ,
nargs="?" ,
help="The app to update" )
parser_update.add_argument( "files" ,
type=str ,
nargs="*" ,
default=[] ,
help="The files to update ; default to all " \
"added files" )
commands.update.init(parser_update)
# Add
parser_add = subparsers.add_parser( "add" ,
@ -376,110 +365,7 @@ if args.command == "add":
# UPDATE COMMAND
########################################
if args.command == "update":
# app == None => update all apps
apps_to_update = {}
if args.app is None:
apps_to_update = persoconf.list_apps(logger=log)
else:
try:
appmeta = Metafile( json_path = persoconf.path +"/"+
args.app +"/"+
persoconf.metafile )
apps_to_update[appmeta.name] = appmeta
except FileNotFoundError:
log.error( "Metafile %s does not exist for app %s"
% (persoconf.metafile, args.app) )
exit(1)
except MalformedMetafileError:
log.error( "Malformed metafile %s in app %s"
% (persoconf.metafile, args.app) )
exit(1)
for app in apps_to_update:
if args.files != []:
dstdic = apps_to_update[app].get_files_dst2src() # Get that here
# for optimization
# 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.
for f in args.files:
# 1) It's probably a confname
if f in apps_to_update[app].files:
if copy_file_or_directory(
log ,
path_dst = persoconf.path
+"/"+ app
+"/"+ f ,
path_src = apps_to_update[app].files[f]["dest"]
):
log.info( "Updated %s from %s"
% (f, apps_to_update[app].files[f]["dest"]) )
else:
log.warning(
"Failed to update %s from %s ; ignoring"
% ( f, apps_to_update[app].files[f]["dest"])
)
continue
# 2) 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 = persoconf.path
+"/"+ 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) )
continue
# 3) Otherwise, no idea what it is
else:
log.warning( "Cannot find file %s in app data; " \
"ignoring it"
% f )
continue
# If they were no 'file' args, it means we need to update all files in
# the app
else:
for f in apps_to_update[app].files:
if copy_file_or_directory(
log ,
path_dst = persoconf.path
+"/"+ app
+"/"+ f ,
path_src = apps_to_update[app].files[f]["dest"]
):
log.info( "Updated %s from %s"
% (f, apps_to_update[app].files[f]["dest"]) )
else:
log.warning( "Failed to update %s from %s ; ignoring"
% (f, apps_to_update[app].files[f]["dest"]) )
commands.update.run(args, persoconf, log)
# PACKAGE COMMAND
########################################