additional debug output, dont store hostclasses but generate new

This commit is contained in:
Marcus Stoegbauer 2019-06-19 00:04:42 +02:00
parent 7779ff5f70
commit 81d8a074cc
2 changed files with 45 additions and 34 deletions

View File

@ -95,11 +95,13 @@ def diff(destfile, tempfile, commentstring, debug):
if line1 != line2: if line1 != line2:
fp1.close() fp1.close()
fp2.close() fp2.close()
debug.debug("%s differs, return true" % destfile, 3)
return True return True
# if differ # if differ
# for line # for line
fp1.close() fp1.close()
fp2.close() fp2.close()
debug.debug("%s is the same, return false" % destfile, 3)
return False return False

View File

@ -25,8 +25,6 @@ debug = Tools.Debug()
classchecks = Checks() classchecks = Checks()
cfg = cfgfile.Conf() cfg = cfgfile.Conf()
hostclasses = classchecks.__classes_for_host__()
help_message = """ help_message = """
-h help -h help
-v verbose level (multiple v for higher level) -v verbose level (multiple v for higher level)
@ -43,7 +41,8 @@ def workconf(directory, depth=2):
"""walks through directory, collecting all filenames, returns list of all filenames""" """walks through directory, collecting all filenames, returns list of all filenames"""
dirs = os.listdir(directory) dirs = os.listdir(directory)
ret = [] ret = []
debug.debug("Finding files in directory %s." % directory, 4) debug.debug(" ================ workconf ===============", 1)
debug.debug(" Finding files in directory %s." % directory, 4)
for d in dirs: for d in dirs:
name = directory+"/"+d name = directory+"/"+d
if os.path.isdir(name): if os.path.isdir(name):
@ -54,7 +53,8 @@ def workconf(directory, depth=2):
if d == ".svn": if d == ".svn":
continue continue
ret.append(name) ret.append(name)
debug.debug("Found file %s in directory %s" % (name, directory), 4) debug.debug(" +++ Found file %s in directory %s" % (name, directory), 4)
debug.debug(" ================ workconf ===============", 1)
return ret return ret
@ -63,16 +63,17 @@ def workdir(directory):
then collect all filenames within this directory and return a dict of all files for this then collect all filenames within this directory and return a dict of all files for this
directory directory
""" """
debug.debug("Working on directory %s" % directory, 3) debug.debug(" ================ workdir ===============", 1)
debug.debug(" Working on directory %s" % directory, 3)
# skip directory if no CONFIGFILE present # skip directory if no CONFIGFILE present
if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")): if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")):
debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1) debug.debug(" --- No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
return {},None return {},None
# get config file for directory # get config file for directory
dir_config = Tools.get_config(directory + "/" + cfg.get("Main", "configfile")) dir_config = Tools.get_config(directory + "/" + cfg.get("Main", "configfile"))
if not dir_config: if not dir_config:
debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1) debug.debug(" --- Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
return {},None return {},None
destdir = dir_config.get("Main","dest") destdir = dir_config.get("Main","dest")
@ -82,28 +83,29 @@ def workdir(directory):
destfiles = {} destfiles = {}
# walk through all know classes in directory and find filenames # walk through all know classes in directory and find filenames
for h in hostclasses: for h in classchecks.__classes_for_host__():
# build classes directory # build classes directory
if h[0] != "": if h[0] != "":
classdir = directory+"/"+h[0]+"_"+h[1] classdir = directory+"/"+h[0]+"_"+h[1]
else: else:
classdir = directory+"/"+h[1] classdir = directory+"/"+h[1]
debug.debug("Looking for directory %s." % classdir, 4) debug.debug(" ??? Looking for directory %s." % classdir, 4)
# if class directory exists # if class directory exists
if os.path.isdir(classdir): if os.path.isdir(classdir):
debug.debug("Found directory %s, getting files." % classdir, 4) debug.debug(" +++ Found directory %s, getting files." % classdir, 4)
# get list of files within this class directory # get list of files within this class directory
tempfiles = workconf(classdir) tempfiles = workconf(classdir)
debug.debug("Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4) debug.debug(" +++ Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4)
# put files into dict # put files into dict
for f in tempfiles: for f in tempfiles:
destname = destdir+os.path.basename(f) # destination filename destname = destdir+os.path.basename(f) # destination filename
if not destname in destfiles: if not destname in destfiles:
destfiles[destname] = [] destfiles[destname] = []
destfiles[destname].append(f) # append each file to dict 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(" +++ 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) debug.debug(" === workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
debug.debug(" ================ workdir ===============", 1)
return destfiles, dir_config return destfiles, dir_config
@ -116,19 +118,20 @@ def build_file(classfiles, destfile, commentstring):
:return: str :return: str
""" """
content = [] content = []
debug.debug(" ================ build_file ===============", 1)
if commentstring != "": if commentstring != "":
debug.debug("commentstring found, adding header.", 3) debug.debug(" +++ commentstring found, adding header.", 3)
content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n") content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n")
for f in classfiles: for f in classfiles:
debug.debug("Merging %s." % f, 4) debug.debug(" +++ Merging %s." % f, 4)
fp = open(f, "r") fp = open(f, "r")
filecontent = fp.read() filecontent = fp.read()
fp.close() fp.close()
if commentstring == "": if commentstring == "":
# look for stamp in content, replace with real stamp # look for stamp in content, replace with real stamp
if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent): if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent):
debug.debug("commentstring empty, replacing stamp in file", 3) debug.debug(" +++ commentstring empty, replacing stamp in file", 3)
filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent) filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent)
content.append(filecontent) content.append(filecontent)
@ -140,45 +143,46 @@ def build_file(classfiles, destfile, commentstring):
Tools.error("Cannot write to temporary file %s" % tempfilename) Tools.error("Cannot write to temporary file %s" % tempfilename)
os.remove(tempfilename) os.remove(tempfilename)
return False return False
debug.debug("Writing merged files into tempfile %s." % tempfilename, 3) debug.debug(" +++ Writing merged files into tempfile %s." % tempfilename, 3)
for block in content: for block in content:
fp.write(block) fp.write(block)
fp.write("\n") fp.write("\n")
fp.close() fp.close()
debug.debug(" ================ build_file ===============", 1)
return tempfilename return tempfilename
def process_all_files(destfiles, dir_config): def process_all_files(destfiles, dir_config):
"""processes all files in destfiles, generate files from classes, compare and copy if necessary""" """processes all files in destfiles, generate files from classes, compare and copy if necessary"""
debug.debug(" ================ process_all_files ===============", 1)
for df in destfiles.keys(): for df in destfiles.keys():
debug.debug("Processing source files for %s." % df, 2) debug.debug(" ??? Processing source files for %s." % df, 2)
# assemble file to tmp # assemble file to tmp
commentstring = "" commentstring = ""
if dir_config.check("Main", "commentstring"): if dir_config.check("Main", "commentstring"):
commentstring = dir_config.get("Main", "commentstring") commentstring = dir_config.get("Main", "commentstring")
debug.debug("Found commentstring %s in %s" % (commentstring, df), 3) debug.debug(" +++ Found commentstring %s in %s" % (commentstring, df), 3)
tempfilename = build_file(destfiles[df], df, commentstring) tempfilename = build_file(destfiles[df], df, commentstring)
if not tempfilename: if not tempfilename:
debug.debug("Error while creating temp file for %s, skipping." % df, 1) debug.debug(" --- Error while creating temp file for %s, skipping." % df, 1)
continue continue
debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2) debug.debug(" +++ Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2)
# diff assembled file and config file # diff assembled file and config file
if Tools.diff(df, tempfilename, commentstring, debug): if Tools.diff(df, tempfilename, commentstring, debug):
debug.debug("File %s has changed" % df, 0) debug.debug("File %s has changed" % df, 0)
if not Tools.user_config_generated(df, cfg): if not Tools.user_config_generated(df, cfg):
debug.debug("%s not generated by userconfig, backing up." % df, 2) debug.debug(" +++ %s not generated by userconfig, backing up." % df, 2)
# file not generated from userconfig -> back up # file not generated from userconfig -> back up
Tools.backup_file(df, debug) Tools.backup_file(df, debug)
# copy tmp file to real location # copy tmp file to real location
debug.debug("Copy %s to %s." % (tempfilename, df), 0) debug.debug("Copy %s to %s." % (tempfilename, df), 0)
Tools.copy_file(tempfilename, df, debug) Tools.copy_file(tempfilename, df, debug)
# remove tmp # remove tmp
debug.debug("Removing temporary file %s." % tempfilename, 2) debug.debug(" +++ Removing temporary file %s." % tempfilename, 2)
os.remove(tempfilename) os.remove(tempfilename)
debug.debug(" ================ process_all_files ===============", 1)
def main(): def main():
configfile = os.environ['HOME']+"/etc/userconfig.cfg" configfile = os.environ['HOME']+"/etc/userconfig.cfg"
@ -203,6 +207,7 @@ def main():
Tools.error("\t for help use --help") Tools.error("\t for help use --help")
return 2 return 2
debug.debug("Verbose level is %d" % debug.verbose, 1)
debug.debug("Using configfile %s." % configfile, 1) debug.debug("Using configfile %s." % configfile, 1)
if not os.path.isfile(configfile): if not os.path.isfile(configfile):
Tools.error("No config file specified.") Tools.error("No config file specified.")
@ -211,30 +216,34 @@ def main():
cfg.setdebug(debug) cfg.setdebug(debug)
cfg.setfilename(configfile) cfg.setfilename(configfile)
debug.debug("Current host is in classes %s" % hostclasses, 1) debug.debug("================ main ===============", 1)
tempclasses = ""
for h in classchecks.__classes_for_host__():
tempclasses=tempclasses + str(h) + ","
debug.debug("+++ Current host is in classes %s" % tempclasses, 1)
configdir = cfg.get("Main", "configdir") configdir = cfg.get("Main", "configdir")
for d in os.listdir(configdir): for d in os.listdir(configdir):
destfiles = {} destfiles = {}
name = configdir+"/"+d name = configdir+"/"+d
debug.debug("Working in %s" % name, 1) debug.debug("+++ Working in %s" % name, 1)
if not os.path.isdir(name): if not os.path.isdir(name):
debug.debug("%s is not a directory, skipping." % name, 3) debug.debug("--- %s is not a directory, skipping." % name, 3)
continue continue
elif d.startswith(".svn"): elif d.startswith(".svn") or d.startswith(".git"):
debug.debug("%s is .svn, skipping." % name, 3) debug.debug("--- %s is .svn or .git, skipping." % name, 3)
continue continue
elif os.path.isfile(name+"/.ignore"): elif os.path.isfile(name+"/.ignore"):
debug.debug("%s contains file .ignore, skipping." % name, 3) debug.debug("--- %s contains file .ignore, skipping." % name, 3)
continue continue
else: else:
debug.debug("Processing files in %s" % name, 2) debug.debug("+++ Processing files in %s" % name, 2)
(destfiles, dirConfig) = workdir(name) (destfiles, dirConfig) = workdir(name)
if isinstance(destfiles, dict): if isinstance(destfiles, dict):
if len(destfiles.keys()) > 0: if len(destfiles.keys()) > 0:
debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3) debug.debug("+++ Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
process_all_files(destfiles, dirConfig) process_all_files(destfiles, dirConfig)
else: else:
debug.debug("No files found for %s, skipping." % name, 1) debug.debug("--- No files found for %s, skipping." % name, 1)
if __name__ == '__main__': if __name__ == '__main__':