Change confname when already taken

If the confname of the file we want to add already exists (for a
different abspath of course), we add a numeric extension to it. Same
thing if the confname is the metafile name.
master
Lertsenem 2016-06-03 03:34:54 +02:00
parent 20566a3c89
commit e6c4dcbdbc
1 changed files with 37 additions and 20 deletions

View File

@ -51,26 +51,6 @@ def run(args, persoconf, logger):
if args.conf is None: if args.conf is None:
exit() exit()
# Check that the conf file we are saving exists
if args.confname:
confname = args.confname
else:
confname = args.conf
# We need to remove trailing '/' because 'os.path.basename("toto/")'
# returns ''. And that's not cool.
while confname[-1] == os.path.sep :
confname = confname[:-1] # Remove trailing slashes
confname = os.path.basename(confname)
# Remove leading dots, so the name under which the conffile is saved is
# not hidden.
while confname[0] == ".":
confname = confname[1:] # Remove leading dots
confpath = appdir + "/" + confname
# Load app META file # Load app META file
try: try:
appmeta = Metafile( json_path = os.path.join( appdir , appmeta = Metafile( json_path = os.path.join( appdir ,
@ -107,6 +87,43 @@ def run(args, persoconf, logger):
% (absconf, dstdic[absconf], args.app) ) % (absconf, dstdic[absconf], args.app) )
exit(1) exit(1)
# Check that the conf file we are saving exists
if args.confname:
confname = args.confname
else:
confname = args.conf
# We need to remove trailing '/' because 'os.path.basename("toto/")'
# returns ''. And that's not cool.
while confname[-1] == os.path.sep :
confname = confname[:-1] # Remove trailing slashes
confname = os.path.basename(confname)
# Remove leading dots, so the name under which the conffile is saved is
# not hidden.
while confname[0] == ".":
confname = confname[1:] # Remove leading dots
# Check the conf file name
if ( confname == persoconf.metafile
or confname in appmeta.files ):
logger.info( "Confname '%s' is already taken: trying another one"
% confname )
ending = 1
while ( confname + "." + str(ending) == persoconf.metafile
or confname + "." + str(ending) in appmeta.files ):
ending += 1
confname = confname + "." + str(ending)
logger.info( "New confname is '%s'" % confname )
# Deduce the path from the conf file name
confpath = appdir + "/" + confname
# Copy the file (or directory) # Copy the file (or directory)
if utils.copy_file_or_directory( logger , if utils.copy_file_or_directory( logger ,
path_dst=confpath , path_dst=confpath ,