2015-08-08 10:41:08 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2015-08-08 11:11:52 -04:00
|
|
|
import logging
|
|
|
|
import os, os.path
|
2015-08-15 11:32:23 -04:00
|
|
|
import shutil
|
2015-09-12 12:54:29 -04:00
|
|
|
import time
|
|
|
|
import tarfile
|
2015-08-08 10:41:08 -04:00
|
|
|
|
2015-12-07 09:00:01 -05:00
|
|
|
from utils import yes_no_question, contractuser, \
|
|
|
|
copy_file_or_directory, delete_file_or_dir
|
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
|
2015-12-07 09:00:01 -05:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
from persoconf import Persoconf, FileInsteadOfDirError, \
|
|
|
|
PersoconfRootDoesNotExistError
|
2015-08-17 10:50:53 -04:00
|
|
|
|
2015-12-07 12:11:35 -05:00
|
|
|
import commands.list, commands.package, commands.update, commands.add, \
|
2015-12-11 09:55:10 -05:00
|
|
|
commands.delete, commands.check
|
2015-12-07 10:36:07 -05:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
|
|
|
|
|
2015-08-08 11:11:52 -04:00
|
|
|
# ARGPARSE
|
2017-01-02 16:17:12 -05:00
|
|
|
##############################################################################
|
2015-08-08 10:41:08 -04:00
|
|
|
# Argument parsing using argparse
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-08-08 10:41:08 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
2015-08-08 11:11:52 -04:00
|
|
|
|
2017-01-02 16:17:12 -05:00
|
|
|
parser.add_argument( "--rootdir",
|
|
|
|
type=str,
|
|
|
|
default="~/.config/persoconf/",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="The persoconf directory to use" )
|
2015-08-08 10:41:08 -04:00
|
|
|
|
2017-01-02 16:17:12 -05:00
|
|
|
parser.add_argument( "--metafile",
|
|
|
|
type=str,
|
|
|
|
default="META",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="The name of the metadata files" )
|
|
|
|
|
2017-01-02 16:17:12 -05:00
|
|
|
parser.add_argument( "--verbose", "-v",
|
|
|
|
action="store_true",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="Spout out more logs" )
|
|
|
|
|
|
|
|
|
2017-01-02 16:17:12 -05:00
|
|
|
subparsers = parser.add_subparsers( dest="command",
|
2015-09-16 11:47:52 -04:00
|
|
|
title="commands" )
|
2015-08-08 10:41:08 -04:00
|
|
|
|
|
|
|
# Help
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_help = subparsers.add_parser( "help",
|
2015-09-16 11:47:52 -04:00
|
|
|
help="Print this help" )
|
2015-08-08 10:41:08 -04:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
# List
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_list = subparsers.add_parser( "list",
|
2015-09-16 11:47:52 -04:00
|
|
|
help="List backuped apps" )
|
2015-12-07 10:36:07 -05:00
|
|
|
commands.list.init(parser_list)
|
2015-09-16 13:31:18 -04:00
|
|
|
|
2015-08-08 10:41:08 -04:00
|
|
|
# Package
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_package = subparsers.add_parser( "package",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="Package a persoconf repository" )
|
2015-12-07 10:48:01 -05:00
|
|
|
commands.package.init(parser_package)
|
2015-08-08 10:41:08 -04:00
|
|
|
|
2015-08-19 19:07:51 -04:00
|
|
|
# Update
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_update = subparsers.add_parser( "update",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="Backup an app configuration in " \
|
|
|
|
"the persoconf directory with " \
|
|
|
|
"the configuration in use now" )
|
2015-12-07 11:24:59 -05:00
|
|
|
commands.update.init(parser_update)
|
2015-08-08 10:41:08 -04:00
|
|
|
|
2015-08-09 09:03:34 -04:00
|
|
|
# Add
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_add = subparsers.add_parser( "add",
|
2015-09-15 09:51:32 -04:00
|
|
|
help="Add an app to the persoconf " \
|
|
|
|
"directory, or add a config file " \
|
|
|
|
"to an existing app" )
|
2015-12-07 11:42:53 -05:00
|
|
|
commands.add.init(parser_add)
|
2015-08-09 09:03:34 -04:00
|
|
|
|
2015-12-11 09:55:10 -05:00
|
|
|
# Check
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_check = subparsers.add_parser( "check",
|
2015-12-11 09:55:10 -05:00
|
|
|
help="Checks if the backuped and the " \
|
|
|
|
"original file are different." )
|
|
|
|
commands.check.init(parser_check)
|
|
|
|
|
2015-09-16 11:48:25 -04:00
|
|
|
# Delete
|
2017-01-02 16:17:12 -05:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
parser_del = subparsers.add_parser( "delete",
|
2015-09-16 11:48:25 -04:00
|
|
|
help="Delete an app from the persoconf " \
|
|
|
|
"directory, or a config file from " \
|
|
|
|
"an existing app" )
|
2015-12-07 12:11:35 -05:00
|
|
|
commands.delete.init(parser_del)
|
2015-09-16 12:51:44 -04:00
|
|
|
|
2015-09-15 09:51:32 -04:00
|
|
|
# Parse arguments
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-08-08 10:41:08 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2015-08-15 11:32:23 -04:00
|
|
|
# Transform paths
|
|
|
|
args.rootdir = os.path.expanduser(args.rootdir)
|
|
|
|
|
2015-08-09 09:03:34 -04:00
|
|
|
# LOGGING
|
2017-01-02 16:17:12 -05:00
|
|
|
##############################################################################
|
2015-08-09 09:03:34 -04:00
|
|
|
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
|
2015-08-08 11:11:52 -04:00
|
|
|
if(args.verbose):
|
2015-08-09 09:03:34 -04:00
|
|
|
log_handler_console.setLevel(logging.DEBUG)
|
2015-08-08 11:11:52 -04:00
|
|
|
|
2015-08-09 09:03:34 -04:00
|
|
|
log_formatter_console = logging.Formatter("%(name)s:%(levelname)s: %(message)s")
|
|
|
|
log_handler_console.setFormatter(log_formatter_console)
|
|
|
|
|
|
|
|
|
|
|
|
log.addHandler(log_handler_console)
|
|
|
|
|
|
|
|
# SCRIPT
|
2017-01-02 16:17:12 -05:00
|
|
|
##############################################################################
|
2015-08-09 08:21:46 -04:00
|
|
|
# If we were asked for help, let's give some
|
|
|
|
if args.command in [ "help", None ]:
|
|
|
|
parser.print_help()
|
|
|
|
exit()
|
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
# Try to create the Persoconf
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-09-14 07:03:30 -04:00
|
|
|
try:
|
|
|
|
persoconf = Persoconf(path=args.rootdir, metafile=args.metafile)
|
2015-08-08 11:11:52 -04:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
# 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))
|
2015-08-08 11:11:52 -04:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
# Otherwise, maybe we can create the directory ?
|
|
|
|
except PersoconfRootDoesNotExistError:
|
2015-08-09 09:03:34 -04:00
|
|
|
log.warning("'%s' does not exist" % str(args.rootdir))
|
2015-08-08 11:11:52 -04:00
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
# Ask before creating the directory
|
2015-09-16 13:31:18 -04:00
|
|
|
if not yes_no_question( "Do you want to create the persoconf directory " \
|
2015-09-14 07:03:30 -04:00
|
|
|
"'%s'?"
|
|
|
|
% str(args.rootdir) ) :
|
2015-08-09 09:03:34 -04:00
|
|
|
log.info("The persoconf directory was not created")
|
2015-08-08 11:11:52 -04:00
|
|
|
exit()
|
|
|
|
|
|
|
|
# Create the directory
|
2015-08-09 09:03:34 -04:00
|
|
|
log.info("Creating persoconf directory...")
|
2015-08-15 11:32:23 -04:00
|
|
|
os.mkdir(args.rootdir)
|
|
|
|
|
2015-09-14 07:03:30 -04:00
|
|
|
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
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-09-14 07:03:30 -04:00
|
|
|
if args.command == "list":
|
2015-12-07 10:36:07 -05:00
|
|
|
commands.list.run(args, persoconf, log)
|
2015-09-16 13:31:18 -04:00
|
|
|
|
2015-09-16 12:51:44 -04:00
|
|
|
# DELETE COMMAND
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-09-16 12:51:44 -04:00
|
|
|
if args.command == "delete":
|
2015-12-07 12:11:35 -05:00
|
|
|
commands.delete.run(args, persoconf, log)
|
2015-09-16 12:51:44 -04:00
|
|
|
|
2015-08-15 12:42:07 -04:00
|
|
|
# ADD COMMAND
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-08-15 11:32:23 -04:00
|
|
|
if args.command == "add":
|
2015-12-07 11:42:53 -05:00
|
|
|
commands.add.run(args, persoconf, log)
|
2015-08-15 11:32:23 -04:00
|
|
|
|
2015-12-11 09:55:10 -05:00
|
|
|
# CHECK COMMAND
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-12-11 09:55:10 -05:00
|
|
|
if args.command == "check":
|
|
|
|
commands.check.run(args, persoconf, log)
|
|
|
|
|
2015-08-19 19:07:51 -04:00
|
|
|
# UPDATE COMMAND
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-08-19 19:07:51 -04:00
|
|
|
if args.command == "update":
|
2015-12-07 11:24:59 -05:00
|
|
|
commands.update.run(args, persoconf, log)
|
2015-08-19 19:07:51 -04:00
|
|
|
|
2015-09-12 11:53:22 -04:00
|
|
|
# PACKAGE COMMAND
|
2017-01-02 16:17:12 -05:00
|
|
|
# =============================================================================
|
2015-09-12 12:54:29 -04:00
|
|
|
if args.command == "package":
|
2015-12-07 10:48:01 -05:00
|
|
|
commands.package.run(args, persoconf, log)
|