107 lines
3.3 KiB
Python
Executable File
107 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import logging
|
|
import os, os.path
|
|
|
|
# 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( '--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()
|
|
|
|
# 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.dir)
|
|
|
|
print(args)
|
|
|
|
|