2013-01-13 00:21:20 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
# $Id$
|
|
|
|
# $URL$
|
|
|
|
|
|
|
|
"""
|
|
|
|
Tools.py
|
|
|
|
|
|
|
|
Created by Marcus Stoegbauer on 2013-01-12.
|
|
|
|
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import cfgfile
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import shutil
|
2013-01-13 13:27:33 +01:00
|
|
|
import itertools
|
|
|
|
|
2013-01-13 00:21:20 +01:00
|
|
|
|
|
|
|
class Debug(object):
|
|
|
|
verbose = 0
|
|
|
|
|
|
|
|
def __init__(self, verbose=0):
|
|
|
|
self.setverbose(verbose)
|
|
|
|
# def __init__
|
|
|
|
|
|
|
|
def setverbose(self, verbose):
|
|
|
|
"""docstring for setverbose"""
|
|
|
|
self.verbose = verbose
|
|
|
|
# def setverbose
|
|
|
|
|
2013-01-14 23:23:48 +01:00
|
|
|
def addverbose(self):
|
|
|
|
"""docstring for setverbose"""
|
|
|
|
self.verbose += 1
|
|
|
|
# def addverbose
|
|
|
|
|
2013-01-13 00:21:20 +01:00
|
|
|
def debug(self, out, level=0):
|
|
|
|
"""docstring for debug"""
|
2013-01-14 23:23:48 +01:00
|
|
|
if self.verbose >= level:
|
|
|
|
print out
|
|
|
|
# if level
|
2013-01-13 00:21:20 +01:00
|
|
|
# def debug
|
|
|
|
# class Debug
|
|
|
|
|
|
|
|
def error(out):
|
|
|
|
"""Print error on stderr"""
|
|
|
|
print >> sys.stderr, str(out)+"\n"
|
|
|
|
# def error
|
|
|
|
|
|
|
|
def getConfig(filename):
|
|
|
|
"""reads filename as config, checks for DEST parameter and returns cfgfile object"""
|
2013-01-13 22:32:33 +01:00
|
|
|
ret = None
|
2013-01-13 00:21:20 +01:00
|
|
|
try:
|
|
|
|
ret = cfgfile.Conf(filename)
|
|
|
|
except:
|
|
|
|
error("Error reading config file %s" % filename)
|
|
|
|
return False
|
|
|
|
# try
|
|
|
|
|
|
|
|
# check for DEST parameter
|
2013-01-13 22:32:33 +01:00
|
|
|
if not ret.check("Main", "dest"):
|
2013-01-13 00:21:20 +01:00
|
|
|
error("No DEST in config file %s" % filename)
|
|
|
|
return False
|
|
|
|
# if no DEST
|
|
|
|
|
|
|
|
# replace $HOME with real home directory
|
2013-01-13 22:32:33 +01:00
|
|
|
if ret.get("Main", "dest") == "$HOME":
|
|
|
|
ret.set("Main", "dest", os.environ['HOME'])
|
2013-01-13 00:21:20 +01:00
|
|
|
# if $HOME
|
|
|
|
|
|
|
|
# make sure DEST ends with /
|
2013-01-13 22:32:33 +01:00
|
|
|
if not ret.get("Main", "dest").endswith("/"):
|
|
|
|
ret.set("Main", "dest", ret.get("Main", "dest")+"/")
|
2013-01-13 00:21:20 +01:00
|
|
|
# if not /
|
|
|
|
|
|
|
|
return ret
|
|
|
|
# def getConfig
|
|
|
|
|
2013-01-13 13:27:33 +01:00
|
|
|
def read_skip_comment(fp, commentstring):
|
|
|
|
"""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
|
|
|
|
# if not match
|
|
|
|
# for line
|
|
|
|
# def read_skip_comment
|
|
|
|
|
2013-01-13 00:21:20 +01:00
|
|
|
def diff(destfile, tempfile, commentstring, debug):
|
|
|
|
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Diffing %s and %s" % (destfile, tempfile), 3)
|
2013-01-13 00:21:20 +01:00
|
|
|
if not os.path.isfile(destfile):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Destfile %s does not exist, returning True." % destfile, 3)
|
2013-01-13 00:21:20 +01: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
|
|
|
|
error("Temporary file %s does not exist, this should not happen." % tempfile)
|
|
|
|
sys.exit(1)
|
|
|
|
# if not tempfile
|
2013-01-13 13:27:33 +01:00
|
|
|
|
|
|
|
fp1 = open(tempfile)
|
|
|
|
fp2 = open(destfile)
|
|
|
|
|
|
|
|
for line1, line2 in itertools.izip(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()
|
|
|
|
return False
|
2013-01-13 00:21:20 +01:00
|
|
|
# def diff
|
|
|
|
|
|
|
|
def userConfigGenerated(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 filename
|
|
|
|
|
2013-01-13 22:32:33 +01:00
|
|
|
if not cfg.check("Main","stamp"):
|
2013-01-13 00:21:20 +01:00
|
|
|
# no STAMP in userconfig.cfg, so no way to check if file was generated by userconfig
|
|
|
|
return False
|
|
|
|
# no STAMP
|
|
|
|
|
|
|
|
fp = open(filename, "r")
|
|
|
|
|
|
|
|
for line in fp:
|
2013-01-13 22:32:33 +01:00
|
|
|
if re.search(re.escape(cfg.get("Main","stamp")), line):
|
2013-01-13 00:21:20 +01:00
|
|
|
return True
|
|
|
|
# if search
|
|
|
|
# for line
|
|
|
|
return False
|
|
|
|
# def userConfigGenerated
|
|
|
|
|
|
|
|
def backupFile(filename, debug):
|
|
|
|
"""make backup of filename, returns True if backup is successful, False else"""
|
|
|
|
if os.path.isfile(filename):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("%s exists, finding backup name." % filename, 3)
|
2013-01-13 00:21:20 +01:00
|
|
|
backupname = filename+".userconfig."+time.strftime("%F")
|
|
|
|
testbackupname = backupname
|
|
|
|
counter = 0
|
|
|
|
while os.path.isfile(testbackupname):
|
|
|
|
counter+=1
|
|
|
|
testbackupname=backupname+"."+str(counter)
|
|
|
|
# while
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
|
2013-01-13 00:21:20 +01:00
|
|
|
os.rename(filename, testbackupname)
|
|
|
|
return True
|
2013-01-14 23:23:48 +01:00
|
|
|
else:
|
|
|
|
debug.debug("%s does not exist, do not need backup." % filename, 3)
|
2013-01-13 00:21:20 +01:00
|
|
|
# if filename
|
|
|
|
return False
|
|
|
|
# def backupFile
|
|
|
|
|
|
|
|
def copyFile(sourcefile, destfile, debug):
|
|
|
|
"""copy sourcefile to destfile, returns True if successful, False else"""
|
|
|
|
|
|
|
|
if os.path.isfile(sourcefile):
|
|
|
|
# sourcefile exists
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Source file %s exists, proceeding with copy." % sourcefile, 3)
|
2013-01-13 01:04:40 +01:00
|
|
|
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
|
2013-01-14 23:23:48 +01:00
|
|
|
debug.debug("Copying %s to %s" % (sourcefile, destfile), 1)
|
2013-01-13 00:21:20 +01:00
|
|
|
shutil.copy(sourcefile, destfile)
|
|
|
|
return True
|
|
|
|
# destfile is writable
|
2013-01-14 23:23:48 +01:00
|
|
|
else:
|
|
|
|
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
|
2013-01-13 00:21:20 +01:00
|
|
|
# if write ok
|
|
|
|
return False
|
|
|
|
# def copyFile
|