debug angepasst und konsequenter gemacht
This commit is contained in:
parent
3e40027923
commit
6c339db86b
@ -31,14 +31,16 @@ class Debug(object):
|
||||
self.verbose = verbose
|
||||
# def setverbose
|
||||
|
||||
def addverbose(self):
|
||||
"""docstring for setverbose"""
|
||||
self.verbose += 1
|
||||
# def addverbose
|
||||
|
||||
def debug(self, out, level=0):
|
||||
"""docstring for debug"""
|
||||
if self.verbose:
|
||||
if level > 0:
|
||||
print "*"*level,out
|
||||
else:
|
||||
print out
|
||||
# if verbose
|
||||
if self.verbose >= level:
|
||||
print out
|
||||
# if level
|
||||
# def debug
|
||||
# class Debug
|
||||
|
||||
@ -89,9 +91,9 @@ def read_skip_comment(fp, commentstring):
|
||||
|
||||
def diff(destfile, tempfile, commentstring, debug):
|
||||
"""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):
|
||||
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
|
||||
return True
|
||||
# if not destfile
|
||||
@ -142,6 +144,7 @@ def userConfigGenerated(filename, cfg):
|
||||
def backupFile(filename, debug):
|
||||
"""make backup of filename, returns True if backup is successful, False else"""
|
||||
if os.path.isfile(filename):
|
||||
debug.debug("%s exists, finding backup name." % filename, 3)
|
||||
backupname = filename+".userconfig."+time.strftime("%F")
|
||||
testbackupname = backupname
|
||||
counter = 0
|
||||
@ -149,9 +152,11 @@ def backupFile(filename, debug):
|
||||
counter+=1
|
||||
testbackupname=backupname+"."+str(counter)
|
||||
# while
|
||||
debug.debug("Renaming %s to %s" % (filename, testbackupname))
|
||||
debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
|
||||
os.rename(filename, testbackupname)
|
||||
return True
|
||||
else:
|
||||
debug.debug("%s does not exist, do not need backup." % filename, 3)
|
||||
# if filename
|
||||
return False
|
||||
# def backupFile
|
||||
@ -161,11 +166,14 @@ def copyFile(sourcefile, destfile, debug):
|
||||
|
||||
if os.path.isfile(sourcefile):
|
||||
# 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):
|
||||
debug.debug("Copying %s to %s" % (sourcefile, destfile))
|
||||
debug.debug("Copying %s to %s" % (sourcefile, destfile), 1)
|
||||
shutil.copy(sourcefile, destfile)
|
||||
return True
|
||||
# destfile is writable
|
||||
else:
|
||||
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
|
||||
# if write ok
|
||||
return False
|
||||
# def copyFile
|
||||
|
@ -12,10 +12,12 @@ Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
||||
"""
|
||||
import ConfigParser
|
||||
import os
|
||||
import re
|
||||
|
||||
class Conf(object):
|
||||
confobj = ConfigParser.RawConfigParser()
|
||||
cfgfile = ''
|
||||
debug = None
|
||||
|
||||
def __init__(self, filename = None):
|
||||
"""if filename is set, open config file and initialize the ConfigParser
|
||||
@ -26,6 +28,11 @@ class Conf(object):
|
||||
# if filename
|
||||
# def __init__
|
||||
|
||||
def setdebug(self, debug):
|
||||
"""docstring for setdebug"""
|
||||
self.debug = debug
|
||||
# def setdebug
|
||||
|
||||
def setfilename(self, filename):
|
||||
"""initialize the ConfigParser
|
||||
"""
|
||||
@ -34,9 +41,28 @@ class Conf(object):
|
||||
raise Exception('Cannot read config file ' + filename)
|
||||
# if cannot read
|
||||
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'])
|
||||
self.confobj.set("Main","configdir",configdir)
|
||||
for s in self.confobj.sections():
|
||||
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 get(self, section, option):
|
||||
|
@ -36,7 +36,7 @@ class Usage(Exception):
|
||||
# def __init__
|
||||
help_message = """
|
||||
-h help
|
||||
-d debug
|
||||
-v verbose level (multiple v for higher level)
|
||||
-c userconfig.cfg config file
|
||||
"""
|
||||
# class Usage
|
||||
@ -45,6 +45,7 @@ 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):
|
||||
@ -53,7 +54,7 @@ def workconf(directory, depth=2):
|
||||
# if dir
|
||||
if not name.endswith(".swp"):
|
||||
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
|
||||
# for d
|
||||
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
|
||||
directory
|
||||
"""
|
||||
debug.debug("===============================", 1)
|
||||
|
||||
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)
|
||||
@ -93,13 +93,13 @@ def workdir(directory):
|
||||
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("workdir: %s" % classdir, 1)
|
||||
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
|
||||
@ -107,12 +107,12 @@ def workdir(directory):
|
||||
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)))
|
||||
debug.debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1)
|
||||
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
|
||||
return (destfiles, dirConfig)
|
||||
# def work
|
||||
|
||||
@ -121,16 +121,19 @@ def buildFile(classfiles, destfile, commentstring):
|
||||
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()
|
||||
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
|
||||
@ -147,7 +150,7 @@ def buildFile(classfiles, destfile, commentstring):
|
||||
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")
|
||||
@ -159,37 +162,36 @@ def buildFile(classfiles, destfile, commentstring):
|
||||
|
||||
def processAllFiles(destfiles, dirConfig):
|
||||
"""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():
|
||||
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))
|
||||
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)
|
||||
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):
|
||||
print df,"changed",
|
||||
debug.debug("File %s has changed" % df, 0)
|
||||
if not Tools.userConfigGenerated(df, cfg):
|
||||
print "(trying to back up file)",
|
||||
debug.debug("%s not generated by userconfig, backing up." % df)
|
||||
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
|
||||
print "copying new version."
|
||||
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
|
||||
@ -207,7 +209,7 @@ def main():
|
||||
|
||||
for option, value in opts:
|
||||
if option == "-v":
|
||||
debug.setverbose(1)
|
||||
debug.addverbose()
|
||||
if option in ("-h", "--help"):
|
||||
raise Usage(help_message)
|
||||
if option in ("-d", "--debug.debug"):
|
||||
@ -222,32 +224,39 @@ def main():
|
||||
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("Classes for host: %s" % hostclasses)
|
||||
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
|
||||
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("main: %s" % name)
|
||||
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 destfiles for %s, skipping." % d)
|
||||
debug.debug("No files found for %s, skipping." % name, 1)
|
||||
# if
|
||||
# for d
|
||||
# def main
|
||||
|
Loading…
Reference in New Issue
Block a user