pyuserconfig/Userconfig/Tools.py

157 lines
4.6 KiB
Python
Raw Normal View History

2019-06-12 23:32:05 +02:00
#!/usr/bin/env python3
2013-01-13 00:21:20 +01:00
# encoding: utf-8
"""
Tools.py
Created by Marcus Stoegbauer on 2013-01-12.
"""
import sys
import os
2019-06-12 23:32:05 +02:00
import Userconfig.cfgfile as cfgfile
2013-01-13 00:21:20 +01:00
import re
import time
import shutil
2013-01-13 00:21:20 +01:00
class Debug(object):
2019-06-12 23:32:05 +02:00
verbose = 0
def __init__(self, verbose=0):
self.setverbose(verbose)
def setverbose(self, verbose):
"""docstring for setverbose"""
self.verbose = verbose
def addverbose(self):
"""docstring for setverbose"""
self.verbose += 1
def debug(self, out, level=0):
"""docstring for debug"""
if self.verbose >= level:
print(out)
2013-01-13 00:21:20 +01:00
def error(out):
2019-06-12 23:32:05 +02:00
"""Print error on stderr"""
print(str(out)+"\n", file=sys.stderr)
def get_config(filename):
"""reads filename as config, checks for DEST parameter and returns cfgfile object"""
ret = None
try:
ret = cfgfile.Conf(filename)
except:
error("Error reading config file %s" % filename)
return False
# check for DEST parameter
if not ret.check("Main", "dest"):
error("No DEST in config file %s" % filename)
return False
# replace $HOME with real home directory
if ret.get("Main", "dest") == "$HOME":
ret.set("Main", "dest", os.environ['HOME'])
# 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]
if (commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line !="" and not re.match("^\s+$", line):
yield line
2013-01-13 00:21:20 +01:00
def diff(destfile, tempfile, commentstring, debug):
2019-06-12 23:32:05 +02:00
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
debug.debug("Diffing %s and %s" % (destfile, tempfile), 3)
if not os.path.isfile(destfile):
debug.debug("Destfile %s does not exist, returning True." % destfile, 3)
# 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
error("Temporary file %s does not exist, this should not happen." % tempfile)
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()
return True
# if differ
# for line
fp1.close()
fp2.close()
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("Main","stamp"):
# 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("Main","stamp")), line):
return True
2013-01-13 00:21:20 +01:00
return False
2019-06-12 23:32:05 +02:00
def backup_file(filename, debug):
"""make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename):
debug.debug("%s exists, finding backup name." % filename, 3)
backupname = filename+".userconfig."+time.strftime("%F")
testbackupname = backupname
counter = 0
while os.path.isfile(testbackupname):
counter+=1
testbackupname=backupname+"."+str(counter)
debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
os.rename(filename, testbackupname)
return True
else:
2019-06-12 23:32:05 +02:00
debug.debug("%s does not exist, do not need backup." % filename, 3)
return False
def copy_file(sourcefile, destfile, debug):
"""copy sourcefile to destfile, returns True if successful, False else"""
if os.path.isfile(sourcefile):
# sourcefile exists
debug.debug("Source file %s exists, proceeding with copy." % sourcefile, 3)
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
debug.debug("Copying %s to %s" % (sourcefile, destfile), 1)
shutil.copy(sourcefile, destfile)
return True
# destfile is writable
else:
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
return False