Zwischenstand
This commit is contained in:
parent
d3f9de40a4
commit
74d3d8f1bb
64
cfgfile.py
Normal file
64
cfgfile.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# encoding: utf-8
|
||||||
|
#
|
||||||
|
# $Id$
|
||||||
|
# $URL$
|
||||||
|
|
||||||
|
"""
|
||||||
|
cfgfile.py
|
||||||
|
|
||||||
|
Created by Marcus Stoegbauer on 2013-01-12.
|
||||||
|
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
||||||
|
"""
|
||||||
|
from ConfigParser import ConfigParser
|
||||||
|
|
||||||
|
class Conf(object):
|
||||||
|
confobj = ConfigParser()
|
||||||
|
cfgfile = ''
|
||||||
|
|
||||||
|
def __init__(self, filename = None):
|
||||||
|
"""if filename is set, open config file and initialize the ConfigParser
|
||||||
|
"""
|
||||||
|
if filename:
|
||||||
|
self.setfilename(filename)
|
||||||
|
# if filename
|
||||||
|
# def __init__
|
||||||
|
|
||||||
|
def setfilename(self, filename):
|
||||||
|
"""initialize the ConfigParser
|
||||||
|
"""
|
||||||
|
if len(self.confobj.read(filename)) == 0 or self.confobj.read(filename)[0] != filename:
|
||||||
|
raise Exception('Cannot read config file ' + filename)
|
||||||
|
# if cannot read
|
||||||
|
self.cfgfile = filename
|
||||||
|
# def setfilename
|
||||||
|
|
||||||
|
def get(self, section, option):
|
||||||
|
"""returns the value of option in section
|
||||||
|
"""
|
||||||
|
if not self.cfgfile:
|
||||||
|
raise Exception('No config file set')
|
||||||
|
# if not cfgfile
|
||||||
|
return self.confobj.get(section, option)
|
||||||
|
# def get
|
||||||
|
|
||||||
|
def set(self, section, option, value):
|
||||||
|
"""docstring for update"""
|
||||||
|
self.confobj.set(section, option, value)
|
||||||
|
# def set
|
||||||
|
|
||||||
|
|
||||||
|
def getitems(self, section):
|
||||||
|
"""returns all items in section
|
||||||
|
"""
|
||||||
|
if not self.cfgfile:
|
||||||
|
raise Exception('No config file set')
|
||||||
|
# if not cfgfile
|
||||||
|
return self.confobj.items(section)
|
||||||
|
# def getitems
|
||||||
|
# class Conf
|
||||||
|
|
||||||
|
|
||||||
|
def check(self, section, option):
|
||||||
|
"""checks for option in section"""
|
||||||
|
return self.confobj.has_option(section, option)
|
15
checks.py
15
checks.py
@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
#
|
||||||
|
# $Id$
|
||||||
|
# $URL$
|
||||||
|
|
||||||
"""
|
"""
|
||||||
checks.py
|
checks.py
|
||||||
|
|
||||||
@ -29,9 +33,18 @@ class checks(object):
|
|||||||
return map(lambda k: (k[1],k[2]), sorted(classes, key=lambda k: k[0]))
|
return map(lambda k: (k[1],k[2]), sorted(classes, key=lambda k: k[0]))
|
||||||
# def __classesForHost__
|
# def __classesForHost__
|
||||||
|
|
||||||
|
def header(self):
|
||||||
|
"""docstring for header"""
|
||||||
|
return (0, "", "header")
|
||||||
|
# def header
|
||||||
|
|
||||||
|
def footer(self):
|
||||||
|
"""docstring for footer"""
|
||||||
|
return (1000, "", "footer")
|
||||||
|
|
||||||
def all(self):
|
def all(self):
|
||||||
"""docstring for all"""
|
"""docstring for all"""
|
||||||
return (999, "", "all")
|
return (998, "", "all")
|
||||||
# def all
|
# def all
|
||||||
|
|
||||||
def arch(self):
|
def arch(self):
|
||||||
|
214
userconfig.py
214
userconfig.py
@ -14,12 +14,28 @@ Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import re
|
||||||
|
import getopt
|
||||||
|
#
|
||||||
|
import cfgfile
|
||||||
from checks import checks
|
from checks import checks
|
||||||
|
|
||||||
classchecks = checks()
|
classchecks = checks()
|
||||||
configdir = "../"
|
cfg = cfgfile.Conf()
|
||||||
|
verbose = 0
|
||||||
|
|
||||||
hostclasses = classchecks.__classesForHost__()
|
hostclasses = classchecks.__classesForHost__()
|
||||||
verbose = 1
|
|
||||||
|
class Usage(Exception):
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = msg
|
||||||
|
|
||||||
|
|
||||||
|
help_message = """
|
||||||
|
-h help
|
||||||
|
-d debug
|
||||||
|
-c userconfig.cfg config file
|
||||||
|
"""
|
||||||
|
|
||||||
def debug(out, level=0):
|
def debug(out, level=0):
|
||||||
"""docstring for debug"""
|
"""docstring for debug"""
|
||||||
@ -31,28 +47,48 @@ def debug(out, level=0):
|
|||||||
# if verbose
|
# if verbose
|
||||||
# def debug
|
# def debug
|
||||||
|
|
||||||
def getDEST(directory):
|
def error(out):
|
||||||
"""docstring for getDEST"""
|
"""Print error on stderr"""
|
||||||
f = open(directory+"/DEST")
|
print >> sys.stderr, str(out)+"\n"
|
||||||
destdir= f.read()[:-1]
|
# def error
|
||||||
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):
|
def getConfig(filename):
|
||||||
"""docstring for workconf"""
|
"""reads filename as config, checks for DEST parameter and returns cfgfile object"""
|
||||||
|
try:
|
||||||
|
ret = cfgfile.Conf(filename)
|
||||||
|
except:
|
||||||
|
error("Error reading config file %s" % filename)
|
||||||
|
return False
|
||||||
|
# try
|
||||||
|
|
||||||
|
# check for DEST parameter
|
||||||
|
if not ret.check("Main", "DEST"):
|
||||||
|
error("No DEST in config file %s" % filename)
|
||||||
|
return False
|
||||||
|
# if no DEST
|
||||||
|
|
||||||
|
# replace $HOME with real home directory
|
||||||
|
if ret.get("Main", "DEST") == "$HOME":
|
||||||
|
ret.set("Main", "DEST", os.environ['HOME'])
|
||||||
|
# if $HOME
|
||||||
|
|
||||||
|
# make sure DEST ends with /
|
||||||
|
if not ret.get("Main", "DEST").endswith("/"):
|
||||||
|
ret.set("Main", "DEST", ret.get("Main", "DEST")+"/")
|
||||||
|
# if not /
|
||||||
|
|
||||||
|
return ret
|
||||||
|
# def getConfig
|
||||||
|
|
||||||
|
def workconf(directory, depth=2):
|
||||||
|
"""walks through directory, collecting all filenames, returns list of all filenames"""
|
||||||
dirs = os.listdir(directory)
|
dirs = os.listdir(directory)
|
||||||
ret = []
|
ret = []
|
||||||
for d in dirs:
|
for d in dirs:
|
||||||
name = directory+"/"+d
|
name = directory+"/"+d
|
||||||
if os.path.isdir(name):
|
if os.path.isdir(name):
|
||||||
# fixme: create name
|
# fixme: create name if it does not exist
|
||||||
workconf(name, destdir, depth+1)
|
workconf(name, depth+1)
|
||||||
# if dir
|
# if dir
|
||||||
ret.append(name)
|
ret.append(name)
|
||||||
debug("workconf: found file %s" % name, depth)
|
debug("workconf: found file %s" % name, depth)
|
||||||
@ -61,30 +97,47 @@ def workconf(directory, destdir, depth=2):
|
|||||||
# def workconf
|
# def workconf
|
||||||
|
|
||||||
def workdir(directory):
|
def workdir(directory):
|
||||||
"""docstring for work"""
|
"""walks through all host classes, checking if the classes directory exists in directory
|
||||||
|
then collect all filenames within this directory and return a dict of all files for this
|
||||||
|
directory
|
||||||
|
"""
|
||||||
debug("===============================", 1)
|
debug("===============================", 1)
|
||||||
|
|
||||||
if not os.path.isfile(directory+"/DEST"):
|
# skip directory if no CONFIGFILE present
|
||||||
debug("No DEST in %s, skipping." % directory, 1)
|
if not os.path.isfile(directory+"/"+cfg.get("Main", "CONFIGFILE")):
|
||||||
return
|
debug("No %s in %s, skipping." % (cfg.get("Main", "CONFIGFILE"), directory), 1)
|
||||||
|
return {}
|
||||||
# if not DEST
|
# if not DEST
|
||||||
destdir = getDEST(directory)
|
|
||||||
|
# get config file for directory
|
||||||
|
dirConfig = getConfig(directory+"/"+cfg.get("Main", "CONFIGFILE"))
|
||||||
|
if not dirConfig:
|
||||||
|
debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "CONFIGFILE"), directory), 1)
|
||||||
|
return {}
|
||||||
|
# if not dirConfig
|
||||||
|
|
||||||
|
destdir = dirConfig.get("Main","DEST")
|
||||||
# destfiles is a dict of all files that will be created from the classes config
|
# 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
|
# key is the destination filename, values are all classes filenames that are used to
|
||||||
# build the file
|
# build the file
|
||||||
destfiles = {}
|
destfiles = {}
|
||||||
# Schritt 1: Dateien einlesen
|
|
||||||
|
# walk through all know classes in directory and find filenames
|
||||||
for h in hostclasses:
|
for h in hostclasses:
|
||||||
|
# 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]
|
||||||
# if all
|
# if all
|
||||||
|
|
||||||
|
# if class directory exists
|
||||||
if os.path.isdir(classdir):
|
if os.path.isdir(classdir):
|
||||||
debug("workdir: %s" % classdir, 1)
|
debug("workdir: %s" % classdir, 1)
|
||||||
tempfiles = workconf(classdir, destdir)
|
# get list of files within this class directory
|
||||||
# tempfiles is a list of files within one classdir
|
tempfiles = workconf(classdir)
|
||||||
|
|
||||||
|
# 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 destfiles.has_key(destname):
|
if not destfiles.has_key(destname):
|
||||||
@ -94,18 +147,26 @@ def workdir(directory):
|
|||||||
# for tempfiles
|
# for tempfiles
|
||||||
# if classdir
|
# if classdir
|
||||||
# for hostclasses
|
# for hostclasses
|
||||||
|
|
||||||
debug("workdir: %s, Files: %s" % (directory, str(destfiles)))
|
debug("workdir: %s, Files: %s" % (directory, str(destfiles)))
|
||||||
debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1)
|
debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1)
|
||||||
return destfiles
|
return destfiles
|
||||||
# def work
|
# def work
|
||||||
|
|
||||||
def buildFile(destfile, classfiles):
|
def buildFile(classfiles, destfile):
|
||||||
"""docstring for buildFile"""
|
"""open all classfiles, assemble them and write the contents into a tempfile
|
||||||
|
returns the name of tempfile"""
|
||||||
content = []
|
content = []
|
||||||
for f in classfiles:
|
for f in classfiles:
|
||||||
fp = open(f, "r")
|
fp = open(f, "r")
|
||||||
content.append(fp.read())
|
filecontent = fp.read()
|
||||||
fp.close()
|
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
|
||||||
|
content.append(filecontent)
|
||||||
# end f
|
# end f
|
||||||
|
|
||||||
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
||||||
@ -114,9 +175,9 @@ def buildFile(destfile, classfiles):
|
|||||||
try:
|
try:
|
||||||
fp = os.fdopen(tempfd, "w")
|
fp = os.fdopen(tempfd, "w")
|
||||||
except:
|
except:
|
||||||
print "Cannot write to temporary file",tempfilename
|
error("Cannot write to temporary file %s" % tempfilename)
|
||||||
os.remove(tempfilename)
|
os.remove(tempfilename)
|
||||||
sys.exit(1)
|
return False
|
||||||
# try
|
# try
|
||||||
|
|
||||||
for block in content:
|
for block in content:
|
||||||
@ -124,22 +185,95 @@ def buildFile(destfile, classfiles):
|
|||||||
fp.write("\n")
|
fp.write("\n")
|
||||||
# for content
|
# for content
|
||||||
fp.close()
|
fp.close()
|
||||||
# fixme: diff tempfilename destfile
|
|
||||||
# fixme: header marker for "is under userconfig control"
|
return tempfilename
|
||||||
# fixme: if diff and not under userconfig control, backup destfile
|
|
||||||
# fixme: copy tempfilename to destfile
|
|
||||||
# fixme: remove tempfilename
|
|
||||||
# def buildFile
|
# def buildFile
|
||||||
|
|
||||||
def buildAllFiles(destfiles):
|
def diff(fileA, fileB):
|
||||||
"""docstring for buildFiles"""
|
"""diff fileA and fileB, returns True if files differ, False if they are the same"""
|
||||||
|
# FIXME: write
|
||||||
|
# FIXME: filter out comments?
|
||||||
|
# FIXME: COMMENTSTRING in config?
|
||||||
|
return False
|
||||||
|
# def diff
|
||||||
|
|
||||||
|
def userConfigGenerated(filename):
|
||||||
|
"""returns True if filename has been generated by userconfig, False else"""
|
||||||
|
# FIXME: write
|
||||||
|
return True
|
||||||
|
# def userConfigGenerated
|
||||||
|
|
||||||
|
def backupFile(filename):
|
||||||
|
"""make backup of filename, returns True if backup is successful, False else"""
|
||||||
|
# FIXME: write
|
||||||
|
return True
|
||||||
|
# def backupFile
|
||||||
|
|
||||||
|
def copyFile(sourcefile, destfile):
|
||||||
|
"""copy sourcefile to destfile, returns True if successful, False else"""
|
||||||
|
return True
|
||||||
|
# def copyFile
|
||||||
|
|
||||||
|
def processAllFiles(destfiles):
|
||||||
|
"""processes all files in destfiles, generate files from classes, compare and copy if necessary"""
|
||||||
for df in destfiles.keys():
|
for df in destfiles.keys():
|
||||||
buildFile(df, destfiles[df])
|
# assemble file to tmp
|
||||||
|
tempfilename = buildFile(destfiles[df], df)
|
||||||
|
if not tempfilename:
|
||||||
|
debug("Error while creating temp file for %s, skipping." % df)
|
||||||
|
continue
|
||||||
|
# if not tempfilename
|
||||||
|
|
||||||
|
# diff assembled file and config file
|
||||||
|
if diff(df, tempfilename):
|
||||||
|
if not userConfigGenerated(df):
|
||||||
|
# file not generated from userconfig -> back up
|
||||||
|
backupFile(df)
|
||||||
|
# if not userConfigGenerated
|
||||||
|
# copy tmp file to real location
|
||||||
|
copyFile(tempfilename, df)
|
||||||
|
# remove tmp
|
||||||
|
os.remove(tempfilename)
|
||||||
|
# if diff
|
||||||
# for df
|
# for df
|
||||||
# def buildAllFiles
|
# def buildAllFiles
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
configfile = ""
|
||||||
|
global verbose
|
||||||
|
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(sys.argv[1:], "hdc:v", ["help", "debug", "config="])
|
||||||
|
except getopt.error, msg:
|
||||||
|
raise Usage(msg)
|
||||||
|
# try getopt
|
||||||
|
|
||||||
|
for option, value in opts:
|
||||||
|
if option == "-v":
|
||||||
|
verbose = 1
|
||||||
|
if option in ("-h", "--help"):
|
||||||
|
raise Usage(help_message)
|
||||||
|
if option in ("-d", "--debug"):
|
||||||
|
pass
|
||||||
|
if option in ("-c", "--config"):
|
||||||
|
configfile = value
|
||||||
|
# if
|
||||||
|
# for option, value
|
||||||
|
except Usage, err:
|
||||||
|
error(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
|
||||||
|
error("\t for help use --help")
|
||||||
|
return 2
|
||||||
|
# try
|
||||||
|
|
||||||
|
if configfile == "":
|
||||||
|
error( "No config file specified.")
|
||||||
|
return 2
|
||||||
|
# if configfile
|
||||||
|
cfg.setfilename(configfile)
|
||||||
|
|
||||||
debug("Classes for host: %s" % hostclasses)
|
debug("Classes for host: %s" % hostclasses)
|
||||||
|
configdir = cfg.get("Main", "CONFIGDIR")
|
||||||
for d in os.listdir(configdir):
|
for d in os.listdir(configdir):
|
||||||
name = configdir+d
|
name = configdir+d
|
||||||
if not os.path.isdir(name):
|
if not os.path.isdir(name):
|
||||||
@ -151,7 +285,7 @@ def main():
|
|||||||
else:
|
else:
|
||||||
debug("main: %s" % name)
|
debug("main: %s" % name)
|
||||||
destfiles = workdir(name)
|
destfiles = workdir(name)
|
||||||
buildAllFiles(destfiles)
|
processAllFiles(destfiles)
|
||||||
# if
|
# if
|
||||||
# for d
|
# for d
|
||||||
# def main
|
# def main
|
||||||
|
Loading…
Reference in New Issue
Block a user