persoconf/persoconf/main.py

595 lines
21 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import logging
import os, os.path
import shutil
import time
import tarfile
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
from persoconf import Persoconf, FileInsteadOfDirError, \
PersoconfRootDoesNotExistError
# FUNCTIONS
########################################
def yes_no_question(question="Yes or no?", default=False):
prompt=""
if(default is True):
prompt = " [Y/n]"
else:
prompt = " [y/N]"
answer = input(question + prompt).lower()
if(len(answer) == 0):
return default
else:
answer = answer[0]
if(default is True):
if answer in ['n','0']:
return False
else:
if answer in ['y','o','1']:
return True
return default
def contractuser(path):
home = os.path.expanduser("~")
if path.startswith(home):
return path.replace(home, "~", 1) # Replace only one instance:
# the first one
else:
return path
def copy_file_or_directory(logger, path_dst, path_src, overwrite=True):
# Expand '~' as user's home dir
dst = os.path.expanduser(path_dst)
src = os.path.expanduser(path_src)
if overwrite:
# --------------------------------------
def replace_backup():
try:
logger.info("Putting backup back in place")
shutil.move(dst + ".bak", dst)
except FileNotFoundError:
logger.error("No backup of %s found. Oops" % dst)
return False
except PermissionError:
logger.error("Cannot replace backup %s because of permissions"
% (dst + ".bak"))
return False
except FileExistsError:
logger.error("%s has been created, you need to chose " \
"manually to keep it or to use the backup %s"
% (dst, dst + ".bak"))
return False
return True
# --------------------------------------
try:
logger.info("Moving old directory %s" % dst)
shutil.move(dst, dst + ".bak")
except FileNotFoundError:
logger.info("No file %s exists yet" % dst)
except PermissionError:
logger.error("Unable to write %s, please check permissions"
% (dst + ".bak"))
return False
except FileExistsError:
logger.error("%s already exists" % (dst + ".bak"))
return False
# No overwrite ? No problem.
else:
def replace_backup():
return True
# Copy the conf dir
# --------------------------------------
try:
logger.info("Copying directory %s" % src)
shutil.copytree(src, dst)
except FileNotFoundError:
logger.error("%s does not seem to exist" % src)
replace_backup()
return False
except PermissionError:
logger.error("Unable to write %s, please check permissions" % dst)
replace_backup()
return False
except FileExistsError:
logger.error("The directory '%s' already exists" % dst)
replace_backup()
return False
# It's not a dir, it's a file !
# --------------------------------------
except NotADirectoryError:
# Try not to overwrite an existing file.
# Note that if 'overwrite' was set to True, any existing file would
# have been moved at this point.
if os.path.exists(dst):
logger.error("The file %s already exists" % dst)
replace_backup()
return False
try:
logger.info("Copying file %s" % src)
shutil.copyfile(src, dst)
except PermissionError:
logger.error("Unable to write %s, please check permissions" % dst)
replace_backup()
return False
# Everything went well, time to remove the backup
# --------------------------------------
if overwrite:
# As before, try it as a dir first
try:
logger.info("Removing backup directory %s" % (dst + ".bak"))
shutil.rmtree(dst + ".bak")
except FileNotFoundError:
logger.warning("Backup file %s seems to be already gone"
% (dst + ".bak"))
except PermissionError:
logger.warning("Cannot remove backup dir %s, you will have to " \
"do it manually"
% (dst + ".bak"))
# If not, try it as a file
except NotADirectoryError:
try:
logger.info("Removing backup file %s" % (dst + ".bak"))
os.remove(dst + ".bak")
except PermissionError:
logger.warning("Cannot remove backup file %s, you will have " \
"to do it manually"
% (dst + ".bak"))
# The End.
return True
# ARGPARSE
########################################
# Argument parsing using argparse
parser = argparse.ArgumentParser()
parser.add_argument( "--rootdir" ,
type=str ,
default="~/.config/persoconf/" ,
help="The persoconf directory to use" )
parser.add_argument( "--metafile" ,
type=str ,
default="META" ,
help="The name of the metadata files" )
parser.add_argument( "--verbose", "-v" ,
action="store_true" ,
help="Spout out more logs" )
subparsers = parser.add_subparsers( dest="command" ,
title="commands" )
# 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 ,
nargs="*" ,
default=[] ,
help="The apps to package ; defaults to all " \
"apps" )
parser_package.add_argument( "--pkgtype", "-t" ,
type=str ,
nargs="?" ,
default="tgz" ,
choices = [ "tgz" ] ,
help="The type of package to use" )
parser_package.add_argument( "--pkgname", "-p" ,
type=str ,
nargs="?" ,
default="persoconf." + (time.strftime("%Y%m%d")) ,
help="The name of the package to create" )
# 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" ,
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" )
# 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.add_argument( "app" ,
type=str ,
help="The app to add, or the app to add a conf " \
"file to" )
parser_add.add_argument( "conf" ,
nargs="?" ,
type=str ,
help="The conf file or directory to add to the app" )
parser_add.add_argument( "--confname" ,
type=str ,
help="A special name to save the conf " \
"file/directory in the persoconf directory" )
# Parse arguments
args = parser.parse_args()
# Transform paths
args.rootdir = os.path.expanduser(args.rootdir)
# LOGGING
########################################
log = logging.getLogger("persoconf")
log.setLevel(logging.DEBUG)
# Console handler
log_handler_console = logging.StreamHandler()
log_handler_console.setLevel(logging.WARNING)
# Let's adjust the console handler to the verbosity level required
if(args.verbose):
log_handler_console.setLevel(logging.DEBUG)
log_formatter_console = logging.Formatter("%(name)s:%(levelname)s: %(message)s")
log_handler_console.setFormatter(log_formatter_console)
log.addHandler(log_handler_console)
# SCRIPT
########################################
# If we were asked for help, let's give some
if args.command in [ "help", None ]:
parser.print_help()
exit()
# Try to create the Persoconf
try:
persoconf = Persoconf(path=args.rootdir, metafile=args.metafile)
# 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))
# 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()
# Create the directory
log.info("Creating persoconf directory...")
os.mkdir(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 = persoconf.path + "/" + args.app
if os.path.isdir(appdir):
if args.conf is None:
log.warning("App '%s' already exists" % args.app)
exit()
elif os.path.exists(appdir):
log.error("The name '%s' is already used by a file in the persoconf directory" % args.app)
exit()
else:
log.info("Creating app '%s' in persoconf directory..." % args.app)
os.mkdir(appdir)
print("New app '%s' created in persoconf directory" % args.app)
if args.conf is None:
exit()
# Check that the conf file we are saving exists
if args.confname:
confname = args.confname
else:
confname = os.path.basename(args.conf)
while confname[0] == ".":
confname = confname[1:] # Remove leading dots
confpath = appdir + "/" + confname
# Load app META file
try:
appmeta = Metafile(json_path = appdir + "/" + persoconf.metafile)
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (persoconf.metafile, args.app))
appmeta = Metafile( name=args.app )
appmeta.json_path = "/".join( [persoconf.path, args.app, persoconf.metafile] )
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
exit(1)
# Check that the file is really new
absconf = contractuser( os.path.abspath(os.path.expanduser(args.conf)) )
try:
dstdic = appmeta.get_files_dst2src()
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
exit(1)
if absconf in dstdic:
log.error("File %s already exists as %s in app %s" % (absconf, dstdic[absconf], args.app))
exit(1)
# Copy the file (or directory)
if copy_file_or_directory(log, path_dst=confpath, path_src=args.conf, overwrite=False):
log.info("New conf dir '%s' associated with app '%s'" % (confname, args.app))
else:
log.error("Failed to copy file or directory %s" % args.conf)
exit(1)
# Add the file to META: first update META data
log.debug("adding meta data for %s in %s" % (confname, args.app))
appmeta.add_file( confname, dest=absconf )
# Then write to the metafile
try:
log.debug("Writing to metafile")
appmeta.save()
except NoPathDefinedError:
log.error("Unable to save json Metafile for app %s : no path defined" % appmeta.name)
exit(1)
# 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()
# 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"])
)
# 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) )
# 3) Otherwise, no idea what it is
else:
log.warning( "Cannot find file %s in app data; " \
"ignoring it"
% f )
# 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"]) )
# PACKAGE COMMAND
########################################
if args.command == "package":
pkgname = args.pkgname
# TODO app == None => package all apps
appmetas = []
for app in args.apps :
# Load app META file
try:
appmetas.append( Metafile(json_path = "/".join([persoconf.path, app, persoconf.metafile])) )
except FileNotFoundError:
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" % (persoconf.metafile, app) )
continue
# TODO check that the app exists
# Adds the app name if there is only one packaged
if len(appmetas) == 1:
pkgname += ("." + appmeta[0].name)
# Switch according to the pkgtype
if args.pkgtype == "tgz":
pkgname += ".tgz"
# Create a tar containing the files in the right place
pkg = tarfile.open(pkgname, "w:gz")
for appmeta in appmetas:
for f in appmeta.files:
filename = persoconf.path +"/"+ appmeta.name +"/"+ f
filedest = appmeta.files[f]["dest"]
# Remove possible 'home'. The final targz should be extracted
# at 'home.
if filedest.startswith("~/"):
filedest = filedest[2:]
# TODO save and restore owners and permissions
try:
pkg.add(filename, arcname=filedest)
log.info("Adding %s to package" % f)
except:
log.warning("Failed to add %s to tar package %s" % (f, pkgname))
pkg.close()
########################################