pyuserconfig/userconfig/__init__.py

68 lines
3.2 KiB
Python

from userconfig.tools import get_config
import subprocess
from userconfig.checks import classes_for_host
class Userconfig():
_cfg = None
def __init__(self, cfg):
self._cfg = cfg
def workdir(self, directory):
"""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
"""
self._cfg.debug.stdout(" ================ workdir ===============", 1)
self._cfg.debug.stdout(" Working on directory %s" % directory, 3)
# skip directory if no CONFIGFILE present
if not os.path.isfile(directory+"/"+self._cfg.get("configfile")):
self._cfg.debug.stdout(f' --- No file {self._cfg.get("configfile")} in {directory}, skipping.', 1)
return {}, None
# get config file for directory
dir_config = get_config(directory + "/" + cfg.get("configfile"))
if not dir_config:
self._cfg.debug.stdout(f' --- Cannot read config file {self._cfg.get("configfile")} in {directory}, skipping.', 1)
return {}, None
destdir = dir_config.get(section="Main",option="dest")
# 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 = {}
reverse_sort = False
try:
reverse_sort = dir_config.get(section="Main", option="reverse", boolean=True)
except ValueError:
reverse_sort = False
if os.access(directory + "/install.sh", os.X_OK):
subprocess.call([directory + "/install.sh"])
# walk through all know classes in directory and find filenames
for h in classes_for_host(reverse_sort): #### continue here
# build classes directory
if h[0] != "":
classdir = directory+"/"+h[0]+"_"+h[1]
else:
classdir = directory+"/"+h[1]
debug.stdout(" ??? Looking for directory %s." % classdir, 4)
# if class directory exists
if os.path.isdir(classdir):
debug.stdout(" +++ Found directory %s, getting files." % classdir, 4)
# get list of files within this class directory
tempfiles = workconf(classdir)
debug.stdout(" +++ Got %d files: %s." % (len(tempfiles), str(tempfiles)), 4)
# put files into dict
for f in tempfiles:
destname = destdir+os.path.basename(f) # destination filename
if not destname in destfiles:
destfiles[destname] = []
destfiles[destname].append(f) # append each file to dict
debug.stdout(" +++ Added file to %s, now %d files: %s" % (destname, len(destfiles[destname]), destfiles[destname]), 4)
debug.stdout(" === workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
debug.stdout(" ================ workdir ===============", 1)
return destfiles, dir_config