92 lines
4.2 KiB
Python
Executable File
92 lines
4.2 KiB
Python
Executable File
import os
|
|
import argparse
|
|
from userconfig.cfgfile import Conf
|
|
from userconfig.tools import Debug
|
|
from userconfig import Userconfig
|
|
import sys
|
|
import subprocess
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(prog='userconfig',
|
|
description='Manages configuration files, usually in the user home directory')
|
|
|
|
parser.add_argument('-v',
|
|
help='Verbosity level (multiple v for higher level)',
|
|
dest='verbose', action='count', default=0)
|
|
parser.add_argument('-f', '--file',
|
|
help='userconfig2.cfg config file',
|
|
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(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, 'STANDARD')
|
|
configdir = cfg.get("configdir")
|
|
# 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()
|