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
|
2013-01-13 01:04:40 +01:00
|
|
|
import time
|
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
#
|
2013-01-13 22:49:18 +01:00
|
|
|
import Userconfig.cfgfile as cfgfile
|
|
|
|
from Userconfig.checks import checks
|
|
|
|
import Userconfig.Tools as Tools
|
2013-01-13 00:21:20 +01:00
|
|
|
|
|
|
|
debug = Tools.Debug()
|
2013-01-11 00:35:09 +01:00
|
|
|
|
|
|
|
classchecks = checks()
|
2013-01-12 23:04:52 +01:00
|
|
|
cfg = cfgfile.Conf()
|
|
|
|
|
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
|
2013-01-13 00:21:20 +01:00
|
|
|
# def __init__
|
|
|
|
help_message = """
|
2013-01-12 23:04:52 +01:00
|
|
|
-h help
|
2013-01-14 23:23:48 +01:00
|
|
|
-v verbose level (multiple v for higher level)
|
2013-01-12 23:04:52 +01:00
|
|
|
-c userconfig.cfg config file
|
|
|
|
"""
|
2013-01-13 00:21:20 +01:00
|
|
|
# class Usage
|
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 = []
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Finding files in directory %s." % directory, 4)
|
2013-01-12 17:17:12 +01:00
|
|
|
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
|
2013-01-13 13:27:33 +01:00
|
|
|
if not name.endswith(".swp"):
|
|
|
|
ret.append(name)
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Found file %s in directory %s" % (name, directory), 4)
|
2013-01-13 13:27:33 +01:00
|
|
|
# if not .swp
|
2013-01-12 17:17:12 +01:00
|
|
|
# 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-14 23:23:48 +01:00
|
|
|
"""
|
|
|
|
debug.debug("Working on directory %s" % directory, 3)
|
2013-01-12 23:04:52 +01:00
|
|
|
# skip directory if no CONFIGFILE present
|
2013-01-13 22:32:33 +01:00
|
|
|
if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")):
|
|
|
|
debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
2013-01-13 01:04:40 +01:00
|
|
|
return ({},None)
|
2013-01-11 00:35:09 +01:00
|
|
|
# if not DEST
|
2013-01-12 23:04:52 +01:00
|
|
|
|
|
|
|
# get config file for directory
|
2013-01-13 22:32:33 +01:00
|
|
|
dirConfig = Tools.getConfig(directory+"/"+cfg.get("Main", "configfile"))
|
2013-01-12 23:04:52 +01:00
|
|
|
if not dirConfig:
|
2013-01-13 22:32:33 +01:00
|
|
|
debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
|
2013-01-13 01:04:40 +01:00
|
|
|
return ({},None)
|
2013-01-12 23:04:52 +01:00
|
|
|
# if not dirConfig
|
|
|
|
|
2013-01-13 22:32:33 +01:00
|
|
|
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-14 23:23:48 +01:00
|
|
|
debug.debug("Looking for directory %s." % classdir, 4)
|
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-14 23:23:48 +01:00
|
|
|
debug.debug("Found directory %s, getting files." % classdir, 4)
|
2013-01-12 23:04:52 +01:00
|
|
|
# get list of files within this class directory
|
|
|
|
tempfiles = workconf(classdir)
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4)
|
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
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4)
|
2013-01-12 17:17:12 +01:00
|
|
|
# for tempfiles
|
2013-01-11 00:35:09 +01:00
|
|
|
# if classdir
|
|
|
|
# for hostclasses
|
2013-01-12 23:04:52 +01:00
|
|
|
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
|
2013-01-13 00:21:20 +01:00
|
|
|
return (destfiles, dirConfig)
|
2013-01-11 00:35:09 +01:00
|
|
|
# def work
|
|
|
|
|
2013-01-13 01:04:40 +01:00
|
|
|
def buildFile(classfiles, destfile, commentstring):
|
2013-01-12 23:04:52 +01:00
|
|
|
"""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 = []
|
2013-01-13 01:04:40 +01:00
|
|
|
if commentstring != "":
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("commentstring found, adding header.", 3)
|
2013-01-13 22:32:33 +01:00
|
|
|
content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n")
|
2013-01-13 01:04:40 +01:00
|
|
|
# if commentstring not empty
|
|
|
|
|
2013-01-12 17:17:12 +01:00
|
|
|
for f in classfiles:
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Merging %s." % f, 4)
|
2013-01-12 17:17:12 +01:00
|
|
|
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-13 01:04:40 +01:00
|
|
|
if commentstring == "":
|
|
|
|
# look for stamp in content, replace with real stamp
|
2013-01-13 22:32:33 +01:00
|
|
|
if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("commentstring empty, replacing stamp in file", 3)
|
2013-01-13 22:32:33 +01:00
|
|
|
filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent)
|
2013-01-13 01:04:40 +01:00
|
|
|
# if search
|
|
|
|
# if commentstring empty
|
2013-01-12 23:04:52 +01:00
|
|
|
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-13 00:21:20 +01:00
|
|
|
Tools.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
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Writing merged files into tempfile %s." % tempfilename, 3)
|
2013-01-12 17:17:12 +01:00
|
|
|
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-13 00:21:20 +01:00
|
|
|
def processAllFiles(destfiles, dirConfig):
|
2013-01-12 23:04:52 +01:00
|
|
|
"""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-14 23:23:48 +01:00
|
|
|
debug.debug("Processing source files for %s." % df, 2)
|
2013-01-12 23:04:52 +01:00
|
|
|
# assemble file to tmp
|
2013-01-13 01:04:40 +01:00
|
|
|
commentstring = ""
|
|
|
|
if dirConfig.check("Main", "commentstring"):
|
|
|
|
commentstring = dirConfig.get("Main", "commentstring")
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Found commentstring %s in %s" % (commentstring, df), 3)
|
2013-01-13 01:04:40 +01:00
|
|
|
# if COMMENTSTRNIG
|
|
|
|
|
|
|
|
tempfilename = buildFile(destfiles[df], df, commentstring)
|
2013-01-12 23:04:52 +01:00
|
|
|
if not tempfilename:
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Error while creating temp file for %s, skipping." % df, 1)
|
2013-01-12 23:04:52 +01:00
|
|
|
continue
|
|
|
|
# if not tempfilename
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2)
|
2013-01-13 00:21:20 +01:00
|
|
|
|
2013-01-12 23:04:52 +01:00
|
|
|
# diff assembled file and config file
|
2013-01-13 00:21:20 +01:00
|
|
|
if Tools.diff(df, tempfilename, commentstring, debug):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("File %s has changed" % df, 0)
|
2013-01-13 00:21:20 +01:00
|
|
|
if not Tools.userConfigGenerated(df, cfg):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("%s not generated by userconfig, backing up." % df, 2)
|
2013-01-12 23:04:52 +01:00
|
|
|
# file not generated from userconfig -> back up
|
2013-01-13 00:21:20 +01:00
|
|
|
Tools.backupFile(df, debug)
|
2013-01-12 23:04:52 +01:00
|
|
|
# if not userConfigGenerated
|
|
|
|
# copy tmp file to real location
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Copy %s to %s." % (tempfilename, df), 0)
|
2013-01-13 00:21:20 +01:00
|
|
|
Tools.copyFile(tempfilename, df, debug)
|
2013-01-12 23:04:52 +01:00
|
|
|
# if diff
|
2013-01-13 00:21:20 +01:00
|
|
|
# remove tmp
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Removing temporary file %s." % tempfilename, 2)
|
2013-01-13 00:21:20 +01:00
|
|
|
os.remove(tempfilename)
|
2013-01-12 17:17:12 +01:00
|
|
|
# for df
|
|
|
|
# def buildAllFiles
|
|
|
|
|
2013-01-11 00:35:09 +01:00
|
|
|
def main():
|
2013-01-14 16:06:52 +01:00
|
|
|
configfile = os.environ['HOME']+"/etc/userconfig.cfg"
|
2013-01-12 23:04:52 +01:00
|
|
|
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":
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.addverbose()
|
2013-01-12 23:04:52 +01:00
|
|
|
if option in ("-h", "--help"):
|
|
|
|
raise Usage(help_message)
|
2013-01-13 00:21:20 +01:00
|
|
|
if option in ("-d", "--debug.debug"):
|
2013-01-12 23:04:52 +01:00
|
|
|
pass
|
|
|
|
if option in ("-c", "--config"):
|
|
|
|
configfile = value
|
|
|
|
# if
|
|
|
|
# for option, value
|
|
|
|
except Usage, err:
|
2013-01-13 00:21:20 +01:00
|
|
|
Tools.error(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
|
|
|
|
Tools.error("\t for help use --help")
|
2013-01-12 23:04:52 +01:00
|
|
|
return 2
|
|
|
|
# try
|
|
|
|
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Using configfile %s." % configfile, 1)
|
2013-01-14 16:06:52 +01:00
|
|
|
if not os.path.isfile(configfile):
|
2013-01-13 00:21:20 +01:00
|
|
|
Tools.error( "No config file specified.")
|
2013-01-12 23:04:52 +01:00
|
|
|
return 2
|
|
|
|
# if configfile
|
2013-01-14 23:23:48 +01:00
|
|
|
cfg.setdebug(debug)
|
2013-01-12 23:04:52 +01:00
|
|
|
cfg.setfilename(configfile)
|
2013-01-14 23:23:48 +01:00
|
|
|
|
|
|
|
debug.debug("Current host is in classes %s" % hostclasses, 1)
|
2013-01-13 22:32:33 +01:00
|
|
|
configdir = cfg.get("Main", "configdir")
|
2013-01-11 00:35:09 +01:00
|
|
|
for d in os.listdir(configdir):
|
2013-01-13 01:04:40 +01:00
|
|
|
destfiles = {}
|
2013-01-14 23:23:48 +01:00
|
|
|
name = configdir+"/"+d
|
|
|
|
debug.debug("Working in %s" % name, 1)
|
2013-01-11 00:35:09 +01:00
|
|
|
if not os.path.isdir(name):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("%s is not a directory, skipping." % name, 3)
|
2013-01-11 00:35:09 +01:00
|
|
|
continue
|
|
|
|
elif d.startswith(".svn"):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("%s is .svn, skipping." % name, 3)
|
2013-01-11 00:35:09 +01:00
|
|
|
continue
|
|
|
|
elif os.path.isfile(name+"/.ignore"):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("%s contains file .ignore, skipping." % name, 3)
|
2013-01-11 00:35:09 +01:00
|
|
|
continue
|
|
|
|
else:
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Processing files in %s" % name, 2)
|
2013-01-13 00:21:20 +01:00
|
|
|
(destfiles, dirConfig) = workdir(name)
|
2013-01-13 01:04:40 +01:00
|
|
|
if isinstance(destfiles, dict):
|
|
|
|
if len(destfiles.keys()) > 0:
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
|
2013-01-13 01:04:40 +01:00
|
|
|
processAllFiles(destfiles, dirConfig)
|
|
|
|
# if > 0
|
|
|
|
else:
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("No files found for %s, skipping." % name, 1)
|
2013-01-11 00:35:09 +01:00
|
|
|
# if
|
|
|
|
# for d
|
|
|
|
# def main
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|