Revamp + add 'list' command
To list all apps and make use of the new Persoconf object.master
parent
8a9d2e60d0
commit
67f3d16b06
|
@ -7,7 +7,9 @@ import shutil
|
||||||
import time
|
import time
|
||||||
import tarfile
|
import tarfile
|
||||||
|
|
||||||
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
|
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
|
||||||
|
from persoconf import Persoconf, FileInsteadOfDirError, \
|
||||||
|
PersoconfRootDoesNotExistError
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
########################################
|
########################################
|
||||||
|
@ -38,7 +40,8 @@ def contractuser(path):
|
||||||
home = os.path.expanduser("~")
|
home = os.path.expanduser("~")
|
||||||
|
|
||||||
if path.startswith(home):
|
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:
|
else:
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
@ -163,6 +166,8 @@ def copy_file_or_directory(logger, path_dst, path_src, overwrite=True):
|
||||||
# The End.
|
# The End.
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ARGPARSE
|
# ARGPARSE
|
||||||
########################################
|
########################################
|
||||||
# Argument parsing using argparse
|
# Argument parsing using argparse
|
||||||
|
@ -177,6 +182,9 @@ subparsers = parser.add_subparsers( dest="command", title="commands") #, descrip
|
||||||
# Help
|
# Help
|
||||||
parser_help = subparsers.add_parser("help", help="Print this help")
|
parser_help = subparsers.add_parser("help", help="Print this help")
|
||||||
|
|
||||||
|
# List
|
||||||
|
parser_list = subparsers.add_parser("list", help="List backuped apps")
|
||||||
|
|
||||||
# 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 ; defaults to all apps", nargs="*", default=[] )
|
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()
|
parser.print_help()
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
# Let's check that the rootdir exists
|
# Try to create the Persoconf
|
||||||
if not os.path.isdir(args.rootdir):
|
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
|
||||||
log.error("'%s' exists but is a file" % str(args.rootdir))
|
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))
|
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")
|
log.info("The persoconf directory was not created")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
@ -242,14 +257,30 @@ if not os.path.isdir(args.rootdir):
|
||||||
log.info("Creating persoconf directory...")
|
log.info("Creating persoconf directory...")
|
||||||
os.mkdir(args.rootdir)
|
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
|
# ADD COMMAND
|
||||||
########################################
|
########################################
|
||||||
if args.command == "add":
|
if args.command == "add":
|
||||||
|
|
||||||
# Check (and create) the app directory
|
# Check (and create) the app directory
|
||||||
appdir = args.rootdir + "/" + args.app
|
appdir = persoconf.path + "/" + args.app
|
||||||
|
|
||||||
if os.path.isdir(appdir):
|
if os.path.isdir(appdir):
|
||||||
if args.conf is None:
|
if args.conf is None:
|
||||||
|
@ -281,15 +312,15 @@ if args.command == "add":
|
||||||
|
|
||||||
# Load app META file
|
# Load app META file
|
||||||
try:
|
try:
|
||||||
appmeta = Metafile(json_path = appdir + "/" + args.metafile)
|
appmeta = Metafile(json_path = appdir + "/" + persoconf.metafile)
|
||||||
|
|
||||||
except FileNotFoundError:
|
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 = 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:
|
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)
|
exit(1)
|
||||||
|
|
||||||
# Check that the file is really new
|
# Check that the file is really new
|
||||||
|
@ -299,7 +330,7 @@ if args.command == "add":
|
||||||
dstdic = appmeta.get_files_dst2src()
|
dstdic = appmeta.get_files_dst2src()
|
||||||
|
|
||||||
except MalformedMetafileError:
|
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)
|
exit(1)
|
||||||
|
|
||||||
if absconf in dstdic:
|
if absconf in dstdic:
|
||||||
|
@ -333,15 +364,15 @@ if args.command == "update":
|
||||||
# TODO app == None => update all apps
|
# TODO app == None => update all apps
|
||||||
# Load app META file
|
# Load app META file
|
||||||
try:
|
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:
|
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 = 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:
|
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)
|
exit(1)
|
||||||
|
|
||||||
# TODO check that the app exists
|
# TODO check that the app exists
|
||||||
|
@ -355,7 +386,7 @@ if args.command == "update":
|
||||||
for f in args.files:
|
for f in args.files:
|
||||||
# It's probably a confname
|
# It's probably a confname
|
||||||
if f in appmeta.files:
|
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"]))
|
log.info("Updated %s from %s" % (f, appmeta.files[f]["dest"]))
|
||||||
else:
|
else:
|
||||||
log.warning("Failed to update %s from %s ; ignoring" % (f, appmeta.files[f]["dest"]))
|
log.warning("Failed to update %s from %s ; ignoring" % (f, appmeta.files[f]["dest"]))
|
||||||
|
@ -364,7 +395,7 @@ if args.command == "update":
|
||||||
else:
|
else:
|
||||||
absf = contractuser( os.path.abspath(os.path.expanduser(f)) )
|
absf = contractuser( os.path.abspath(os.path.expanduser(f)) )
|
||||||
if absf in dstdic:
|
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))
|
log.info("Updated %s from %s" % (dstdic[absf], absf))
|
||||||
else:
|
else:
|
||||||
log.warning("Failed to update %s from %s ; ignoring" % (dstdic[absf], absf))
|
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
|
# If they were no 'file' args, it means we need to update all files in the app
|
||||||
else:
|
else:
|
||||||
for f in appmeta.files:
|
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"]))
|
log.info("Updated %s from %s" % (f, appmeta.files[f]["dest"]))
|
||||||
else:
|
else:
|
||||||
log.warning("Failed to update %s from %s ; ignoring" % (f, appmeta.files[f]["dest"]))
|
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 :
|
for app in args.apps :
|
||||||
# Load app META file
|
# Load app META file
|
||||||
try:
|
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:
|
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
|
continue
|
||||||
|
|
||||||
except MalformedMetafileError:
|
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
|
continue
|
||||||
|
|
||||||
# TODO check that the app exists
|
# TODO check that the app exists
|
||||||
|
@ -418,7 +449,7 @@ if args.command == "package":
|
||||||
|
|
||||||
for appmeta in appmetas:
|
for appmeta in appmetas:
|
||||||
for f in appmeta.files:
|
for f in appmeta.files:
|
||||||
filename = args.rootdir +"/"+ appmeta.name +"/"+ f
|
filename = persoconf.path +"/"+ appmeta.name +"/"+ f
|
||||||
filedest = appmeta.files[f]["dest"]
|
filedest = appmeta.files[f]["dest"]
|
||||||
|
|
||||||
# Remove possible 'home'. The final targz should be extracted
|
# Remove possible 'home'. The final targz should be extracted
|
||||||
|
|
Loading…
Reference in New Issue