2013-01-13 00:21:20 +01:00
|
|
|
import sys
|
|
|
|
import os
|
2024-03-29 21:48:06 +01:00
|
|
|
from userconfig.cfgfile import Conf
|
2013-01-13 00:21:20 +01:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import shutil
|
2013-01-13 13:27:33 +01:00
|
|
|
|
2013-01-13 00:21:20 +01:00
|
|
|
|
2024-03-29 22:36:07 +01:00
|
|
|
class Debug:
|
2024-03-29 21:48:06 +01:00
|
|
|
_verbose = 0
|
2019-06-12 23:32:05 +02:00
|
|
|
|
|
|
|
def __init__(self, verbose=0):
|
2024-03-29 21:48:06 +01:00
|
|
|
self.set_verbose(verbose)
|
2019-06-12 23:32:05 +02:00
|
|
|
|
2024-03-29 21:48:06 +01:00
|
|
|
def set_verbose(self, verbose):
|
|
|
|
self._verbose = verbose
|
2019-06-12 23:32:05 +02:00
|
|
|
|
2024-03-29 21:48:06 +01: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-29 21:48:06 +01:00
|
|
|
def stdout(self, out, level=0):
|
|
|
|
if self._verbose >= level:
|
2019-06-12 23:32:05 +02:00
|
|
|
print(out)
|
|
|
|
|
2024-03-29 21:48:06 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
2024-03-29 21:48:06 +01: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:
|
2024-03-29 21:48:06 +01:00
|
|
|
ret = Conf(filename)
|
2024-03-29 22:36:07 +01:00
|
|
|
except ValueError:
|
2024-03-29 21:48:06 +01:00
|
|
|
cfg.debug.stderr("Error reading config file %s" % filename)
|
2019-06-12 23:32:05 +02:00
|
|
|
return False
|
|
|
|
# check for DEST parameter
|
2024-03-30 00:25:35 +01:00
|
|
|
if not ret.check(section="Main", option="dest"):
|
2024-03-29 21:48:06 +01:00
|
|
|
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 /
|
2024-03-30 00:25:35 +01:00
|
|
|
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
|
|
|
|
2013-01-13 13:27:33 +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
|
|
|
|
|
2013-01-13 13:27:33 +01:00
|
|
|
|
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
|
|
|
|
|
2024-03-30 00:25:35 +01:00
|
|
|
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:
|
2024-03-30 00:25:35 +01:00
|
|
|
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
|
2013-01-14 23:23:48 +01:00
|
|
|
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
|