Compare commits

...

6 Commits

4 changed files with 58 additions and 12 deletions

View File

@@ -1,10 +1,11 @@
#!/bin/sh
if type pip3 >/dev/null 2>&1; then
pip3 install --user git+https://git.lys.is/lysis/pyuserconfig.git
git clone git@git.lys.is:lysis/userconfig.git ~/.userconfig
mkdir ~/.config
PYTHONPATH=~/.local/lib ~/.local/bin/userconfig.py
if type pipx >/dev/null 2>&1; then
pipx install git+https://git.lys.is/lysis/pyuserconfig.git
if [ ! -e ~/.userconfig ]; then
git clone git@git.lys.is:lysis/userconfig.git ~/.userconfig
fi
userconfig
else
echo "No pip3 installed, cannot proceed."
echo "No pipx installed, cannot proceed."
fi

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "userconfig"
version = "2.0"
version = "2.1"
authors = [ {name = "Marcus Stoegbauer", email = "marcus@grmpf.org"} ]
maintainers = [ {name = "Marcus Stoegbauer", email = "marcus@grmpf.org"} ]
description = "Generate config files for user home"

View File

@@ -91,6 +91,9 @@ class Userconfig:
(prio, category, value, category_dir) = category_dir_tuple
self._cfg.debug.stdout(f'process category {category_dir}', 3)
for file in os.scandir(category_dir):
if file.name.endswith('.swp'):
self._cfg.debug.stdout(f'Ignoring swap file {file.name}', 3)
continue
if file.name not in file_list:
file_list[file.name] = []
file_list[file.name].append(file.path)
@@ -170,6 +173,7 @@ class Userconfig:
ret.set(section="Main", option="dest", value=ret.get(section="Main", option="dest")+"/")
return ret
# FIXME remove, unused now
@staticmethod
def read_skip_comment(fp, comment_string):
"""Read line from filehandle fp and skip all empty (whitespace) lines and lines starting with comment_string
@@ -180,7 +184,8 @@ class Userconfig:
not re.match(r"^\s+$", line)):
yield line
def diff(self, dest_file, temp_file, comment_string):
# FIXME remove, unused now
def diff_old(self, dest_file, temp_file, comment_string):
"""diff dest_file and temp_file, returns True if files differ, False if they are the same"""
self._cfg.debug.stdout(f'Diffing {dest_file} and {temp_file}, comment: {comment_string}', 3)
if not os.path.isfile(dest_file):
@@ -204,6 +209,35 @@ class Userconfig:
self._cfg.debug.stdout(f'{dest_file} is the same as generated config', 3)
return False
@staticmethod
def sanitize_input(fp, comment_string):
"""Read line from filehandle fp and skip all empty (whitespace) lines and lines starting with comment_string
return result as set
"""
ret = set()
for line in fp:
line = line[:-1]
if ((comment_string != "" and not re.match("^"+re.escape(comment_string), line)) and line != "" and
not re.match(r"^\s+$", line)):
ret.add(line)
return ret
def diff(self, dest_file, temp_file, comment_string):
"""diff dest_file and temp_file, returns True if files differ, False if they are the same"""
self._cfg.debug.stdout(f'Diffing {dest_file} and {temp_file}, comment: {comment_string}', 3)
if not os.path.isfile(dest_file):
self._cfg.debug.stdout(f'dest_file {dest_file} does not exist.', 3)
return True
if not os.path.isfile(temp_file):
self._cfg.debug.stderr(f'Temporary file {temp_file} does not exist, this should not happen.', 0, 'ERROR')
sys.exit(1)
# get temp_file and dest_file, remove comments and whitespaces
temp_lines = self.sanitize_input(open(temp_file), comment_string)
dest_lines = self.sanitize_input(open(dest_file), comment_string)
# differences of temp_file and dest_file in both directions, if != 0: files differ
return len(temp_lines.symmetric_difference(dest_lines)) != 0
def user_config_generated(self, filename):
"""returns True if filename has been generated by userconfig, False else"""

View File

@@ -2,22 +2,33 @@ import platform
def get_hostname():
hostname = platform.node()
if hostname.count("."):
hostname = hostname.split(".")[0]
return hostname
node_name = platform.node()
if node_name.count("."):
return node_name.split(".")[0]
else:
return node_name
def get_arch():
return platform.system()
def get_domain():
node_name = platform.node()
if node_name.count("."):
return '.'.join(node_name.split('.')[1:])
else:
return ''
def check_class(class_tuple):
(prio, category, value, path) = class_tuple
if category == 'Arch':
return get_arch() == value
elif category == 'Host':
return get_hostname() == value
elif category == 'Domain':
return get_domain() == value
elif value == '': # if value is empty, we cannot filter anything, so it matches always
return True
else: