rewrite to python3

This commit is contained in:
Marcus Stoegbauer 2019-06-12 23:32:05 +02:00
parent 32aa6ff7d2
commit 2114d9a77d
10 changed files with 477 additions and 524 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/__pycache__/
*.pyc
build/
.idea/workspace.xml

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (pyuserconfig)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pyuserconfig.iml" filepath="$PROJECT_DIR$/.idea/pyuserconfig.iml" />
</modules>
</component>
</project>

11
.idea/pyuserconfig.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.7 (pyuserconfig)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,22 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# $Id$
# $URL$
""" """
Tools.py Tools.py
Created by Marcus Stoegbauer on 2013-01-12. Created by Marcus Stoegbauer on 2013-01-12.
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
import sys import sys
import os import os
import cfgfile import Userconfig.cfgfile as cfgfile
import re import re
import time import time
import shutil import shutil
import itertools
class Debug(object): class Debug(object):
@ -24,32 +20,27 @@ class Debug(object):
def __init__(self, verbose=0): def __init__(self, verbose=0):
self.setverbose(verbose) self.setverbose(verbose)
# def __init__
def setverbose(self, verbose): def setverbose(self, verbose):
"""docstring for setverbose""" """docstring for setverbose"""
self.verbose = verbose self.verbose = verbose
# def setverbose
def addverbose(self): def addverbose(self):
"""docstring for setverbose""" """docstring for setverbose"""
self.verbose += 1 self.verbose += 1
# def addverbose
def debug(self, out, level=0): def debug(self, out, level=0):
"""docstring for debug""" """docstring for debug"""
if self.verbose >= level: if self.verbose >= level:
print out print(out)
# if level
# def debug
# class Debug
def error(out): def error(out):
"""Print error on stderr""" """Print error on stderr"""
print >> sys.stderr, str(out)+"\n" print(str(out)+"\n", file=sys.stderr)
# def error
def getConfig(filename):
def get_config(filename):
"""reads filename as config, checks for DEST parameter and returns cfgfile object""" """reads filename as config, checks for DEST parameter and returns cfgfile object"""
ret = None ret = None
try: try:
@ -57,26 +48,22 @@ def getConfig(filename):
except: except:
error("Error reading config file %s" % filename) error("Error reading config file %s" % filename)
return False return False
# try
# check for DEST parameter # check for DEST parameter
if not ret.check("Main", "dest"): if not ret.check("Main", "dest"):
error("No DEST in config file %s" % filename) error("No DEST in config file %s" % filename)
return False return False
# if no DEST
# replace $HOME with real home directory # replace $HOME with real home directory
if ret.get("Main", "dest") == "$HOME": if ret.get("Main", "dest") == "$HOME":
ret.set("Main", "dest", os.environ['HOME']) ret.set("Main", "dest", os.environ['HOME'])
# if $HOME
# make sure DEST ends with / # make sure DEST ends with /
if not ret.get("Main", "dest").endswith("/"): if not ret.get("Main", "dest").endswith("/"):
ret.set("Main", "dest", ret.get("Main", "dest")+"/") ret.set("Main", "dest", ret.get("Main", "dest")+"/")
# if not /
return ret return ret
# def getConfig
def read_skip_comment(fp, commentstring): def read_skip_comment(fp, commentstring):
"""Read line from filehandle fp and skip all empty (whitespace) lines and lines starting with commentstring """Read line from filehandle fp and skip all empty (whitespace) lines and lines starting with commentstring
@ -85,9 +72,7 @@ def read_skip_comment(fp, commentstring):
line = line[:-1] line = line[:-1]
if (commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line !="" and not re.match("^\s+$", line): if (commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line !="" and not re.match("^\s+$", line):
yield line yield line
# if not match
# for line
# def read_skip_comment
def diff(destfile, tempfile, commentstring, debug): def diff(destfile, tempfile, commentstring, debug):
"""diff destfile and tempfile, returns True if files differ, False if they are the same""" """diff destfile and tempfile, returns True if files differ, False if they are the same"""
@ -106,7 +91,7 @@ def diff(destfile, tempfile, commentstring, debug):
fp1 = open(tempfile) fp1 = open(tempfile)
fp2 = open(destfile) fp2 = open(destfile)
for line1, line2 in itertools.izip(read_skip_comment(fp1, commentstring), read_skip_comment(fp2, commentstring)): for line1, line2 in zip(read_skip_comment(fp1, commentstring), read_skip_comment(fp2, commentstring)):
if line1 != line2: if line1 != line2:
fp1.close() fp1.close()
fp2.close() fp2.close()
@ -116,32 +101,28 @@ def diff(destfile, tempfile, commentstring, debug):
fp1.close() fp1.close()
fp2.close() fp2.close()
return False return False
# def diff
def userConfigGenerated(filename, cfg):
def user_config_generated(filename, cfg):
"""returns True if filename has been generated by userconfig, False else""" """returns True if filename has been generated by userconfig, False else"""
if not os.path.isfile(filename): if not os.path.isfile(filename):
# filename does not exist, so it was not generated by userconfig # filename does not exist, so it was not generated by userconfig
return False return False
# if not filename
if not cfg.check("Main","stamp"): if not cfg.check("Main","stamp"):
# no STAMP in userconfig.cfg, so no way to check if file was generated by userconfig # no STAMP in userconfig.cfg, so no way to check if file was generated by userconfig
return False return False
# no STAMP
fp = open(filename, "r") fp = open(filename, "r")
for line in fp: for line in fp:
if re.search(re.escape(cfg.get("Main","stamp")), line): if re.search(re.escape(cfg.get("Main","stamp")), line):
return True return True
# if search
# for line
return False return False
# def userConfigGenerated
def backupFile(filename, debug):
def backup_file(filename, debug):
"""make backup of filename, returns True if backup is successful, False else""" """make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename): if os.path.isfile(filename):
debug.debug("%s exists, finding backup name." % filename, 3) debug.debug("%s exists, finding backup name." % filename, 3)
@ -151,17 +132,15 @@ def backupFile(filename, debug):
while os.path.isfile(testbackupname): while os.path.isfile(testbackupname):
counter+=1 counter+=1
testbackupname=backupname+"."+str(counter) testbackupname=backupname+"."+str(counter)
# while
debug.debug("Renaming %s to %s" % (filename, testbackupname), 1) debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
os.rename(filename, testbackupname) os.rename(filename, testbackupname)
return True return True
else: else:
debug.debug("%s does not exist, do not need backup." % filename, 3) debug.debug("%s does not exist, do not need backup." % filename, 3)
# if filename
return False return False
# def backupFile
def copyFile(sourcefile, destfile, debug):
def copy_file(sourcefile, destfile, debug):
"""copy sourcefile to destfile, returns True if successful, False else""" """copy sourcefile to destfile, returns True if successful, False else"""
if os.path.isfile(sourcefile): if os.path.isfile(sourcefile):
@ -174,6 +153,4 @@ def copyFile(sourcefile, destfile, debug):
# destfile is writable # destfile is writable
else: else:
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3) debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
# if write ok
return False return False
# def copyFile

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# #
# $Id$
# $URL$
""" """
cfgfile.py cfgfile.py
@ -10,28 +8,26 @@ cfgfile.py
Created by Marcus Stoegbauer on 2013-01-12. Created by Marcus Stoegbauer on 2013-01-12.
Copyright (c) 2013 __MyCompanyName__. All rights reserved. Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
import ConfigParser import configparser
import os import os
import re import re
class Conf(object): class Conf(object):
confobj = ConfigParser.RawConfigParser() confobj = configparser.RawConfigParser()
cfgfile = '' cfgfile = ''
debug = None debug = None
def __init__(self, filename=None): def __init__(self, filename=None):
"""if filename is set, open config file and initialize the ConfigParser """if filename is set, open config file and initialize the ConfigParser
""" """
self.confobj = ConfigParser.RawConfigParser() self.confobj = configparser.RawConfigParser()
if filename: if filename:
self.setfilename(filename) self.setfilename(filename)
# if filename
# def __init__
def setdebug(self, debug): def setdebug(self, debug):
"""docstring for setdebug""" """docstring for setdebug"""
self.debug = debug self.debug = debug
# def setdebug
def setfilename(self, filename): def setfilename(self, filename):
"""initialize the ConfigParser """initialize the ConfigParser
@ -39,58 +35,42 @@ class Conf(object):
ret = self.confobj.read(filename) ret = self.confobj.read(filename)
if len(ret) == 0 or ret[0] != filename: if len(ret) == 0 or ret[0] != filename:
raise Exception('Cannot read config file ' + filename) raise Exception('Cannot read config file ' + filename)
# if cannot read
self.cfgfile = filename self.cfgfile = filename
if self.debug: if self.debug:
self.debug.debug("Read config file %s" % filename, 2) self.debug.debug("Read config file %s" % filename, 2)
self.debug.debug("Replacing environment variables in %s." % filename, 3) self.debug.debug("Replacing environment variables in %s." % filename, 3)
# if debug
for s in self.confobj.sections(): for s in self.confobj.sections():
for (i, val) in self.confobj.items(s): for (i, val) in self.confobj.items(s):
tempre = re.search("\$([A-Z]+)[^A-Z]*", val) tempre = re.search(r"\$([A-Z]+)[^A-Z]*", val)
if tempre: if tempre:
varname = tempre.group(1) varname = tempre.group(1)
if self.debug: if self.debug:
self.debug.debug("Found variable %s in %s." % (varname, i), 3) self.debug.debug("Found variable %s in %s." % (varname, i), 3)
# if debug if varname in os.environ:
if os.environ.has_key(varname):
if self.debug: if self.debug:
self.debug.debug("%s exists in environment, replacing with %s." % (varname, os.environ[varname]), 3) self.debug.debug("%s exists in environment, replacing with %s." %
# if debug (varname, os.environ[varname]), 3)
self.set(s, i, val.replace("$"+varname, os.environ[varname])) self.set(s, i, val.replace("$"+varname, os.environ[varname]))
# if has_key
# if tempre
# for i
# for s
# def setfilename
def get(self, section, option): def get(self, section, option):
"""returns the value of option in section """returns the value of option in section
""" """
if not self.cfgfile: if not self.cfgfile:
raise Exception('No config file set') raise Exception('No config file set')
# if not cfgfile
return self.confobj.get(section, option) return self.confobj.get(section, option)
# def get
def set(self, section, option, value): def set(self, section, option, value):
"""docstring for update""" """docstring for update"""
self.confobj.set(section, option, value) self.confobj.set(section, option, value)
# def set
def getitems(self, section): def getitems(self, section):
"""returns all items in section """returns all items in section
""" """
if not self.cfgfile: if not self.cfgfile:
raise Exception('No config file set') raise Exception('No config file set')
# if not cfgfile
return self.confobj.items(section) return self.confobj.items(section)
# def getitems
def check(self, section, option): def check(self, section, option):
"""checks for option in section""" """checks for option in section"""
return self.confobj.has_option(section, option) return self.confobj.has_option(section, option)
# def check
# class Conf

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# #
# $Id$ # $Id$
@ -11,41 +11,36 @@ Created by Marcus Stoegbauer on 2013-01-10.
Copyright (c) 2013 __MyCompanyName__. All rights reserved. Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
import sys
import os
import platform import platform
class checks(object):
class Checks(object):
def __init__(self): def __init__(self):
pass pass
# def __init__
def getShortHostname(self): def get_short_hostname(self):
"""docstring for getShortHostname""" """docstring for getShortHostname"""
hostname = platform.node() hostname = platform.node()
if hostname.count("."): if hostname.count("."):
hostname = hostname.split(".")[0] hostname = hostname.split(".")[0]
return hostname return hostname
# def getShortHostname # def getShortHostname
def __classesForHost__(self): def __classes_for_host__(self):
"""docstring for __classesForHost""" """docstring for __classesForHost"""
classes = [] classes = []
for c in dir(self): for c in dir(self):
if c.startswith("__"): if c.startswith("__"):
continue continue
# if __
ret = getattr(self, c)() ret = getattr(self, c)()
if len(ret) == 3: if len(ret) == 3:
classes.append(ret) classes.append(ret)
# for c
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 header(self): def header(self):
"""docstring for header""" """docstring for header"""
return (0, "", "header") return (0, "", "header")
# def header
def footer(self): def footer(self):
"""docstring for footer""" """docstring for footer"""
@ -54,23 +49,20 @@ class checks(object):
def all(self): def all(self):
"""docstring for all""" """docstring for all"""
return (998, "", "all") return (998, "", "all")
# def all
def arch(self): def arch(self):
"""docstring for arch""" """docstring for arch"""
return (800, "Arch", platform.system()) return (800, "Arch", platform.system())
# def arch
def hostname(self): def hostname(self):
"""docstring for hostname""" """docstring for hostname"""
hostname = self.getShortHostname() hostname = self.get_short_hostname()
return (10, "Host", hostname) return (10, "Host", hostname)
# def hostname
def app(self): def app(self):
"""docstring for app""" """docstring for app"""
hostname = self.getShortHostname() hostname = self.get_short_hostname()
if (hostname == "glitters"): if hostname == "glitters":
return (500, "", "rancid_hosts") return (500, "", "rancid_hosts")
else: else:
return () return ()

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python3
from distutils.core import setup from distutils.core import setup
import sys
import os
setup(name="userconfig", setup(name="userconfig",
version="0.1", version="0.1",

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# #
# $Id$ # $Id$
@ -8,7 +8,6 @@
userconfig.py userconfig.py
Created by Marcus Stoegbauer on 2013-01-10. Created by Marcus Stoegbauer on 2013-01-10.
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
import sys import sys
@ -20,26 +19,27 @@ import time
# #
import Userconfig.cfgfile as cfgfile import Userconfig.cfgfile as cfgfile
from Userconfig.checks import checks from Userconfig.checks import Checks
import Userconfig.Tools as Tools import Userconfig.Tools as Tools
debug = Tools.Debug() debug = Tools.Debug()
classchecks = checks() classchecks = Checks()
cfg = cfgfile.Conf() cfg = cfgfile.Conf()
hostclasses = classchecks.__classesForHost__() hostclasses = classchecks.__classes_for_host__()
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
# def __init__
help_message = """ help_message = """
-h help -h help
-v verbose level (multiple v for higher level) -v verbose level (multiple v for higher level)
-c userconfig.cfg config file -c userconfig.cfg config file
""" """
# class Usage
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def workconf(directory, depth=2): def workconf(directory, depth=2):
"""walks through directory, collecting all filenames, returns list of all filenames""" """walks through directory, collecting all filenames, returns list of all filenames"""
@ -51,17 +51,14 @@ def workconf(directory, depth=2):
if os.path.isdir(name): if os.path.isdir(name):
# fixme: create name if it does not exist # fixme: create name if it does not exist
workconf(name, depth+1) workconf(name, depth+1)
# if dir
if name.endswith(".swp"): if name.endswith(".swp"):
continue continue
if d == ".svn": if d == ".svn":
continue continue
ret.append(name) ret.append(name)
debug.debug("Found file %s in directory %s" % (name, directory), 4) debug.debug("Found file %s in directory %s" % (name, directory), 4)
# if not .swp
# for d
return ret return ret
# def workconf
def workdir(directory): def workdir(directory):
"""walks through all host classes, checking if the classes directory exists in directory """walks through all host classes, checking if the classes directory exists in directory
@ -72,17 +69,15 @@ def workdir(directory):
# skip directory if no CONFIGFILE present # skip directory if no CONFIGFILE present
if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")): if not os.path.isfile(directory+"/"+cfg.get("Main", "configfile")):
debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1) debug.debug("No %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
return ({},None) return {},None
# if not DEST
# get config file for directory # get config file for directory
dirConfig = Tools.getConfig(directory+"/"+cfg.get("Main", "configfile")) dir_config = Tools.get_config(directory + "/" + cfg.get("Main", "configfile"))
if not dirConfig: if not dir_config:
debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1) debug.debug("Cannot read %s in %s, skipping." % (cfg.get("Main", "configfile"), directory), 1)
return ({},None) return {},None
# if not dirConfig
destdir = dirConfig.get("Main","dest") destdir = dir_config.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
@ -95,7 +90,6 @@ def workdir(directory):
classdir = directory+"/"+h[0]+"_"+h[1] classdir = directory+"/"+h[0]+"_"+h[1]
else: else:
classdir = directory+"/"+h[1] classdir = directory+"/"+h[1]
# if all
debug.debug("Looking for directory %s." % classdir, 4) debug.debug("Looking for directory %s." % classdir, 4)
# if class directory exists # if class directory exists
if os.path.isdir(classdir): if os.path.isdir(classdir):
@ -106,27 +100,27 @@ def workdir(directory):
# put files into dict # 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 destname in destfiles:
destfiles[destname] = [] destfiles[destname] = []
# if not destname, create key with empty list
destfiles[destname].append(f) # append each file to dict destfiles[destname].append(f) # append each file to dict
debug.debug("Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4) debug.debug("Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4)
# for tempfiles
# if classdir
# for hostclasses
debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3) debug.debug("workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
return (destfiles, dirConfig) return destfiles, dir_config
# def work
def buildFile(classfiles, destfile, commentstring):
def build_file(classfiles, destfile, commentstring):
"""open all classfiles, assemble them and write the contents into a tempfile """open all classfiles, assemble them and write the contents into a tempfile
returns the name of tempfile""" returns the name of tempfile
:param classfiles:
:param destfile:
:param commentstring:
:return: str
"""
content = [] content = []
if commentstring != "": if commentstring != "":
debug.debug("commentstring found, adding header.", 3) debug.debug("commentstring found, adding header.", 3)
content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n") content.append(commentstring + " " + cfg.get("Main","stamp") + " " + time.strftime("%+") + "\n")
# if commentstring not empty
for f in classfiles: for f in classfiles:
debug.debug("Merging %s." % f, 4) debug.debug("Merging %s." % f, 4)
@ -138,77 +132,64 @@ def buildFile(classfiles, destfile, commentstring):
if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent): if re.search(re.escape(cfg.get("Main","stampreplace")), filecontent):
debug.debug("commentstring empty, replacing stamp in file", 3) debug.debug("commentstring empty, replacing stamp in file", 3)
filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent) filecontent = re.sub(re.escape(cfg.get("Main","stampreplace")), cfg.get("Main","stamp"), filecontent)
# if search
# if commentstring empty
content.append(filecontent) content.append(filecontent)
# end f
(tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp") (tempfd, tempfilename) = tempfile.mkstemp(prefix=os.path.basename(destfile), dir="/tmp")
fp = None
try: try:
fp = os.fdopen(tempfd, "w") fp = os.fdopen(tempfd, "w")
except: except:
Tools.error("Cannot write to temporary file %s" % tempfilename) Tools.error("Cannot write to temporary file %s" % tempfilename)
os.remove(tempfilename) os.remove(tempfilename)
return False return False
# try
debug.debug("Writing merged files into tempfile %s." % tempfilename, 3) debug.debug("Writing merged files into tempfile %s." % tempfilename, 3)
for block in content: for block in content:
fp.write(block) fp.write(block)
fp.write("\n") fp.write("\n")
# for content
fp.close() fp.close()
return tempfilename return tempfilename
# def buildFile
def processAllFiles(destfiles, dirConfig):
def process_all_files(destfiles, dir_config):
"""processes all files in destfiles, generate files from classes, compare and copy if necessary""" """processes all files in destfiles, generate files from classes, compare and copy if necessary"""
for df in destfiles.keys(): for df in destfiles.keys():
debug.debug("Processing source files for %s." % df, 2) debug.debug("Processing source files for %s." % df, 2)
# assemble file to tmp # assemble file to tmp
commentstring = "" commentstring = ""
if dirConfig.check("Main", "commentstring"): if dir_config.check("Main", "commentstring"):
commentstring = dirConfig.get("Main", "commentstring") commentstring = dir_config.get("Main", "commentstring")
debug.debug("Found commentstring %s in %s" % (commentstring, df), 3) debug.debug("Found commentstring %s in %s" % (commentstring, df), 3)
# if COMMENTSTRNIG
tempfilename = buildFile(destfiles[df], df, commentstring) tempfilename = build_file(destfiles[df], df, commentstring)
if not tempfilename: if not tempfilename:
debug.debug("Error while creating temp file for %s, skipping." % df, 1) debug.debug("Error while creating temp file for %s, skipping." % df, 1)
continue continue
# if not tempfilename
debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2) debug.debug("Merged files %s for %s into %s" % (str(destfiles[df]), df, tempfilename), 2)
# diff assembled file and config file # diff assembled file and config file
if Tools.diff(df, tempfilename, commentstring, debug): if Tools.diff(df, tempfilename, commentstring, debug):
debug.debug("File %s has changed" % df, 0) debug.debug("File %s has changed" % df, 0)
if not Tools.userConfigGenerated(df, cfg): if not Tools.user_config_generated(df, cfg):
debug.debug("%s not generated by userconfig, backing up." % df, 2) debug.debug("%s not generated by userconfig, backing up." % df, 2)
# file not generated from userconfig -> back up # file not generated from userconfig -> back up
Tools.backupFile(df, debug) Tools.backup_file(df, debug)
# if not userConfigGenerated
# copy tmp file to real location # copy tmp file to real location
debug.debug("Copy %s to %s." % (tempfilename, df), 0) debug.debug("Copy %s to %s." % (tempfilename, df), 0)
Tools.copyFile(tempfilename, df, debug) Tools.copy_file(tempfilename, df, debug)
# if diff
# remove tmp # remove tmp
debug.debug("Removing temporary file %s." % tempfilename, 2) debug.debug("Removing temporary file %s." % tempfilename, 2)
os.remove(tempfilename) os.remove(tempfilename)
# for df
# def buildAllFiles
def main(): def main():
configfile = os.environ['HOME']+"/etc/userconfig.cfg" configfile = os.environ['HOME']+"/etc/userconfig.cfg"
global verbose
try: try:
try: try:
opts, args = getopt.getopt(sys.argv[1:], "hdc:v", ["help", "debug", "config="]) opts, args = getopt.getopt(sys.argv[1:], "hdc:v", ["help", "debug", "config="])
except getopt.error, msg: except getopt.GetoptError as msg:
raise Usage(msg) raise Usage(msg)
# try getopt
for option, value in opts: for option, value in opts:
if option == "-v": if option == "-v":
@ -219,19 +200,16 @@ def main():
pass pass
if option in ("-c", "--config"): if option in ("-c", "--config"):
configfile = value configfile = value
# if except Usage as err:
# for option, value
except Usage, err:
Tools.error(sys.argv[0].split("/")[-1] + ": " + str(err.msg)) Tools.error(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
Tools.error("\t for help use --help") Tools.error("\t for help use --help")
return 2 return 2
# try
debug.debug("Using configfile %s." % configfile, 1) debug.debug("Using configfile %s." % configfile, 1)
if not os.path.isfile(configfile): if not os.path.isfile(configfile):
Tools.error("No config file specified.") Tools.error("No config file specified.")
return 2 return 2
# if configfile
cfg.setdebug(debug) cfg.setdebug(debug)
cfg.setfilename(configfile) cfg.setfilename(configfile)
@ -256,15 +234,10 @@ def main():
if isinstance(destfiles, dict): if isinstance(destfiles, dict):
if len(destfiles.keys()) > 0: if len(destfiles.keys()) > 0:
debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3) debug.debug("Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
processAllFiles(destfiles, dirConfig) process_all_files(destfiles, dirConfig)
# if > 0
else: else:
debug.debug("No files found for %s, skipping." % name, 1) debug.debug("No files found for %s, skipping." % name, 1)
# if
# for d
# def main
if __name__ == '__main__': if __name__ == '__main__':
main() main()