first revision
This commit is contained in:
@@ -2,9 +2,9 @@ import os
|
||||
import argparse
|
||||
from userconfig.cfgfile import Conf
|
||||
from userconfig.tools import Debug
|
||||
from userconfig.checks import classes_for_host
|
||||
from userconfig import Userconfig
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(prog='userconfig',
|
||||
@@ -18,39 +18,74 @@ def main():
|
||||
dest='file', action='store')
|
||||
cmdline = parser.parse_args()
|
||||
debug = Debug()
|
||||
debug.set_verbose(cmdline.verbose)
|
||||
cfg = Conf(filename=cmdline.file, debug=debug)
|
||||
# debug.set_verbose(cmdline.verbose)
|
||||
# cfg = Conf(filename=cmdline.file, debug=debug)
|
||||
debug.set_verbose(4)
|
||||
cfg = Conf(filename='/Users/lysis/userconfig2-test.conf', debug=debug)
|
||||
# cfg = Conf(filename='/Users/lysis/etc/userconfig2.conf', debug=debug)
|
||||
uc = Userconfig(cfg)
|
||||
|
||||
cfg.debug.stdout(f"Verbose level is {cfg.debug.get_verbose()}", 1)
|
||||
cfg.debug.stdout("================ main ===============", 1)
|
||||
temp_classes = ""
|
||||
for h in classes_for_host():
|
||||
temp_classes = temp_classes + str(h) + ","
|
||||
cfg.debug.stdout("+++ Current host is in classes %s" % temp_classes, 1)
|
||||
cfg.debug.stdout(f"Verbose level is {cfg.debug.get_verbose()}", 1, 'STANDARD')
|
||||
configdir = cfg.get("configdir")
|
||||
for d in os.listdir(configdir):
|
||||
name = configdir+"/"+d
|
||||
cfg.debug.stdout("+++ Working in %s" % name, 1)
|
||||
if not os.path.isdir(name):
|
||||
cfg.debug.stdout("--- %s is not a directory, skipping." % name, 3)
|
||||
continue
|
||||
elif d.startswith(".svn") or d.startswith(".git"):
|
||||
cfg.debug.stdout("--- %s is .svn or .git, skipping." % name, 3)
|
||||
continue
|
||||
elif os.path.isfile(name+"/.ignore"):
|
||||
cfg.debug.stdout("--- %s contains file .ignore, skipping." % name, 3)
|
||||
continue
|
||||
else:
|
||||
cfg.debug.stdout("+++ Processing files in %s" % name, 2)
|
||||
(destfiles, dirConfig) = uc.workdir(name)
|
||||
if isinstance(destfiles, dict):
|
||||
if len(destfiles.keys()) > 0:
|
||||
cfg.debug.stdout("+++ Building %d files: %s" % (len(destfiles.keys()), destfiles.keys()), 3)
|
||||
uc.process_all_files(destfiles, dirConfig)
|
||||
else:
|
||||
cfg.debug.stdout("--- No files found for %s, skipping." % name, 1)
|
||||
# configdir is the root of the userconfig files
|
||||
# Directory structure:
|
||||
# configdir/
|
||||
# |-- userconfig2.conf
|
||||
# |-- package1/
|
||||
# |- package.conf
|
||||
# |- 001_Arch_Linux/
|
||||
# |- file1
|
||||
# |- file2
|
||||
# |- 002_Host_glitters/
|
||||
# |- file1
|
||||
# |- 003_all/
|
||||
# |- file2
|
||||
#
|
||||
# Terminology:
|
||||
# directories below configdir are packages, packages contain directories categorizing for which host/arch they
|
||||
# are fitted, below that are files which are installed at the destination
|
||||
#
|
||||
# directory names in packages:
|
||||
# [Number]_[category]_[value]
|
||||
# Number: can have leading zeros for better sorting in directory structure, will be used for sorting and priority
|
||||
# category: which kind of match we are looking for. currently: Arch for `uname -s`, Host for `hostname -s`
|
||||
# value: optional, if category requires input, for example: Arch_Linux
|
||||
# can be empty for example if category is all (no match needed, we want this always to be applied)
|
||||
|
||||
cfg.debug.stdout(f'configdir: {configdir}', 1)
|
||||
for package in os.scandir(configdir):
|
||||
if not package.is_dir():
|
||||
cfg.debug.stdout(f'{package.path} is not a directory, skipping', 2, 'WARNING')
|
||||
continue
|
||||
if package.name in ['.svn', '.git']:
|
||||
cfg.debug.stdout(f'{package.path} is a svn or git data directory, skipping', 2, 'WARNING')
|
||||
if os.path.isfile(f'{package.path}/.ignore'):
|
||||
cfg.debug.stdout(f'{package.path} contains .ignore, skipping', 2, 'WARNING')
|
||||
continue
|
||||
cfg.debug.stdout(f'============ start {package.path} ============', 2)
|
||||
(category_dirs, dir_config) = uc.process_package_dir(package.path)
|
||||
cfg.debug.stdout(f'Got categories: {category_dirs}', 2)
|
||||
host_category_dirs = uc.filter_categories(category_dirs)
|
||||
cfg.debug.stdout(f'Filtered categories for host: {host_category_dirs}', 2)
|
||||
file_list = dict()
|
||||
for c in host_category_dirs:
|
||||
file_list = uc.process_category_dir(c, file_list)
|
||||
cfg.debug.stdout(f'Found files: {file_list}', 2)
|
||||
if len(file_list) and os.access(f'{package.path}/install.sh', os.X_OK):
|
||||
cfg.debug.stdout(f'Execute {package.path}/install.sh', 2)
|
||||
subprocess.call([f'{package.path}/install.sh'])
|
||||
for file in file_list:
|
||||
dest = f'{dir_config.get(section="Main", option="dest")}/{file}'
|
||||
try:
|
||||
comment_string = dir_config.get(section="Main", option="commentstring")
|
||||
except ValueError:
|
||||
cfg.debug.stdout(f'commentstring does not exist in config file {dir_config._cfgfiles}', 0, 'ERROR')
|
||||
sys.exit(1)
|
||||
# Make sure all directories for destination file exist
|
||||
uc.create_destination_directories(dir_config.get(section="Main", option="dest"))
|
||||
temp_filename = uc.build_file(file_list[file], dest, comment_string)
|
||||
uc.copy_file(temp_filename, dest, comment_string)
|
||||
cfg.debug.stdout(f'============ end {package.path} ============\n\n', 2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user