196 lines
5.8 KiB
Python
Executable File
196 lines
5.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import logging
|
|
import os, os.path
|
|
import shutil
|
|
|
|
# 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
|
|
|
|
# ARGPARSE
|
|
########################################
|
|
# Argument parsing using argparse
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument( "--rootdir", type=str, help="The persoconf directory to use", default="~/.config/persoconf/" )
|
|
parser.add_argument( "--metafile", type=str, help="The name of the metadata files", default="META" )
|
|
parser.add_argument( '--verbose', "-v", help='Spout out more logs', action="store_true")
|
|
|
|
|
|
subparsers = parser.add_subparsers( dest="command", title="commands") #, description="Valid commands" )
|
|
|
|
# Help
|
|
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"] )
|
|
|
|
# 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"])
|
|
|
|
# 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', help="The app to add, or the app to add a conf file to", type=str)
|
|
parser_add.add_argument( 'conf', help="The conf file or directory to add to the app", nargs="?", type=str)
|
|
parser.add_argument( '--confname', help='A special name to save the conf file/directory in the persoconf directory', type=str)
|
|
|
|
|
|
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()
|
|
|
|
# Let's check that the rootdir exists
|
|
if not os.path.isdir(args.rootdir):
|
|
|
|
if os.path.exists(args.rootdir):
|
|
log.error("'%s' exists but is a file" % 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) ) :
|
|
log.info("The persoconf directory was not created")
|
|
exit()
|
|
|
|
# Create the directory
|
|
log.info("Creating persoconf directory...")
|
|
os.mkdir(args.rootdir)
|
|
|
|
print("New persoconf directory created at '%s'" % arg.rootdir)
|
|
|
|
|
|
# SWITCH COMMAND
|
|
if args.command == "add":
|
|
|
|
# Check (and create) the app directory
|
|
appdir = args.rootdir + "/" + 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 (and create) the conf file
|
|
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
|
|
|
|
# Check that the file is really new
|
|
absconf = os.path.abspath(os.path.expanduser(args.conf))
|
|
|
|
|
|
# Copy the conf dir
|
|
try:
|
|
log.info("Copying conf directory")
|
|
shutil.copytree(args.conf, confpath)
|
|
|
|
print("New conf dir '%s' associated with app '%s'" % confname, args.app)
|
|
|
|
except FileNotFoundError:
|
|
log.error("The conf file '%s' does not seem to exist" % args.conf)
|
|
exit()
|
|
|
|
except PermissionError:
|
|
log.error("Unable to write on '%s', please check permissions" % confpath)
|
|
exit()
|
|
|
|
except FileExistsError:
|
|
log.error("The conf dir '%s' already exists in '%s', please choose another --confname." % (confname, appdir))
|
|
exit()
|
|
|
|
# It's not a dir, it's a file !
|
|
except NotADirectoryError:
|
|
|
|
# Try not to overwrite an existing file
|
|
if os.path.exists(confpath):
|
|
log.error("The conf file '%s' already exists in '%s', please choose another --confname." % (confname, appdir))
|
|
exit()
|
|
|
|
try:
|
|
log.info("Copying conf file")
|
|
shutil.copyfile(args.conf, confpath)
|
|
|
|
print("New conf file '%s' associated with app '%s'" % (confname, args.app))
|
|
|
|
except PermissionError:
|
|
log.error("Unable to write on '%s', please check permissions" % confpath)
|
|
exit()
|
|
|
|
|
|
|
|
# TODO add the file to META
|
|
|
|
|
|
|
|
print(args)
|
|
|
|
|