Addinf 'add' command

But no use of META right now
master
Lertsenem 2015-08-15 17:32:23 +02:00
parent 50f0990f3e
commit 4157776316
1 changed files with 90 additions and 1 deletions

View File

@ -3,6 +3,7 @@
import argparse
import logging
import os, os.path
import shutil
# FUNCTIONS
########################################
@ -34,6 +35,7 @@ def yes_no_question(question="Yes or no?", default=False):
# 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")
@ -59,6 +61,9 @@ parser.add_argument( '--confname', help='A special name to save the conf file/di
args = parser.parse_args()
# Transform paths
args.rootdir = os.path.expanduser(args.rootdir)
# LOGGING
########################################
log = logging.getLogger("persoconf")
@ -99,7 +104,91 @@ if not os.path.isdir(args.rootdir):
# Create the directory
log.info("Creating persoconf directory...")
os.mkdir(args.dir)
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)