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-11 00:35:09 +01:00
|
|
|
from checks import checks
|
|
|
|
|
|
|
|
classchecks = checks()
|
|
|
|
configdir = "../"
|
|
|
|
hostclasses = classchecks.__classesForHost__()
|
2013-01-12 17:17:12 +01:00
|
|
|
verbose = 1
|
|
|
|
|
|
|
|
def debug(out, level=0):
|
|
|
|
"""docstring for debug"""
|
|
|
|
if verbose:
|
|
|
|
if level > 0:
|
|
|
|
print "*"*level,out
|
|
|
|
else:
|
|
|
|
print out
|
|
|
|
# if verbose
|
|
|
|
# def debug
|
|
|
|
|
|
|
|
def getDEST(directory):
|
|
|
|
"""docstring for getDEST"""
|
|
|
|
f = open(directory+"/DEST")
|
|
|
|
destdir= f.read()[:-1]
|
|
|
|
if destdir == "$HOME":
|
|
|
|
destdir = os.environ['HOME']
|
|
|
|
# if $HOME
|
|
|
|
if not destdir.endswith("/"):
|
|
|
|
destdir += "/"
|
|
|
|
# if not /
|
|
|
|
return destdir
|
|
|
|
# def getDEST
|
2013-01-11 00:35:09 +01:00
|
|
|
|
2013-01-12 17:17:12 +01:00
|
|
|
def workconf(directory, destdir, depth=2):
|
|
|
|
"""docstring for workconf"""
|
|
|
|
dirs = os.listdir(directory)
|
|
|
|
ret = []
|
|
|
|
for d in dirs:
|
|
|
|
name = directory+"/"+d
|
|
|
|
if os.path.isdir(name):
|
|
|
|
# fixme: create name
|
|
|
|
workconf(name, destdir, depth+1)
|
|
|
|
# if dir
|
|
|
|
ret.append(name)
|
|
|
|
debug("workconf: found file %s" % name, depth)
|
|
|
|
# for d
|
|
|
|
return ret
|
|
|
|
# def workconf
|
|
|
|
|
|
|
|
def workdir(directory):
|
2013-01-11 00:35:09 +01:00
|
|
|
"""docstring for work"""
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("===============================", 1)
|
|
|
|
|
2013-01-11 00:35:09 +01:00
|
|
|
if not os.path.isfile(directory+"/DEST"):
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("No DEST in %s, skipping." % directory, 1)
|
2013-01-11 00:35:09 +01:00
|
|
|
return
|
|
|
|
# if not DEST
|
2013-01-12 17:17:12 +01:00
|
|
|
destdir = getDEST(directory)
|
|
|
|
# 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 = {}
|
|
|
|
# Schritt 1: Dateien einlesen
|
2013-01-11 00:35:09 +01:00
|
|
|
for h in hostclasses:
|
|
|
|
if h[0] != "":
|
|
|
|
classdir = directory+"/"+h[0]+"_"+h[1]
|
|
|
|
else:
|
|
|
|
classdir = directory+"/"+h[1]
|
|
|
|
# if all
|
|
|
|
if os.path.isdir(classdir):
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("workdir: %s" % classdir, 1)
|
|
|
|
tempfiles = workconf(classdir, destdir)
|
|
|
|
# tempfiles is a list of files within one classdir
|
|
|
|
|
|
|
|
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 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 17:17:12 +01:00
|
|
|
def buildFile(destfile, classfiles):
|
|
|
|
"""docstring for buildFile"""
|
|
|
|
content = []
|
|
|
|
for f in classfiles:
|
|
|
|
fp = open(f, "r")
|
|
|
|
content.append(fp.read())
|
|
|
|
fp.close()
|
|
|
|
# end f
|
|
|
|
|
|
|
|
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
|
|
|
|
|
|
|
|
fp = None
|
|
|
|
try:
|
|
|
|
fp = os.fdopen(tempfd, "w")
|
|
|
|
except:
|
|
|
|
print "Cannot write to temporary file",tempfilename
|
|
|
|
os.remove(tempfilename)
|
|
|
|
sys.exit(1)
|
|
|
|
# try
|
|
|
|
|
|
|
|
for block in content:
|
|
|
|
fp.write(block)
|
|
|
|
fp.write("\n")
|
|
|
|
# for content
|
|
|
|
fp.close()
|
|
|
|
# fixme: diff tempfilename destfile
|
|
|
|
# fixme: header marker for "is under userconfig control"
|
|
|
|
# fixme: if diff and not under userconfig control, backup destfile
|
|
|
|
# fixme: copy tempfilename to destfile
|
|
|
|
# fixme: remove tempfilename
|
|
|
|
# def buildFile
|
|
|
|
|
|
|
|
def buildAllFiles(destfiles):
|
|
|
|
"""docstring for buildFiles"""
|
|
|
|
for df in destfiles.keys():
|
|
|
|
buildFile(df, destfiles[df])
|
|
|
|
# for df
|
|
|
|
# def buildAllFiles
|
|
|
|
|
2013-01-11 00:35:09 +01:00
|
|
|
def main():
|
2013-01-12 17:17:12 +01:00
|
|
|
debug("Classes for host: %s" % hostclasses)
|
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)
|
|
|
|
buildAllFiles(destfiles)
|
2013-01-11 00:35:09 +01:00
|
|
|
# if
|
|
|
|
# for d
|
|
|
|
# def main
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|