Adding META management for 'add' command

master
Lertsenem 2015-08-15 18:42:07 +02:00
parent 4157776316
commit ebf051fe3f
1 changed files with 43 additions and 7 deletions

View File

@ -2,6 +2,7 @@
import argparse import argparse
import logging import logging
import json
import os, os.path import os, os.path
import shutil import shutil
@ -30,6 +31,14 @@ def yes_no_question(question="Yes or no?", default=False):
return default return default
def contractuser(path):
home = os.path.expanduser("~")
if path.startswith(home):
return path.replace(home, "~", 1) # Replace only one instance: the first one
else:
return path
# ARGPARSE # ARGPARSE
######################################## ########################################
# Argument parsing using argparse # Argument parsing using argparse
@ -108,8 +117,8 @@ if not os.path.isdir(args.rootdir):
print("New persoconf directory created at '%s'" % arg.rootdir) print("New persoconf directory created at '%s'" % arg.rootdir)
# ADD COMMAND
# SWITCH COMMAND ########################################
if args.command == "add": if args.command == "add":
# Check (and create) the app directory # Check (and create) the app directory
@ -143,9 +152,30 @@ if args.command == "add":
confpath = appdir + "/" + confname confpath = appdir + "/" + confname
# Check that the file is really new # Load app META file
absconf = os.path.abspath(os.path.expanduser(args.conf)) metafile = appdir + "/" + args.metafile
try:
appmeta = json.load( open(metafile) )
except FileNotFoundError:
log.info("Metafile %s does not exist for app %s" % (args.metafile, args.app))
appmeta = { "name" : args.app, "depends": {}, "files": {} }
# Check that the file is really new
absconf = contractuser( os.path.abspath(os.path.expanduser(args.conf)) )
try:
destslist = {}
for (fname,finfo) in appmeta["files"].items():
destslist[finfo["dest"]] = fname
except KeyError:
log.error("Malformed metafile %s in app %s" % (args.metafile, args.app) )
exit(1)
if absconf in destslist:
log.error("File %s already exists as %s in app %s" % (absconf, destslist[absconf], args.app))
exit(1)
# Copy the conf dir # Copy the conf dir
try: try:
@ -178,18 +208,24 @@ if args.command == "add":
log.info("Copying conf file") log.info("Copying conf file")
shutil.copyfile(args.conf, confpath) shutil.copyfile(args.conf, confpath)
print("New conf file '%s' associated with app '%s'" % (confname, args.app)) log.info("New conf file '%s' associated with app '%s'" % (confname, args.app))
except PermissionError: except PermissionError:
log.error("Unable to write on '%s', please check permissions" % confpath) log.error("Unable to write on '%s', please check permissions" % confpath)
exit() exit()
# Add the file to META
# Update META data
log.debug("adding meta data for %s in %s" % (confname, args.app))
appmeta["files"][confname] = { "dest": absconf }
# Write to the metafile
# TODO add the file to META log.debug("Wrinting to metafile")
json.dump(appmeta, open(metafile, 'w'))
########################################
print(args) print(args)