parent
cd848ff88a
commit
6206c015a2
|
@ -0,0 +1,134 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
import utils
|
||||||
|
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
|
||||||
|
|
||||||
|
# --------------------------------------
|
||||||
|
def init(parser):
|
||||||
|
|
||||||
|
parser.add_argument( "app" ,
|
||||||
|
type=str ,
|
||||||
|
help="The app to add, or the app to add a conf " \
|
||||||
|
"file to" )
|
||||||
|
|
||||||
|
parser.add_argument( "conf" ,
|
||||||
|
nargs="?" ,
|
||||||
|
type=str ,
|
||||||
|
help="The conf file or directory to add to the app" )
|
||||||
|
|
||||||
|
parser.add_argument( "--confname" ,
|
||||||
|
type=str ,
|
||||||
|
help="A special name to save the conf " \
|
||||||
|
"file/directory in the persoconf directory" )
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------
|
||||||
|
def run(args, persoconf, logger):
|
||||||
|
|
||||||
|
# Check (and create) the app directory
|
||||||
|
appdir = os.path.join( persoconf.path, args.app )
|
||||||
|
|
||||||
|
if os.path.isdir(appdir):
|
||||||
|
if args.conf is None:
|
||||||
|
logger.warning("App '%s' already exists" % args.app)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
elif os.path.exists(appdir):
|
||||||
|
logger.error( "The name '%s' is already used by a file in the " \
|
||||||
|
"persoconf directory"
|
||||||
|
% args.app)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.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 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
|
||||||
|
try:
|
||||||
|
appmeta = Metafile( json_path = os.path.join( appdir ,
|
||||||
|
persoconf.metafile ) )
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.info( "Metafile %s does not exist for app %s"
|
||||||
|
% (persoconf.metafile, args.app) )
|
||||||
|
appmeta = Metafile( name=args.app )
|
||||||
|
appmeta.json_path = os.path.join( persoconf.path ,
|
||||||
|
args.app ,
|
||||||
|
persoconf.metafile )
|
||||||
|
|
||||||
|
except MalformedMetafileError:
|
||||||
|
logger.error( "Malformed metafile %s in app %s"
|
||||||
|
% (persoconf.metafile, args.app) )
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Check that the file is really new
|
||||||
|
absconf = utils.contractuser(
|
||||||
|
os.path.abspath(os.path.expanduser(args.conf))
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
dstdic = appmeta.get_files_dst2src()
|
||||||
|
|
||||||
|
except MalformedMetafileError:
|
||||||
|
logger.error( "Malformed metafile %s in app %s"
|
||||||
|
% (persoconf.metafile, args.app) )
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
if absconf in dstdic:
|
||||||
|
logger.error( "File %s already exists as %s in app %s"
|
||||||
|
% (absconf, dstdic[absconf], args.app) )
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Copy the file (or directory)
|
||||||
|
if utils.copy_file_or_directory( logger ,
|
||||||
|
path_dst=confpath ,
|
||||||
|
path_src=args.conf,
|
||||||
|
overwrite=False ):
|
||||||
|
logger.info( "New conf dir '%s' associated with app '%s'"
|
||||||
|
% (confname, args.app))
|
||||||
|
else:
|
||||||
|
logger.error( "Failed to copy file or directory %s" % args.conf)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Add the file to META: first update META data
|
||||||
|
logger.debug( "adding meta data for %s in %s" % (confname, args.app) )
|
||||||
|
appmeta.add_file( confname, dest=absconf )
|
||||||
|
|
||||||
|
# Then write to the metafile
|
||||||
|
try:
|
||||||
|
logger.debug("Writing to metafile")
|
||||||
|
appmeta.save()
|
||||||
|
|
||||||
|
except NoPathDefinedError:
|
||||||
|
logger.error( "Unable to save json Metafile for app %s : no path " \
|
||||||
|
"defined"
|
||||||
|
% appmeta.name )
|
||||||
|
exit(1)
|
|
@ -15,7 +15,7 @@ from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
|
||||||
from persoconf import Persoconf, FileInsteadOfDirError, \
|
from persoconf import Persoconf, FileInsteadOfDirError, \
|
||||||
PersoconfRootDoesNotExistError
|
PersoconfRootDoesNotExistError
|
||||||
|
|
||||||
import commands.list, commands.package, commands.update
|
import commands.list, commands.package, commands.update, commands.add
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,21 +68,7 @@ parser_add = subparsers.add_parser( "add" ,
|
||||||
help="Add an app to the persoconf " \
|
help="Add an app to the persoconf " \
|
||||||
"directory, or add a config file " \
|
"directory, or add a config file " \
|
||||||
"to an existing app" )
|
"to an existing app" )
|
||||||
|
commands.add.init(parser_add)
|
||||||
parser_add.add_argument( "app" ,
|
|
||||||
type=str ,
|
|
||||||
help="The app to add, or the app to add a conf " \
|
|
||||||
"file to" )
|
|
||||||
|
|
||||||
parser_add.add_argument( "conf" ,
|
|
||||||
nargs="?" ,
|
|
||||||
type=str ,
|
|
||||||
help="The conf file or directory to add to the app" )
|
|
||||||
|
|
||||||
parser_add.add_argument( "--confname" ,
|
|
||||||
type=str ,
|
|
||||||
help="A special name to save the conf " \
|
|
||||||
"file/directory in the persoconf directory" )
|
|
||||||
|
|
||||||
# Delete
|
# Delete
|
||||||
parser_del = subparsers.add_parser( "delete" ,
|
parser_del = subparsers.add_parser( "delete" ,
|
||||||
|
@ -273,94 +259,7 @@ if args.command == "delete":
|
||||||
# ADD COMMAND
|
# ADD COMMAND
|
||||||
########################################
|
########################################
|
||||||
if args.command == "add":
|
if args.command == "add":
|
||||||
|
commands.add.run(args, persoconf, log)
|
||||||
# Check (and create) the app directory
|
|
||||||
appdir = persoconf.path + "/" + 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 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] == "/":
|
|
||||||
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
|
|
||||||
try:
|
|
||||||
appmeta = Metafile(json_path = appdir + "/" + persoconf.metafile)
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
|
||||||
log.info("Metafile %s does not exist for app %s" % (persoconf.metafile, args.app))
|
|
||||||
appmeta = Metafile( name=args.app )
|
|
||||||
appmeta.json_path = "/".join( [persoconf.path, args.app, persoconf.metafile] )
|
|
||||||
|
|
||||||
except MalformedMetafileError:
|
|
||||||
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Check that the file is really new
|
|
||||||
absconf = contractuser( os.path.abspath(os.path.expanduser(args.conf)) )
|
|
||||||
|
|
||||||
try:
|
|
||||||
dstdic = appmeta.get_files_dst2src()
|
|
||||||
|
|
||||||
except MalformedMetafileError:
|
|
||||||
log.error("Malformed metafile %s in app %s" % (persoconf.metafile, args.app) )
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
if absconf in dstdic:
|
|
||||||
log.error("File %s already exists as %s in app %s" % (absconf, dstdic[absconf], args.app))
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
# 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 )
|
|
||||||
|
|
||||||
# Then write to the metafile
|
|
||||||
try:
|
|
||||||
log.debug("Writing to metafile")
|
|
||||||
appmeta.save()
|
|
||||||
|
|
||||||
except NoPathDefinedError:
|
|
||||||
log.error("Unable to save json Metafile for app %s : no path defined" % appmeta.name)
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# UPDATE COMMAND
|
# UPDATE COMMAND
|
||||||
########################################
|
########################################
|
||||||
|
|
Loading…
Reference in New Issue