pyuserconfig/userconfig/tools.py

161 lines
5.5 KiB
Python
Raw Normal View History

2013-01-13 00:21:20 +01:00
import sys
import os
from userconfig.cfgfile import Conf
2013-01-13 00:21:20 +01:00
import re
import time
import shutil
2013-01-13 00:21:20 +01:00
2024-03-29 22:36:07 +01:00
class Debug:
_verbose = 0
2019-06-12 23:32:05 +02:00
2024-03-31 22:20:47 +02:00
_COLOR_RED = '\033[91m'
_COLOR_BLUE = '\33[34m'
_COLOR_GREEN = '\33[32m'
_COLOR_YELLOW = '\033[93m'
_COLOR_END = '\33[0m'
_FORMAT = { 'STANDARD': '',
'ERROR': f'{_COLOR_RED}ERROR:{_COLOR_END} ',
'NOTICE': f'{_COLOR_BLUE}NOTICE:{_COLOR_END} ',
'WARNING': f'{_COLOR_YELLOW}WARNING:{_COLOR_END} ',
'SUCCESS': f'{_COLOR_GREEN}SUCCESS:{_COLOR_END} '
}
2019-06-12 23:32:05 +02:00
def __init__(self, verbose=0):
self.set_verbose(verbose)
2019-06-12 23:32:05 +02:00
def set_verbose(self, verbose):
self._verbose = verbose
2019-06-12 23:32:05 +02:00
def add_verbose(self):
self._verbose += 1
2019-06-12 23:32:05 +02:00
2024-03-29 22:36:07 +01:00
def get_verbose(self):
return self._verbose
2024-03-31 22:20:47 +02:00
def stdout(self, out, verbose_level=0, category='STANDARD'):
if self._verbose >= verbose_level:
if category in self._FORMAT:
print(f'{self._FORMAT[category]}{out}')
else:
print(f'[category {category} unknown]:{out}')
def stderr(self, out, verbose_level=0, category='STANDARD'):
if self._verbose >= verbose_level:
if category in self._FORMAT:
print(f'{self._FORMAT[category]}{out}', file=sys.stderr)
else:
print(f'[category {category} unknown]:{out}', file=sys.stderr)
2019-06-12 23:32:05 +02:00
def get_config(filename, cfg):
2019-06-12 23:32:05 +02:00
"""reads filename as config, checks for DEST parameter and returns cfgfile object"""
try:
ret = Conf(filename=filename, debug=cfg.debug, force_filename=True)
2024-03-29 22:36:07 +01:00
except ValueError:
cfg.debug.stderr("Error reading config file %s" % filename)
2019-06-12 23:32:05 +02:00
return False
# check for DEST parameter
if not ret.check(section="Main", option="dest"):
cfg.debug.stderr("No dest in config file %s" % filename)
2019-06-12 23:32:05 +02:00
return False
# make sure DEST ends with /
if not ret.get(section="Main", option="dest").endswith("/"):
ret.set(section="Main", option="dest", value=ret.get(section="Main", option="dest")+"/")
2019-06-12 23:32:05 +02:00
return ret
2013-01-13 00:21:20 +01:00
def read_skip_comment(fp, commentstring):
2019-06-12 23:32:05 +02:00
"""Read line from filehandle fp and skip all empty (whitespace) lines and lines starting with commentstring
"""
for line in fp:
line = line[:-1]
2024-03-29 22:36:07 +01:00
if ((commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line != "" and
not re.match(r"^\s+$", line)):
2019-06-12 23:32:05 +02:00
yield line
2024-03-29 22:36:07 +01:00
def diff(destfile, tempfile, commentstring, cfg):
2019-06-12 23:32:05 +02:00
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
2024-03-30 00:56:58 +01:00
cfg.debug.stdout("Diffing %s and %s, comment: %s" % (destfile, tempfile, commentstring), 3)
2019-06-12 23:32:05 +02:00
if not os.path.isfile(destfile):
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("Destfile %s does not exist, returning True." % destfile, 3)
2019-06-12 23:32:05 +02:00
# destfile does not exist -> copy tempfile over
return True
# if not destfile
if not os.path.isfile(tempfile):
# tempfile does not exist, this should never happen
2024-03-29 22:36:07 +01:00
cfg.debug.stderr("Temporary file %s does not exist, this should not happen." % tempfile)
2019-06-12 23:32:05 +02:00
sys.exit(1)
# if not tempfile
fp1 = open(tempfile)
fp2 = open(destfile)
for line1, line2 in zip(read_skip_comment(fp1, commentstring), read_skip_comment(fp2, commentstring)):
if line1 != line2:
fp1.close()
fp2.close()
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("%s differs, return true" % destfile, 3)
2019-06-12 23:32:05 +02:00
return True
# if differ
# for line
fp1.close()
fp2.close()
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("%s is the same, return false" % destfile, 3)
2013-01-13 00:21:20 +01:00
return False
2019-06-12 23:32:05 +02:00
def user_config_generated(filename, cfg):
"""returns True if filename has been generated by userconfig, False else"""
if not os.path.isfile(filename):
# filename does not exist, so it was not generated by userconfig
return False
if not cfg.check("stamp"):
2019-06-12 23:32:05 +02:00
# no STAMP in userconfig.cfg, so no way to check if file was generated by userconfig
return False
fp = open(filename, "r")
for line in fp:
if re.search(re.escape(cfg.get("stamp")), line):
2019-06-12 23:32:05 +02:00
return True
2013-01-13 00:21:20 +01:00
return False
2019-06-12 23:32:05 +02:00
2024-03-29 22:36:07 +01:00
def backup_file(filename, cfg):
2019-06-12 23:32:05 +02:00
"""make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename):
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("%s exists, finding backup name." % filename, 3)
2019-06-12 23:32:05 +02:00
backupname = filename+".userconfig."+time.strftime("%F")
testbackupname = backupname
counter = 0
while os.path.isfile(testbackupname):
2024-03-29 22:36:07 +01:00
counter += 1
testbackupname = backupname+"."+str(counter)
cfg.debug.stdout("Renaming %s to %s" % (filename, testbackupname), 1)
2019-06-12 23:32:05 +02:00
os.rename(filename, testbackupname)
return True
else:
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("%s does not exist, do not need backup." % filename, 3)
2019-06-12 23:32:05 +02:00
return False
2024-03-29 22:36:07 +01:00
def copy_file(sourcefile, destfile, cfg):
2019-06-12 23:32:05 +02:00
"""copy sourcefile to destfile, returns True if successful, False else"""
if os.path.isfile(sourcefile):
# sourcefile exists
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("Source file %s exists, proceeding with copy." % sourcefile, 3)
2019-06-12 23:32:05 +02:00
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("Copying %s to %s" % (sourcefile, destfile), 1)
2019-06-12 23:32:05 +02:00
shutil.copy(sourcefile, destfile)
return True
# destfile is writable
else:
2024-03-29 22:36:07 +01:00
cfg.debug.stdout("Destination file %s does not exist or is not writable." % destfile, 3)
2019-06-12 23:32:05 +02:00
return False