pyuserconfig/userconfig/tools.py

142 lines
4.6 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
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
def stdout(self, out, level=0):
if self._verbose >= level:
2019-06-12 23:32:05 +02:00
print(out)
def stderr(self, out, level=0):
if self._verbose >= level:
print(str(out)+"\n", 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)
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("Main", "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("Main", "dest").endswith("/"):
ret.set("Main", "dest", ret.get("Main", "dest")+"/")
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-29 22:36:07 +01:00
cfg.debug.stdout("Diffing %s and %s" % (destfile, tempfile), 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
2024-03-29 22:36:07 +01:00
if not cfg.check("Main", "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:
2024-03-29 22:36:07 +01:00
if re.search(re.escape(cfg.get("Main", "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