diff --git a/checks.py b/checks.py index a640e7f..15f77ab 100644 --- a/checks.py +++ b/checks.py @@ -36,7 +36,7 @@ class checks(object): def arch(self): """docstring for arch""" - return(800, "ARCH", platform.system()) + return(800, "Arch", platform.system()) # def arch def hostname(self): diff --git a/userconfig.py b/userconfig.py index 6a69030..923e5db 100755 --- a/userconfig.py +++ b/userconfig.py @@ -13,21 +13,67 @@ Copyright (c) 2013 __MyCompanyName__. All rights reserved. import sys import os +import tempfile from checks import checks classchecks = checks() - configdir = "../" - hostclasses = classchecks.__classesForHost__() +verbose = 1 -def work(directory): +def debug(out, level=0): + """docstring for debug""" + if verbose: + if level > 0: + print "*"*level,out + else: + print out + # if verbose +# def debug + +def getDEST(directory): + """docstring for getDEST""" + f = open(directory+"/DEST") + destdir= f.read()[:-1] + if destdir == "$HOME": + destdir = os.environ['HOME'] + # if $HOME + if not destdir.endswith("/"): + destdir += "/" + # if not / + return destdir +# def getDEST + +def workconf(directory, destdir, depth=2): + """docstring for workconf""" + dirs = os.listdir(directory) + ret = [] + for d in dirs: + name = directory+"/"+d + if os.path.isdir(name): + # fixme: create name + workconf(name, destdir, depth+1) + # if dir + ret.append(name) + debug("workconf: found file %s" % name, depth) + # for d + return ret +# def workconf + +def workdir(directory): """docstring for work""" + debug("===============================", 1) + if not os.path.isfile(directory+"/DEST"): + debug("No DEST in %s, skipping." % directory, 1) return # if not DEST - dirs = os.listdir(directory) - dirs.remove("DEST") + destdir = getDEST(directory) + # 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 = {} + # Schritt 1: Dateien einlesen for h in hostclasses: if h[0] != "": classdir = directory+"/"+h[0]+"_"+h[1] @@ -35,14 +81,65 @@ def work(directory): classdir = directory+"/"+h[1] # if all if os.path.isdir(classdir): - print "Directory",classdir,"exists." + debug("workdir: %s" % classdir, 1) + tempfiles = workconf(classdir, destdir) + # tempfiles is a list of files within one classdir + + 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 + # for tempfiles # if classdir # for hostclasses - return + debug("workdir: %s, Files: %s" % (directory, str(destfiles))) + debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1) + return destfiles # def work +def buildFile(destfile, classfiles): + """docstring for buildFile""" + content = [] + for f in classfiles: + fp = open(f, "r") + content.append(fp.read()) + fp.close() + # end f + + (tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp") + + fp = None + try: + fp = os.fdopen(tempfd, "w") + except: + print "Cannot write to temporary file",tempfilename + os.remove(tempfilename) + sys.exit(1) + # try + + for block in content: + fp.write(block) + fp.write("\n") + # for content + fp.close() + # fixme: diff tempfilename destfile + # fixme: header marker for "is under userconfig control" + # fixme: if diff and not under userconfig control, backup destfile + # fixme: copy tempfilename to destfile + # fixme: remove tempfilename +# def buildFile + +def buildAllFiles(destfiles): + """docstring for buildFiles""" + for df in destfiles.keys(): + buildFile(df, destfiles[df]) + # for df +# def buildAllFiles + def main(): - print "Classes for host:",hostclasses + debug("Classes for host: %s" % hostclasses) for d in os.listdir(configdir): name = configdir+d if not os.path.isdir(name): @@ -52,7 +149,9 @@ def main(): elif os.path.isfile(name+"/.ignore"): continue else: - work(name) + debug("main: %s" % name) + destfiles = workdir(name) + buildAllFiles(destfiles) # if # for d # def main