Adding copy function

master
Lertsenem 2015-08-20 01:07:17 +02:00
parent 0292d4d706
commit 33b0d49970
1 changed files with 131 additions and 39 deletions

View File

@ -40,6 +40,127 @@ def contractuser(path):
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
@ -143,7 +264,7 @@ if args.command == "add":
if args.conf is None:
exit()
# Check (and create) the conf file
# Check that the conf file we are saving exists
if args.confname:
confname = args.confname
else:
@ -160,6 +281,7 @@ if args.command == "add":
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (args.metafile, args.app))
appmeta = Metafile( name=args.app )
appmeta.json_path = "/".join( [args.rootdir, args.app, args.metafile] )
except MalformedMetafileError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
@ -179,48 +301,18 @@ if args.command == "add":
log.error("File %s already exists as %s in app %s" % (absconf, dstdic[absconf], args.app))
exit(1)
# Copy the conf dir
try:
log.info("Copying conf directory")
shutil.copytree(args.conf, confpath)
# 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)
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)
log.info("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()
# Add the file to META
# Update META data
# 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 )
# Write to the metafile
# Then write to the metafile
try:
log.debug("Writing to metafile")
appmeta.save()