Erste feature-komplette Alpha
This commit is contained in:
147
Tools.py
Normal file
147
Tools.py
Normal file
@@ -0,0 +1,147 @@
|
||||
#!/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 filecmp
|
||||
import re
|
||||
import time
|
||||
import shutil
|
||||
|
||||
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
|
||||
|
||||
def debug(self, out, level=0):
|
||||
"""docstring for debug"""
|
||||
if self.verbose:
|
||||
if level > 0:
|
||||
print "*"*level,out
|
||||
else:
|
||||
print out
|
||||
# if verbose
|
||||
# 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"""
|
||||
try:
|
||||
ret = cfgfile.Conf(filename)
|
||||
except:
|
||||
error("Error reading config file %s" % filename)
|
||||
return False
|
||||
# try
|
||||
|
||||
# check for DEST parameter
|
||||
if not ret.check("Main", "DEST"):
|
||||
error("No DEST in config file %s" % filename)
|
||||
return False
|
||||
# if no DEST
|
||||
|
||||
# replace $HOME with real home directory
|
||||
if ret.get("Main", "DEST") == "$HOME":
|
||||
ret.set("Main", "DEST", os.environ['HOME'])
|
||||
# if $HOME
|
||||
|
||||
# make sure DEST ends with /
|
||||
if not ret.get("Main", "DEST").endswith("/"):
|
||||
ret.set("Main", "DEST", ret.get("Main", "DEST")+"/")
|
||||
# if not /
|
||||
|
||||
return ret
|
||||
# def getConfig
|
||||
|
||||
def diff(destfile, tempfile, commentstring, debug):
|
||||
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
|
||||
# FIXME: filter out comments?
|
||||
debug.debug("Diffing %s and %s" % (destfile, tempfile))
|
||||
if not os.path.isfile(destfile):
|
||||
debug.debug("Destfile %s does not exist, returning True." % destfile)
|
||||
# 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
|
||||
return not filecmp.cmp(tempfile, destfile)
|
||||
|
||||
# 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
|
||||
|
||||
if not cfg.check("Main","STAMP"):
|
||||
# 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:
|
||||
if re.search(re.escape(cfg.get("Main","STAMP")), line):
|
||||
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):
|
||||
backupname = filename+".userconfig."+time.strftime("%F")
|
||||
testbackupname = backupname
|
||||
counter = 0
|
||||
while os.path.isfile(testbackupname):
|
||||
counter+=1
|
||||
testbackupname=backupname+"."+str(counter)
|
||||
# while
|
||||
debug.debug("Renaming %s to %s" % (filename, testbackupname))
|
||||
os.rename(filename, testbackupname)
|
||||
return True
|
||||
# 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
|
||||
if os.access(destfile, os.W_OK):
|
||||
debug.debug("Copying %s to %s" % (sourcefile, destfile))
|
||||
shutil.copy(sourcefile, destfile)
|
||||
return True
|
||||
# destfile is writable
|
||||
# if write ok
|
||||
return False
|
||||
# def copyFile
|
||||
Reference in New Issue
Block a user