Revamp + add 'list' command

To list all apps and make use of the new Persoconf object.
master
Lertsenem 2015-09-14 13:03:30 +02:00
parent 8a9d2e60d0
commit 67f3d16b06
1 changed files with 56 additions and 25 deletions

View File

@ -8,6 +8,8 @@ import time
import tarfile
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
from persoconf import Persoconf, FileInsteadOfDirError, \
PersoconfRootDoesNotExistError
# FUNCTIONS
########################################
@ -38,7 +40,8 @@ def contractuser(path):
home = os.path.expanduser("~")
if path.startswith(home):
return path.replace(home, "~", 1) # Replace only one instance: the first one
return path.replace(home, "~", 1) # Replace only one instance:
# the first one
else:
return path
@ -163,6 +166,8 @@ def copy_file_or_directory(logger, path_dst, path_src, overwrite=True):
# The End.
return True
# ARGPARSE
########################################
# Argument parsing using argparse
@ -177,6 +182,9 @@ subparsers = parser.add_subparsers( dest="command", title="commands") #, descrip
# Help
parser_help = subparsers.add_parser("help", help="Print this help")
# List
parser_list = subparsers.add_parser("list", help="List backuped apps")
# 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=[] )
@ -226,15 +234,22 @@ if args.command in [ "help", None ]:
parser.print_help()
exit()
# Let's check that the rootdir exists
if not os.path.isdir(args.rootdir):
# Try to create the Persoconf
try:
persoconf = Persoconf(path=args.rootdir, metafile=args.metafile)
if os.path.exists(args.rootdir):
# If the place is already taken, there's nothing we can do
except FileInsteadOfDirError:
log.error("'%s' exists but is a file" % str(args.rootdir))
# Otherwise, maybe we can create the directory ?
except PersoconfRootDoesNotExistError:
log.warning("'%s' does not exist" % str(args.rootdir))
if not yes_no_question( "Do you want to create the persoconf directory '%s'?" % str(args.rootdir) ) :
# Ask before creating the directory
if not yes_no_question( "Do you want to create the persoconf directory " \
"'%s'?"
% str(args.rootdir) ) :
log.info("The persoconf directory was not created")
exit()
@ -242,14 +257,30 @@ if not os.path.isdir(args.rootdir):
log.info("Creating persoconf directory...")
os.mkdir(args.rootdir)
print("New persoconf directory created at '%s'" % args.rootdir)
log.info("New persoconf directory created at '%s'" % args.rootdir)
# Finally retry to create the Persoconf
try:
persoconf = Persoconf(args.rootdir, metafile=args.metafile)
except Error as err:
log.error("Something very unexpected occured, I'm sorry")
log.error("Error dump follows")
log.error(str(err))
exit()
# LIST COMMAND
########################################
if args.command == "list":
log.info("Listing all apps")
for app in persoconf.list_apps(logger=log):
print(app)
# ADD COMMAND
########################################
if args.command == "add":
# Check (and create) the app directory
appdir = args.rootdir + "/" + args.app
appdir = persoconf.path + "/" + args.app
if os.path.isdir(appdir):
if args.conf is None:
@ -281,15 +312,15 @@ if args.command == "add":
# Load app META file
try:
appmeta = Metafile(json_path = appdir + "/" + args.metafile)
appmeta = Metafile(json_path = appdir + "/" + persoconf.metafile)
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (args.metafile, args.app))
log.info("Metafile %s does not exist for app %s" % (persoconf.metafile, args.app))
appmeta = Metafile( name=args.app )
appmeta.json_path = "/".join( [args.rootdir, args.app, args.metafile] )
appmeta.json_path = "/".join( [persoconf.path, args.app, persoconf.metafile] )
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
exit(1)
# Check that the file is really new
@ -299,7 +330,7 @@ if args.command == "add":
dstdic = appmeta.get_files_dst2src()
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
exit(1)
if absconf in dstdic:
@ -333,15 +364,15 @@ 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]))
appmeta = Metafile(json_path = "/".join([persoconf.path, args.app, persoconf.metafile]))
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (args.metafile, args.app))
log.info("Metafile %s does not exist for app %s" % (persoconf.metafile, args.app))
appmeta = Metafile( name=args.app )
appmeta.json_path = "/".join( [args.rootdir, args.app, args.metafile] )
appmeta.json_path = "/".join( [persoconf.path, args.app, persoconf.metafile] )
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
exit(1)
# TODO check that the app exists
@ -355,7 +386,7 @@ if args.command == "update":
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"]):
if copy_file_or_directory(log, path_dst='/'.join([persoconf.path, 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"]))
@ -364,7 +395,7 @@ if args.command == "update":
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):
if copy_file_or_directory(log, path_dst='/'.join([persoconf.path, 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))
@ -376,7 +407,7 @@ if args.command == "update":
# 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"]):
if copy_file_or_directory(log, path_dst='/'.join([persoconf.path, 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"]))
@ -392,14 +423,14 @@ if args.command == "package":
for app in args.apps :
# Load app META file
try:
appmetas.append( Metafile(json_path = "/".join([args.rootdir, app, args.metafile])) )
appmetas.append( Metafile(json_path = "/".join([persoconf.path, app, persoconf.metafile])) )
except FileNotFoundError:
log.warning("Metafile %s does not exist for app %s; ignoring" % (args.metafile, app))
log.warning("Metafile %s does not exist for app %s; ignoring" % (persoconf.metafile, app))
continue
except MalformedMetafileError:
log.warning("Malformed metafile %s in app %s; ignoring" % (args.metafile, app) )
log.warning("Malformed metafile %s in app %s; ignoring" % (persoconf.metafile, app) )
continue
# TODO check that the app exists
@ -418,7 +449,7 @@ if args.command == "package":
for appmeta in appmetas:
for f in appmeta.files:
filename = args.rootdir +"/"+ appmeta.name +"/"+ f
filename = persoconf.path +"/"+ appmeta.name +"/"+ f
filedest = appmeta.files[f]["dest"]
# Remove possible 'home'. The final targz should be extracted