Alpha 2, diff muss umgeschrieben werden, damit er Kommentare ignoriert
This commit is contained in:
		
							
								
								
									
										4
									
								
								Tools.py
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Tools.py
									
									
									
									
									
								
							@@ -76,7 +76,7 @@ def getConfig(filename):
 | 
			
		||||
 | 
			
		||||
def diff(destfile, tempfile, commentstring, debug):
 | 
			
		||||
  """diff destfile and tempfile, returns True if files differ, False if they are the same"""
 | 
			
		||||
  # FIXME: filter out comments?
 | 
			
		||||
  # FIXME: filter out comments, SVN-Header and STAMP are causing diff errors
 | 
			
		||||
  debug.debug("Diffing %s and %s" % (destfile, tempfile))
 | 
			
		||||
  if not os.path.isfile(destfile):
 | 
			
		||||
    debug.debug("Destfile %s does not exist, returning True." % destfile)
 | 
			
		||||
@@ -137,7 +137,7 @@ def copyFile(sourcefile, destfile, debug):
 | 
			
		||||
  
 | 
			
		||||
  if os.path.isfile(sourcefile):
 | 
			
		||||
    # sourcefile exists
 | 
			
		||||
    if 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))
 | 
			
		||||
      shutil.copy(sourcefile, destfile)
 | 
			
		||||
      return True
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,8 @@ import os
 | 
			
		||||
import tempfile
 | 
			
		||||
import re
 | 
			
		||||
import getopt
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
# 
 | 
			
		||||
import cfgfile
 | 
			
		||||
from checks import checks
 | 
			
		||||
@@ -65,14 +67,14 @@ def workdir(directory):
 | 
			
		||||
  # 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 ({},{})
 | 
			
		||||
    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 ({},{})
 | 
			
		||||
    return ({},None)
 | 
			
		||||
  # if not dirConfig
 | 
			
		||||
  
 | 
			
		||||
  destdir = dirConfig.get("Main","DEST")
 | 
			
		||||
@@ -112,19 +114,24 @@ def workdir(directory):
 | 
			
		||||
  return (destfiles, dirConfig)
 | 
			
		||||
# def work
 | 
			
		||||
 | 
			
		||||
def buildFile(classfiles, destfile):
 | 
			
		||||
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 != "":
 | 
			
		||||
    content.append(commentstring + " " + cfg.get("Main","STAMP") + " " + time.strftime("%+") + "\n")
 | 
			
		||||
  # if commentstring not empty
 | 
			
		||||
  
 | 
			
		||||
  for f in classfiles:
 | 
			
		||||
    fp = open(f, "r")
 | 
			
		||||
    filecontent = fp.read()
 | 
			
		||||
    fp.close()
 | 
			
		||||
    
 | 
			
		||||
    # look for stamp in content, replace with real stamp
 | 
			
		||||
    if re.search(re.escape(cfg.get("Main","STAMPREPLACE")), filecontent):
 | 
			
		||||
      filecontent = re.sub(re.escape(cfg.get("Main","STAMPREPLACE")), cfg.get("Main","STAMP"), filecontent)
 | 
			
		||||
    # if match
 | 
			
		||||
    if commentstring == "":
 | 
			
		||||
      # look for stamp in content, replace with real stamp
 | 
			
		||||
      if re.search(re.escape(cfg.get("Main","STAMPREPLACE")), filecontent):
 | 
			
		||||
        filecontent = re.sub(re.escape(cfg.get("Main","STAMPREPLACE")), cfg.get("Main","STAMP"), filecontent)
 | 
			
		||||
      # if search
 | 
			
		||||
    # if commentstring empty  
 | 
			
		||||
    content.append(filecontent)
 | 
			
		||||
  # end f
 | 
			
		||||
  
 | 
			
		||||
@@ -150,18 +157,22 @@ def buildFile(classfiles, destfile):
 | 
			
		||||
 | 
			
		||||
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():
 | 
			
		||||
    # assemble file to tmp
 | 
			
		||||
    tempfilename = buildFile(destfiles[df], df)
 | 
			
		||||
    commentstring = ""
 | 
			
		||||
    if dirConfig.check("Main", "commentstring"):
 | 
			
		||||
      commentstring = dirConfig.get("Main", "commentstring")
 | 
			
		||||
      debug.debug("Found commentstring %s in %s" % (commentstring, df))
 | 
			
		||||
    # if COMMENTSTRNIG
 | 
			
		||||
        
 | 
			
		||||
    tempfilename = buildFile(destfiles[df], df, commentstring)
 | 
			
		||||
    if not tempfilename:
 | 
			
		||||
      debug.debug("Error while creating temp file for %s, skipping." % df)
 | 
			
		||||
      continue
 | 
			
		||||
    # if not tempfilename
 | 
			
		||||
    
 | 
			
		||||
    commentstring = ""
 | 
			
		||||
    if dirConfig.check("Main", "COMMENSTRING"):
 | 
			
		||||
      commentstring = dirConfig.get("Main", "COMMENTSTRING")
 | 
			
		||||
    # if COMMENTSTRNIG
 | 
			
		||||
    
 | 
			
		||||
    # diff assembled file and config file
 | 
			
		||||
    if Tools.diff(df, tempfilename, commentstring, debug):
 | 
			
		||||
@@ -216,6 +227,7 @@ def main():
 | 
			
		||||
  debug.debug("Classes for host: %s" % hostclasses)
 | 
			
		||||
  configdir = cfg.get("Main", "CONFIGDIR")
 | 
			
		||||
  for d in os.listdir(configdir):
 | 
			
		||||
    destfiles = {}
 | 
			
		||||
    name = configdir+d
 | 
			
		||||
    if not os.path.isdir(name):
 | 
			
		||||
      continue
 | 
			
		||||
@@ -226,7 +238,12 @@ def main():
 | 
			
		||||
    else:
 | 
			
		||||
      debug.debug("main: %s" % name)
 | 
			
		||||
      (destfiles, dirConfig) = workdir(name)
 | 
			
		||||
      processAllFiles(destfiles, dirConfig)
 | 
			
		||||
      if isinstance(destfiles, dict):
 | 
			
		||||
        if len(destfiles.keys()) > 0:
 | 
			
		||||
          processAllFiles(destfiles, dirConfig)
 | 
			
		||||
        # if > 0
 | 
			
		||||
      else:
 | 
			
		||||
        debug.debug("No destfiles for %s, skipping." % d)
 | 
			
		||||
    # if
 | 
			
		||||
  # for d
 | 
			
		||||
# def main
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user