2013-01-11 00:35:09 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
# $URL$
|
|
|
|
|
|
|
|
"""
|
|
|
|
userconfig.py
|
|
|
|
|
|
|
|
Created by Marcus Stoegbauer on 2013-01-10.
|
|
|
|
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2013-01-12 17:17:12 +01:00
|
|
|
import tempfile
|
2013-01-12 23:04:52 +01:00
|
|
|
import re
|
|
|
|
import getopt
|
|
|
|
#
|
|
|
|
import cfgfile
|
2013-01-11 00:35:09 +01:00
|
|
|
from checks import checks
|
|
|
|
|
|
|
|
classchecks = checks()
|
2013-01-12 23:04:52 +01:00
|
|
|
cfg = cfgfile.Conf()
|
|
|
|
verbose = 0
|
|
|
|
|
2013-01-11 00:35:09 +01:00
|
|
|
hostclasses = classchecks.__classesForHost__()
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
class Usage(Exception):
|
|
|
|
def __init__(self, msg):
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
|
|
|
|
help_message = """
|
|
|
|
-h help
|
|
|
|
-d debug
|
|
|
|
-c userconfig.cfg config file
|
|
|
|
"""
|
2013-01-12 17:17:12 +01:00
|
|
|
|
|
|
|
def debug(out, level=0):
|
|
|
|
"""docstring for debug"""
|
|
|
|
if verbose:
|
|
|
|
if level > 0:
|
|
|
|
print "*"*level,out
|
|
|
|
else:
|
|
|
|
print out
|
|
|
|
# if verbose
|
|
|
|
# def debug
|
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
def error(out):
|
|
|
|
"""Print error on stderr"""
|
|
|
|
print >> sys.stderr, str(out)+"\n"
|
|
|
|
# def error
|
|
|
|
|
|
|
|
def getConfig(filename):
|
|
|
|
"""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'])
|
2013-01-12 17:17:12 +01:00
|
|
|
# if $HOME
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# make sure DEST ends with /
|
|
|
|
if not ret.get("Main", "DEST").endswith("/"):
|
|
|
|
ret.set("Main", "DEST", ret.get("Main", "DEST")+"/")
|
2013-01-12 17:17:12 +01:00
|
|
|
# if not /
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
return ret
|
|
|
|
# def getConfig
|
2013-01-11 00:35:09 +01:00
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
def workconf(directory, depth=2):
|
|
|
|
"""walks through directory, collecting all filenames, returns list of all filenames"""
|
2013-01-12 17:17:12 +01:00
|
|
|
dirs = os.listdir(directory)
|
|
|
|
ret = []
|
|
|
|
for d in dirs:
|
|
|
|
name = directory+"/"+d
|
|
|
|
if os.path.isdir(name):
|
2013-01-12 23:04:52 +01:00
|
|
|
# fixme: create name if it does not exist
|
|
|
|
workconf(name, depth+1)
|
2013-01-12 17:17:12 +01:00
|
|
|
# if dir
|
|
|
|
ret.append(name)
|
|
|
|
debug("workconf: found file %s" % name, depth)
|
|
|
|
# for d
|
|
|
|
return ret
|
|
|
|
# def workconf
|
|
|
|
|
|
|
|
def workdir(directory):
|
2013-01-12 23:04:52 +01:00
|
|
|
"""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
|
|
|
|
"""
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("===============================", 1)
|
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
# skip directory if no CONFIGFILE present
|
|
|
|
if not os.path.isfile(directory+"/"+cfg.get("Main", "CONFIGFILE")):
|
|
|
|
debug("No %s in %s, skipping." % (cfg.get("Main", "CONFIGFILE"), directory), 1)
|
|
|
|
return {}
|
2013-01-11 00:35:09 +01:00
|
|
|
# if not DEST
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# 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")
|
2013-01-12 17:17:12 +01:00
|
|
|
# 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 = {}
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# walk through all know classes in directory and find filenames
|
2013-01-11 00:35:09 +01:00
|
|
|
for h in hostclasses:
|
2013-01-12 23:04:52 +01:00
|
|
|
# build classes directory
|
2013-01-11 00:35:09 +01:00
|
|
|
if h[0] != "":
|
|
|
|
classdir = directory+"/"+h[0]+"_"+h[1]
|
|
|
|
else:
|
|
|
|
classdir = directory+"/"+h[1]
|
|
|
|
# if all
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# if class directory exists
|
2013-01-11 00:35:09 +01:00
|
|
|
if os.path.isdir(classdir):
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("workdir: %s" % classdir, 1)
|
2013-01-12 23:04:52 +01:00
|
|
|
# get list of files within this class directory
|
|
|
|
tempfiles = workconf(classdir)
|
2013-01-12 17:17:12 +01:00
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
# put files into dict
|
2013-01-12 17:17:12 +01:00
|
|
|
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
|
2013-01-11 00:35:09 +01:00
|
|
|
# if classdir
|
|
|
|
# for hostclasses
|
2013-01-12 23:04:52 +01:00
|
|
|
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("workdir: %s, Files: %s" % (directory, str(destfiles)))
|
|
|
|
debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", 1)
|
|
|
|
return destfiles
|
2013-01-11 00:35:09 +01:00
|
|
|
# def work
|
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
def buildFile(classfiles, destfile):
|
|
|
|
"""open all classfiles, assemble them and write the contents into a tempfile
|
|
|
|
returns the name of tempfile"""
|
2013-01-12 17:17:12 +01:00
|
|
|
content = []
|
|
|
|
for f in classfiles:
|
|
|
|
fp = open(f, "r")
|
2013-01-12 23:04:52 +01:00
|
|
|
filecontent = fp.read()
|
2013-01-12 17:17:12 +01:00
|
|
|
fp.close()
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# 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)
|
2013-01-12 17:17:12 +01:00
|
|
|
# end f
|
|
|
|
|
|
|
|
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
2013-01-12 23:04:52 +01:00
|
|
|
|
2013-01-12 17:17:12 +01:00
|
|
|
fp = None
|
|
|
|
try:
|
|
|
|
fp = os.fdopen(tempfd, "w")
|
|
|
|
except:
|
2013-01-12 23:04:52 +01:00
|
|
|
error("Cannot write to temporary file %s" % tempfilename)
|
2013-01-12 17:17:12 +01:00
|
|
|
os.remove(tempfilename)
|
2013-01-12 23:04:52 +01:00
|
|
|
return False
|
2013-01-12 17:17:12 +01:00
|
|
|
# try
|
|
|
|
|
|
|
|
for block in content:
|
|
|
|
fp.write(block)
|
|
|
|
fp.write("\n")
|
|
|
|
# for content
|
|
|
|
fp.close()
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
return tempfilename
|
2013-01-12 17:17:12 +01:00
|
|
|
# def buildFile
|
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
def diff(fileA, fileB):
|
|
|
|
"""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"""
|
2013-01-12 17:17:12 +01:00
|
|
|
for df in destfiles.keys():
|
2013-01-12 23:04:52 +01:00
|
|
|
# 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
|
2013-01-12 17:17:12 +01:00
|
|
|
# for df
|
|
|
|
# def buildAllFiles
|
|
|
|
|
2013-01-11 00:35:09 +01:00
|
|
|
def main():
|
2013-01-12 23:04:52 +01:00
|
|
|
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)
|
|
|
|
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("Classes for host: %s" % hostclasses)
|
2013-01-12 23:04:52 +01:00
|
|
|
configdir = cfg.get("Main", "CONFIGDIR")
|
2013-01-11 00:35:09 +01:00
|
|
|
for d in os.listdir(configdir):
|
|
|
|
name = configdir+d
|
|
|
|
if not os.path.isdir(name):
|
|
|
|
continue
|
|
|
|
elif d.startswith(".svn"):
|
|
|
|
continue
|
|
|
|
elif os.path.isfile(name+"/.ignore"):
|
|
|
|
continue
|
|
|
|
else:
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("main: %s" % name)
|
|
|
|
destfiles = workdir(name)
|
2013-01-12 23:04:52 +01:00
|
|
|
processAllFiles(destfiles)
|
2013-01-11 00:35:09 +01:00
|
|
|
# if
|
|
|
|
# for d
|
|
|
|
# def main
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|