rewrite to python3
This commit is contained in:
421
userconfig.py
421
userconfig.py
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
# encoding: utf-8
|
||||
#
|
||||
# $Id$
|
||||
@@ -8,7 +8,6 @@
|
||||
userconfig.py
|
||||
|
||||
Created by Marcus Stoegbauer on 2013-01-10.
|
||||
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
||||
"""
|
||||
|
||||
import sys
|
||||
@@ -20,251 +19,225 @@ import time
|
||||
|
||||
#
|
||||
import Userconfig.cfgfile as cfgfile
|
||||
from Userconfig.checks import checks
|
||||
from Userconfig.checks import Checks
|
||||
import Userconfig.Tools as Tools
|
||||
|
||||
debug = Tools.Debug()
|
||||
|
||||
classchecks = checks()
|
||||
classchecks = Checks()
|
||||
cfg = cfgfile.Conf()
|
||||
|
||||
hostclasses = classchecks.__classesForHost__()
|
||||
hostclasses = classchecks.__classes_for_host__()
|
||||
|
||||
class Usage(Exception):
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
# def __init__
|
||||
help_message = """
|
||||
help_message = """
|
||||
-h help
|
||||
-v verbose level (multiple v for higher level)
|
||||
-c userconfig.cfg config file
|
||||
"""
|
||||
# class Usage
|
||||
|
||||
|
||||
class Usage(Exception):
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
|
||||
|
||||
def workconf(directory, depth=2):
|
||||
"""walks through directory, collecting all filenames, returns list of all filenames"""
|
||||
dirs = os.listdir(directory)
|
||||
ret = []
|
||||
debug.debug("Finding files in directory %s." % directory, 4)
|
||||
for d in dirs:
|
||||
name = directory+"/"+d
|
||||
if os.path.isdir(name):
|
||||
# fixme: create name if it does not exist
|
||||
workconf(name, depth+1)
|
||||
# if dir
|
||||
if name.endswith(".swp"):
|
||||
continue
|
||||
if d == ".svn":
|
||||
continue
|
||||
ret.append(name)
|
||||
debug.debug("Found file %s in directory %s" % (name, directory), 4)
|
||||
# if not .swp
|
||||
# for d
|
||||
return ret
|
||||
# def workconf
|
||||
"""walks through directory, collecting all filenames, returns list of all filenames"""
|
||||
dirs = os.listdir(directory)
|
||||
ret = []
|
||||
debug.debug("Finding files in directory %s." % directory, 4)
|
||||
for d in dirs:
|
||||
name = directory+"/"+d
|
||||
if os.path.isdir(name):
|
||||
# fixme: create name if it does not exist
|
||||
workconf(name, depth+1)
|
||||
if name.endswith(".swp"):
|
||||
continue
|
||||
if d == ".svn":
|
||||
continue
|
||||
ret.append(name)
|
||||
debug.debug("Found file %s in directory %s" % (name, directory), 4)
|
||||
return ret
|
||||
|
||||
|
||||
def workdir(directory):
|
||||
"""walks through all host classes, checking if the classes directory exists in directory
|
||||
then collect all filenames within this directory and return a dict of all files for this
|
||||
directory
|
||||
"""
|
||||
debug.debug("Working on directory %s" % directory, 3)
|
||||
# skip directory if no CONFIGFILE present
|
||||
if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")):
|
||||
debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
||||
return ({},None)
|
||||
# if not DEST
|
||||
|
||||
# get config file for directory
|
||||
dirConfig = Tools.getConfig(directory+"/"+cfg.get("Main", "configfile"))
|
||||
if not dirConfig:
|
||||
debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
||||
return ({},None)
|
||||
# if not dirConfig
|
||||
|
||||
destdir = dirConfig.get("Main","dest")
|
||||
# destfiles is a dict of all files that will be created from the classes config
|
||||
# key is the destination filename, values are all classes filenames that are used to
|
||||
# build the file
|
||||
destfiles = {}
|
||||
|
||||
# walk through all know classes in directory and find filenames
|
||||
for h in hostclasses:
|
||||
# build classes directory
|
||||
if h[0] != "":
|
||||
classdir = directory+"/"+h[0]+"_"+h[1]
|
||||
else:
|
||||
classdir = directory+"/"+h[1]
|
||||
# if all
|
||||
debug.debug("Looking for directory %s." % classdir, 4)
|
||||
# if class directory exists
|
||||
if os.path.isdir(classdir):
|
||||
debug.debug("Found directory %s, getting files." % classdir, 4)
|
||||
# get list of files within this class directory
|
||||
tempfiles = workconf(classdir)
|
||||
debug.debug("Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4)
|
||||
# put files into dict
|
||||
for f in tempfiles:
|
||||
destname = destdir+os.path.basename(f) # destination filename
|
||||
if not destfiles.has_key(destname):
|
||||
destfiles[destname] = []
|
||||
# if not destname, create key with empty list
|
||||
destfiles[destname].append(f) # append each file to dict
|
||||
debug.debug("Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4)
|
||||
# for tempfiles
|
||||
# if classdir
|
||||
# for hostclasses
|
||||
|
||||
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
|
||||
return (destfiles, dirConfig)
|
||||
# def work
|
||||
"""walks through all host classes, checking if the classes directory exists in directory
|
||||
then collect all filenames within this directory and return a dict of all files for this
|
||||
directory
|
||||
"""
|
||||
debug.debug("Working on directory %s" % directory, 3)
|
||||
# skip directory if no CONFIGFILE present
|
||||
if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")):
|
||||
debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
||||
return {},None
|
||||
|
||||
def buildFile(classfiles, destfile, commentstring):
|
||||
"""open all classfiles, assemble them and write the contents into a tempfile
|
||||
returns the name of tempfile"""
|
||||
content = []
|
||||
if commentstring != "":
|
||||
debug.debug("commentstring found, adding header.", 3)
|
||||
content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n")
|
||||
# if commentstring not empty
|
||||
|
||||
for f in classfiles:
|
||||
debug.debug("Merging %s." % f, 4)
|
||||
fp = open(f, "r")
|
||||
filecontent = fp.read()
|
||||
# get config file for directory
|
||||
dir_config = Tools.get_config(directory + "/" + cfg.get("Main", "configfile"))
|
||||
if not dir_config:
|
||||
debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
||||
return {},None
|
||||
|
||||
destdir = dir_config.get("Main","dest")
|
||||
# destfiles is a dict of all files that will be created from the classes config
|
||||
# key is the destination filename, values are all classes filenames that are used to
|
||||
# build the file
|
||||
destfiles = {}
|
||||
|
||||
# walk through all know classes in directory and find filenames
|
||||
for h in hostclasses:
|
||||
# build classes directory
|
||||
if h[0] != "":
|
||||
classdir = directory+"/"+h[0]+"_"+h[1]
|
||||
else:
|
||||
classdir = directory+"/"+h[1]
|
||||
debug.debug("Looking for directory %s." % classdir, 4)
|
||||
# if class directory exists
|
||||
if os.path.isdir(classdir):
|
||||
debug.debug("Found directory %s, getting files." % classdir, 4)
|
||||
# get list of files within this class directory
|
||||
tempfiles = workconf(classdir)
|
||||
debug.debug("Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4)
|
||||
# put files into dict
|
||||
for f in tempfiles:
|
||||
destname = destdir+os.path.basename(f) # destination filename
|
||||
if not destname in destfiles:
|
||||
destfiles[destname] = []
|
||||
destfiles[destname].append(f) # append each file to dict
|
||||
debug.debug("Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4)
|
||||
|
||||
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
|
||||
return destfiles, dir_config
|
||||
|
||||
|
||||
def build_file(classfiles, destfile, commentstring):
|
||||
"""open all classfiles, assemble them and write the contents into a tempfile
|
||||
returns the name of tempfile
|
||||
:param classfiles:
|
||||
:param destfile:
|
||||
:param commentstring:
|
||||
:return: str
|
||||
"""
|
||||
content = []
|
||||
if commentstring != "":
|
||||
debug.debug("commentstring found, adding header.", 3)
|
||||
content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n")
|
||||
|
||||
for f in classfiles:
|
||||
debug.debug("Merging %s." % f, 4)
|
||||
fp = open(f, "r")
|
||||
filecontent = fp.read()
|
||||
fp.close()
|
||||
if commentstring == "":
|
||||
# look for stamp in content, replace with real stamp
|
||||
if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent):
|
||||
debug.debug("commentstring empty, replacing stamp in file", 3)
|
||||
filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent)
|
||||
content.append(filecontent)
|
||||
|
||||
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
||||
|
||||
try:
|
||||
fp = os.fdopen(tempfd, "w")
|
||||
except:
|
||||
Tools.error("Cannot write to temporary file %s" % tempfilename)
|
||||
os.remove(tempfilename)
|
||||
return False
|
||||
debug.debug("Writing merged files into tempfile %s." % tempfilename, 3)
|
||||
for block in content:
|
||||
fp.write(block)
|
||||
fp.write("\n")
|
||||
fp.close()
|
||||
if commentstring == "":
|
||||
# look for stamp in content, replace with real stamp
|
||||
if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent):
|
||||
debug.debug("commentstring empty, replacing stamp in file", 3)
|
||||
filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent)
|
||||
# if search
|
||||
# if commentstring empty
|
||||
content.append(filecontent)
|
||||
# end f
|
||||
|
||||
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
||||
|
||||
fp = None
|
||||
try:
|
||||
fp = os.fdopen(tempfd, "w")
|
||||
except:
|
||||
Tools.error("Cannot write to temporary file %s" % tempfilename)
|
||||
os.remove(tempfilename)
|
||||
return False
|
||||
# try
|
||||
debug.debug("Writing merged files into tempfile %s." % tempfilename, 3)
|
||||
for block in content:
|
||||
fp.write(block)
|
||||
fp.write("\n")
|
||||
# for content
|
||||
fp.close()
|
||||
return tempfilename
|
||||
|
||||
return tempfilename
|
||||
# def buildFile
|
||||
|
||||
def processAllFiles(destfiles, dirConfig):
|
||||
"""processes all files in destfiles, generate files from classes, compare and copy if necessary"""
|
||||
for df in destfiles.keys():
|
||||
debug.debug("Processing source files for %s." % df, 2)
|
||||
# assemble file to tmp
|
||||
commentstring = ""
|
||||
if dirConfig.check("Main", "commentstring"):
|
||||
commentstring = dirConfig.get("Main", "commentstring")
|
||||
debug.debug("Found commentstring %s in %s" % (commentstring, df), 3)
|
||||
# if COMMENTSTRNIG
|
||||
|
||||
tempfilename = buildFile(destfiles[df], df, commentstring)
|
||||
if not tempfilename:
|
||||
debug.debug("Error while creating temp file for %s, skipping." % df, 1)
|
||||
continue
|
||||
# if not tempfilename
|
||||
debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2)
|
||||
|
||||
# diff assembled file and config file
|
||||
if Tools.diff(df, tempfilename, commentstring, debug):
|
||||
debug.debug("File %s has changed" % df, 0)
|
||||
if not Tools.userConfigGenerated(df, cfg):
|
||||
debug.debug("%s not generated by userconfig, backing up." % df, 2)
|
||||
# file not generated from userconfig -> back up
|
||||
Tools.backupFile(df, debug)
|
||||
# if not userConfigGenerated
|
||||
# copy tmp file to real location
|
||||
debug.debug("Copy %s to %s." % (tempfilename, df), 0)
|
||||
Tools.copyFile(tempfilename, df, debug)
|
||||
# if diff
|
||||
# remove tmp
|
||||
debug.debug("Removing temporary file %s." % tempfilename, 2)
|
||||
os.remove(tempfilename)
|
||||
# for df
|
||||
# def buildAllFiles
|
||||
def process_all_files(destfiles, dir_config):
|
||||
"""processes all files in destfiles, generate files from classes, compare and copy if necessary"""
|
||||
for df in destfiles.keys():
|
||||
debug.debug("Processing source files for %s." % df, 2)
|
||||
# assemble file to tmp
|
||||
commentstring = ""
|
||||
if dir_config.check("Main", "commentstring"):
|
||||
commentstring = dir_config.get("Main", "commentstring")
|
||||
debug.debug("Found commentstring %s in %s" % (commentstring, df), 3)
|
||||
|
||||
tempfilename = build_file(destfiles[df], df, commentstring)
|
||||
if not tempfilename:
|
||||
debug.debug("Error while creating temp file for %s, skipping." % df, 1)
|
||||
continue
|
||||
debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2)
|
||||
|
||||
# diff assembled file and config file
|
||||
if Tools.diff(df, tempfilename, commentstring, debug):
|
||||
debug.debug("File %s has changed" % df, 0)
|
||||
if not Tools.user_config_generated(df, cfg):
|
||||
debug.debug("%s not generated by userconfig, backing up." % df, 2)
|
||||
# file not generated from userconfig -> back up
|
||||
Tools.backup_file(df, debug)
|
||||
# copy tmp file to real location
|
||||
debug.debug("Copy %s to %s." % (tempfilename, df), 0)
|
||||
Tools.copy_file(tempfilename, df, debug)
|
||||
# remove tmp
|
||||
debug.debug("Removing temporary file %s." % tempfilename, 2)
|
||||
os.remove(tempfilename)
|
||||
|
||||
|
||||
def main():
|
||||
configfile = os.environ['HOME']+"/etc/userconfig.cfg"
|
||||
global verbose
|
||||
|
||||
try:
|
||||
configfile = os.environ['HOME']+"/etc/userconfig.cfg"
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "hdc:v", ["help", "debug", "config="])
|
||||
except getopt.error, msg:
|
||||
raise Usage(msg)
|
||||
# try getopt
|
||||
|
||||
for option, value in opts:
|
||||
if option == "-v":
|
||||
debug.addverbose()
|
||||
if option in ("-h", "--help"):
|
||||
raise Usage(help_message)
|
||||
if option in ("-d", "--debug.debug"):
|
||||
pass
|
||||
if option in ("-c", "--config"):
|
||||
configfile = value
|
||||
# if
|
||||
# for option, value
|
||||
except Usage, err:
|
||||
Tools.error(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
|
||||
Tools.error("\t for help use --help")
|
||||
return 2
|
||||
# try
|
||||
|
||||
debug.debug("Using configfile %s." % configfile, 1)
|
||||
if not os.path.isfile(configfile):
|
||||
Tools.error( "No config file specified.")
|
||||
return 2
|
||||
# if configfile
|
||||
cfg.setdebug(debug)
|
||||
cfg.setfilename(configfile)
|
||||
|
||||
debug.debug("Current host is in classes %s" % hostclasses, 1)
|
||||
configdir = cfg.get("Main", "configdir")
|
||||
for d in os.listdir(configdir):
|
||||
destfiles = {}
|
||||
name = configdir+"/"+d
|
||||
debug.debug("Working in %s" % name, 1)
|
||||
if not os.path.isdir(name):
|
||||
debug.debug("%s is not a directory, skipping." % name, 3)
|
||||
continue
|
||||
elif d.startswith(".svn"):
|
||||
debug.debug("%s is .svn, skipping." % name, 3)
|
||||
continue
|
||||
elif os.path.isfile(name+"/.ignore"):
|
||||
debug.debug("%s contains file .ignore, skipping." % name, 3)
|
||||
continue
|
||||
else:
|
||||
debug.debug("Processing files in %s" % name, 2)
|
||||
(destfiles, dirConfig) = workdir(name)
|
||||
if isinstance(destfiles, dict):
|
||||
if len(destfiles.keys()) > 0:
|
||||
debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
|
||||
processAllFiles(destfiles, dirConfig)
|
||||
# if > 0
|
||||
else:
|
||||
debug.debug("No files found for %s, skipping." % name, 1)
|
||||
# if
|
||||
# for d
|
||||
# def main
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "hdc:v", ["help", "debug", "config="])
|
||||
except getopt.GetoptError as msg:
|
||||
raise Usage(msg)
|
||||
|
||||
for option, value in opts:
|
||||
if option == "-v":
|
||||
debug.addverbose()
|
||||
if option in ("-h", "--help"):
|
||||
raise Usage(help_message)
|
||||
if option in ("-d", "--debug.debug"):
|
||||
pass
|
||||
if option in ("-c", "--config"):
|
||||
configfile = value
|
||||
except Usage as err:
|
||||
Tools.error(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
|
||||
Tools.error("\t for help use --help")
|
||||
return 2
|
||||
|
||||
debug.debug("Using configfile %s." % configfile, 1)
|
||||
if not os.path.isfile(configfile):
|
||||
Tools.error("No config file specified.")
|
||||
return 2
|
||||
|
||||
cfg.setdebug(debug)
|
||||
cfg.setfilename(configfile)
|
||||
|
||||
debug.debug("Current host is in classes %s" % hostclasses, 1)
|
||||
configdir = cfg.get("Main", "configdir")
|
||||
for d in os.listdir(configdir):
|
||||
destfiles = {}
|
||||
name = configdir+"/"+d
|
||||
debug.debug("Working in %s" % name, 1)
|
||||
if not os.path.isdir(name):
|
||||
debug.debug("%s is not a directory, skipping." % name, 3)
|
||||
continue
|
||||
elif d.startswith(".svn"):
|
||||
debug.debug("%s is .svn, skipping." % name, 3)
|
||||
continue
|
||||
elif os.path.isfile(name+"/.ignore"):
|
||||
debug.debug("%s contains file .ignore, skipping." % name, 3)
|
||||
continue
|
||||
else:
|
||||
debug.debug("Processing files in %s" % name, 2)
|
||||
(destfiles, dirConfig) = workdir(name)
|
||||
if isinstance(destfiles, dict):
|
||||
if len(destfiles.keys()) > 0:
|
||||
debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
|
||||
process_all_files(destfiles, dirConfig)
|
||||
else:
|
||||
debug.debug("No files found for %s, skipping." % name, 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user