debug angepasst und konsequenter gemacht

This commit is contained in:
Marcus Stoegbauer 2013-01-14 22:23:48 +00:00
parent 3e40027923
commit 6c339db86b
3 changed files with 84 additions and 41 deletions

View File

@ -31,14 +31,16 @@ class Debug(object):
self.verbose = verbose self.verbose = verbose
# def setverbose # def setverbose
def addverbose(self):
"""docstring for setverbose"""
self.verbose += 1
# def addverbose
def debug(self, out, level=0): def debug(self, out, level=0):
"""docstring for debug""" """docstring for debug"""
if self.verbose: if self.verbose >= level:
if level > 0:
print "*"*level,out
else:
print out print out
# if verbose # if level
# def debug # def debug
# class Debug # class Debug
@ -89,9 +91,9 @@ def read_skip_comment(fp, commentstring):
def diff(destfile, tempfile, commentstring, debug): def diff(destfile, tempfile, commentstring, debug):
"""diff destfile and tempfile, returns True if files differ, False if they are the same""" """diff destfile and tempfile, returns True if files differ, False if they are the same"""
debug.debug("Diffing %s and %s" % (destfile, tempfile)) debug.debug("Diffing %s and %s" % (destfile, tempfile), 3)
if not os.path.isfile(destfile): if not os.path.isfile(destfile):
debug.debug("Destfile %s does not exist, returning True." % destfile) debug.debug("Destfile %s does not exist, returning True." % destfile, 3)
# destfile does not exist -> copy tempfile over # destfile does not exist -> copy tempfile over
return True return True
# if not destfile # if not destfile
@ -142,6 +144,7 @@ def userConfigGenerated(filename, cfg):
def backupFile(filename, debug): def backupFile(filename, debug):
"""make backup of filename, returns True if backup is successful, False else""" """make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename): if os.path.isfile(filename):
debug.debug("%s exists, finding backup name." % filename, 3)
backupname = filename+".userconfig."+time.strftime("%F") backupname = filename+".userconfig."+time.strftime("%F")
testbackupname = backupname testbackupname = backupname
counter = 0 counter = 0
@ -149,9 +152,11 @@ def backupFile(filename, debug):
counter+=1 counter+=1
testbackupname=backupname+"."+str(counter) testbackupname=backupname+"."+str(counter)
# while # while
debug.debug("Renaming %s to %s" % (filename, testbackupname)) debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
os.rename(filename, testbackupname) os.rename(filename, testbackupname)
return True return True
else:
debug.debug("%s does not exist, do not need backup." % filename, 3)
# if filename # if filename
return False return False
# def backupFile # def backupFile
@ -161,11 +166,14 @@ def copyFile(sourcefile, destfile, debug):
if os.path.isfile(sourcefile): if os.path.isfile(sourcefile):
# sourcefile exists # sourcefile exists
debug.debug("Source file %s exists, proceeding with copy." % sourcefile, 3)
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK): if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
debug.debug("Copying %s to %s" % (sourcefile, destfile)) debug.debug("Copying %s to %s" % (sourcefile, destfile), 1)
shutil.copy(sourcefile, destfile) shutil.copy(sourcefile, destfile)
return True return True
# destfile is writable # destfile is writable
else:
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
# if write ok # if write ok
return False return False
# def copyFile # def copyFile

View File

@ -12,10 +12,12 @@ Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
import ConfigParser import ConfigParser
import os import os
import re
class Conf(object): class Conf(object):
confobj = ConfigParser.RawConfigParser() confobj = ConfigParser.RawConfigParser()
cfgfile = '' cfgfile = ''
debug = None
def __init__(self, filename = None): def __init__(self, filename = None):
"""if filename is set, open config file and initialize the ConfigParser """if filename is set, open config file and initialize the ConfigParser
@ -26,6 +28,11 @@ class Conf(object):
# if filename # if filename
# def __init__ # def __init__
def setdebug(self, debug):
"""docstring for setdebug"""
self.debug = debug
# def setdebug
def setfilename(self, filename): def setfilename(self, filename):
"""initialize the ConfigParser """initialize the ConfigParser
""" """
@ -34,9 +41,28 @@ class Conf(object):
raise Exception('Cannot read config file ' + filename) raise Exception('Cannot read config file ' + filename)
# if cannot read # if cannot read
self.cfgfile = filename self.cfgfile = filename
if self.debug:
self.debug.debug("Read config file %s" % filename, 2)
self.debug.debug("Replacing environment variables in %s." % filename, 3)
# if debug
configdir=self.get("Main","configdir").replace("$HOME",os.environ['HOME']) for s in self.confobj.sections():
self.confobj.set("Main","configdir",configdir) for (i, val) in self.confobj.items(s):
tempre = re.search("\$([A-Z]+)[^A-Z]*", val)
if tempre:
varname = tempre.group(1)
if self.debug:
self.debug.debug("Found variable %s in %s." % (varname, i), 3)
# if debug
if os.environ.has_key(varname):
if self.debug:
self.debug.debug("%s exists in environment, replacing with %s." % (varname, os.environ[varname]))
# if debug
self.set(s, i, val.replace("$"+varname, os.environ[varname]))
# if has_key
# if tempre
# for i
# for s
# def setfilename # def setfilename
def get(self, section, option): def get(self, section, option):

View File

@ -36,7 +36,7 @@ class Usage(Exception):
# def __init__ # def __init__
help_message = """ help_message = """
-h help -h help
-d debug -v verbose level (multiple v for higher level)
-c userconfig.cfg config file -c userconfig.cfg config file
""" """
# class Usage # class Usage
@ -45,6 +45,7 @@ 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)
for d in dirs: for d in dirs:
name = directory+"/"+d name = directory+"/"+d
if os.path.isdir(name): if os.path.isdir(name):
@ -53,7 +54,7 @@ def workconf(directory, depth=2):
# if dir # if dir
if not name.endswith(".swp"): if not name.endswith(".swp"):
ret.append(name) ret.append(name)
debug.debug("workconf: found file %s" % name, depth) debug.debug("Found file %s in directory %s" % (name, directory), 4)
# if not .swp # if not .swp
# for d # for d
return ret return ret
@ -64,8 +65,7 @@ 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("===============================", 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)
@ -93,13 +93,13 @@ def workdir(directory):
else: else:
classdir = directory+"/"+h[1] classdir = directory+"/"+h[1]
# if all # if all
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("workdir: %s" % classdir, 1) 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)
# 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
@ -107,12 +107,12 @@ def workdir(directory):
destfiles[destname] = [] destfiles[destname] = []
# if not destname, create key with empty list # if not destname, create key with empty list
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)
# for tempfiles # for tempfiles
# if classdir # if classdir
# for hostclasses # for hostclasses
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles))) debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
debug.debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1)
return (destfiles, dirConfig) return (destfiles, dirConfig)
# def work # def work
@ -121,16 +121,19 @@ def buildFile(classfiles, destfile, commentstring):
returns the name of tempfile""" returns the name of tempfile"""
content = [] content = []
if commentstring != "": if commentstring != "":
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")
# if commentstring not empty # if commentstring not empty
for f in classfiles: for f in classfiles:
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)
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)
# if search # if search
# if commentstring empty # if commentstring empty
@ -147,7 +150,7 @@ def buildFile(classfiles, destfile, commentstring):
os.remove(tempfilename) os.remove(tempfilename)
return False return False
# try # try
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")
@ -159,37 +162,36 @@ def buildFile(classfiles, destfile, commentstring):
def processAllFiles(destfiles, dirConfig): def processAllFiles(destfiles, dirConfig):
"""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("processAllFiles, dirConfig: %s" % (str(dirConfig.getitems("Main"))))
for df in destfiles.keys(): for df in destfiles.keys():
debug.debug("Processing source files for %s." % df, 2)
# assemble file to tmp # assemble file to tmp
commentstring = "" commentstring = ""
if dirConfig.check("Main", "commentstring"): if dirConfig.check("Main", "commentstring"):
commentstring = dirConfig.get("Main", "commentstring") commentstring = dirConfig.get("Main", "commentstring")
debug.debug("Found commentstring %s in %s" % (commentstring, df)) debug.debug("Found commentstring %s in %s" % (commentstring, df), 3)
# if COMMENTSTRNIG # if COMMENTSTRNIG
tempfilename = buildFile(destfiles[df], df, commentstring) tempfilename = buildFile(destfiles[df], df, commentstring)
if not tempfilename: if not tempfilename:
debug.debug("Error while creating temp file for %s, skipping." % df) debug.debug("Error while creating temp file for %s, skipping." % df, 1)
continue continue
# if not tempfilename # if not tempfilename
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):
print df,"changed", debug.debug("File %s has changed" % df, 0)
if not Tools.userConfigGenerated(df, cfg): if not Tools.userConfigGenerated(df, cfg):
print "(trying to back up file)", debug.debug("%s not generated by userconfig, backing up." % df, 2)
debug.debug("%s not generated by userconfig, backing up." % df)
# file not generated from userconfig -> back up # file not generated from userconfig -> back up
Tools.backupFile(df, debug) Tools.backupFile(df, debug)
# if not userConfigGenerated # if not userConfigGenerated
# copy tmp file to real location # copy tmp file to real location
print "copying new version." debug.debug("Copy %s to %s." % (tempfilename, df), 0)
Tools.copyFile(tempfilename, df, debug) Tools.copyFile(tempfilename, df, debug)
# if diff # if diff
# remove tmp # remove tmp
debug.debug("Removing temporary file %s." % tempfilename, 2)
os.remove(tempfilename) os.remove(tempfilename)
# for df # for df
# def buildAllFiles # def buildAllFiles
@ -207,7 +209,7 @@ def main():
for option, value in opts: for option, value in opts:
if option == "-v": if option == "-v":
debug.setverbose(1) debug.addverbose()
if option in ("-h", "--help"): if option in ("-h", "--help"):
raise Usage(help_message) raise Usage(help_message)
if option in ("-d", "--debug.debug"): if option in ("-d", "--debug.debug"):
@ -222,32 +224,39 @@ def main():
return 2 return 2
# try # try
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.")
return 2 return 2
# if configfile # if configfile
cfg.setdebug(debug)
cfg.setfilename(configfile) cfg.setfilename(configfile)
debug.debug("Classes for host: %s" % hostclasses) debug.debug("Current host is in classes %s" % hostclasses, 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)
if not os.path.isdir(name): if not os.path.isdir(name):
debug.debug("%s is not a directory, skipping." % name, 3)
continue continue
elif d.startswith(".svn"): elif d.startswith(".svn"):
debug.debug("%s is .svn, 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)
continue continue
else: else:
debug.debug("main: %s" % name) 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)
processAllFiles(destfiles, dirConfig) processAllFiles(destfiles, dirConfig)
# if > 0 # if > 0
else: else:
debug.debug("No destfiles for %s, skipping." % d) debug.debug("No files found for %s, skipping." % name, 1)
# if # if
# for d # for d
# def main # def main