Commit 0d068616 authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

All tools now make use of the pidlockfile handler module.

This is a legacy commit from before 2015-05-18.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.

Not tested yet.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4381
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 13540928
......@@ -33,6 +33,7 @@
# 03/01/2012 V0.1 Daniel Armbruster
# 10/01/2012 V0.2 adjustments because every subdirectory now contains its own
# checksumfile.
# 11/01/2012 V0.4 pid lockfile handler ability added
#
# =============================================================================
......@@ -43,6 +44,7 @@ import pwd
import logging
import csfile
import csbacklog
import pidlock
__version__ = "V0.2"
__subversion__ = "$Id$"
......@@ -69,7 +71,8 @@ class Usage(Error):
"\n"+__subversion__+"\nAuthor: "+__author__+ """
Usage: csbackchk [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--tolerant] [-l|--logging] [SOURCEPATH] PATH
[-t|--tolerant] [-l|--logging] [-a|--announce] [SOURCEPATH]
PATH
or: csbackchk -h|--help\n"""
sys.stderr.write("csbackchk [line "+self.line+"]: "+self.msg+"\n")
sys.stderr.write(usage_text)
......@@ -83,7 +86,8 @@ def help():
__subversion__+"\nAuthor: "+__author__+"""
Usage: csbackchk [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--tolerant] [-l|--logging] [SOURCEPATH] PATH
[-t|--tolerant] [-l|--logging] [-a|--announce] [SOURCEPATH]
PATH
or: csbackchk -h|--help
-------------------------------------------------------------------------------
-v|--verbose Be verbose.
......@@ -101,6 +105,11 @@ def help():
e.g. as a ring buffer.
-l|--logging Switch on logging to files. Logfiles will be located in
~/.csback/log/.
-a|--announce Announce csbackchk to the csback pid lockfilehandler. This
flag is neccessary if several csback processes were run
simultaneously and the '-l|--logging' option is enabled.
Otherwise problems with the logging mechanism could
occur.
SOURCEPATH Optional sourcepath for comparison with files backed up in
PATH. PATH and its subdirectories (if option '-R' had not
been selected) must contain the csback checksumfile(s).
......@@ -129,16 +138,18 @@ def main(argv=None):
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhe:Rdftl", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "tolerant", "logging"])
opts, args = getopt.getopt(argv[1:], "vhe:Rdftla", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "tolerant", "logging", \
"announce"])
except getopt.GetoptError as err:
raise Usage(135,err.msg)
raise Usage(144,err.msg)
verbose = False
debugMode = False
notRecursive = False
followLinks = False
beTolerant = False
regexes = []
pidAnnounce = False
# collect arguments
for opt, arg in opts:
if opt in ("-v", "--verbose"):
......@@ -160,8 +171,10 @@ def main(argv=None):
beTolerant = True
elif opt in ("-l", "--logging"):
logger.configure()
elif opt in ("-a", "--announce"):
pidAnnounce = True
else:
raise Usage(164,"Unhandled option chosen.")
raise Usage(176,"Unhandled option chosen.")
if verbose or debugMode:
logger.addHandler(console)
......@@ -178,7 +191,12 @@ def main(argv=None):
inputpath = str(args[1]).rstrip(os.sep)+os.sep
inputDirs = [inputpath]
else:
raise Usage(180,"Invalid argument(s).")
raise Usage(194,"Invalid argument(s).")
# announce to pidlock
if pidAnnounce:
pidhandler = pidlock.PidHandler()
pidhandler.announce(os.getpid())
# major part
logger.getLogger().debug("Collecting subdirectories ...")
......@@ -191,11 +209,11 @@ def main(argv=None):
csfile.getSubDirectories(inputpath, regexes, followLinks))
if len(sourceDirs) != len(inputDirs):
raise Error(194, \
raise Error(212, \
"Directory structure of inputpath and sourcepath different.")
if not csfile.hasCsFile(inputpath):
raise Error(198,"PATH does not contain a checksumfile.")
raise Error(216,"PATH does not contain a checksumfile.")
paths = list(zip(inputDirs, sourceDirs))
......@@ -204,10 +222,16 @@ def main(argv=None):
checksumfile = csfile.CsFile(path[0], path[1])
checksumfile.read()
checksumfile.check(regexes, beTolerant)
# remove from pidlock
if pidAnnounce:
pidhandler.cancel(os.getpid())
except Usage as err:
err.display()
return 2
except pidlock.PidLockError as err:
logger.getLogger().error("{0}".format(err.msg))
err.display()
return 2
except Error as err:
logger.getLogger().error("message: %s [line %s]", err.msg, err.line)
err.display()
......
......@@ -33,6 +33,7 @@
# 02/01/2012 V0.1 Daniel Armbruster
# 05/01/2012 V0.2 added logging flag to enable a file logging mechanism
# 10/01/2012 V0.3 introduced --hash flag
# 11/01/2012 V0.4 pid lockfile handler ability added
#
# =============================================================================
......@@ -44,8 +45,9 @@ import pwd
import logging
import csfile
import csbacklog
import pidlock
__version__ = "V0.3"
__version__ = "V0.4"
__subversion__ = "$Id$"
__license__ = "GPLv2"
__author__ = "Daniel Armbruster"
......@@ -68,7 +70,8 @@ class Usage(Error):
"\n"+__subversion__+"\nAuthor: "+__author__+ """
Usage: csbackgen [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--target DIR] [-l|--logging] [--hash ARG] PATH
[-t|--target DIR] [-l|--logging] [--hash ARG]
[-a|--announce] PATH
or: csbackgen -h|--help\n"""
sys.stderr.write("csbackgen: " + self.msg + "\n")
sys.stderr.write(usage_text)
......@@ -82,7 +85,8 @@ def help():
__subversion__+"\nAuthor: "+__author__+"""
Usage: csbackgen [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--target ROOTDIR] [-l|--logging] [--hash ARG] PATH
[-t|--target ROOTDIR] [-l|--logging] [--hash ARG]
[-a|--announce] PATH
or: csbackgen -h|--help
-------------------------------------------------------------------------------
-v|--verbose Be verbose.
......@@ -102,6 +106,11 @@ def help():
~/.csback/log/.
--hash ARG Set the hash function algorithm. Valid values are:
sha224, sha256, sha384, sha512. (default: sha256)
-a|--announce Announce csbackgen to the csback pid lockfilehandler.
This flag is neccessary if several csback processes were
run simultaneously and the '-l|--logging' option is
enabled. Otherwise problems with the logging mechanism
could occur.
PATH Path to generate checksumfile(s) for including its
subdirectories if option '-R' is not set.
-------------------------------------------------------------------------------
......@@ -131,9 +140,9 @@ def main(argv=None):
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhe:Rdft:l", ["help", "verbose", \
opts, args = getopt.getopt(argv[1:], "vhe:Rdft:la", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "target=", "logging", \
"hash="])
"hash=", "announce"])
except getopt.GetoptError as err:
raise Usage(err.msg)
verbose = False
......@@ -144,6 +153,7 @@ def main(argv=None):
targetSet = False
enableLogging = False
hashfunc = 'sha256'
pidAnnounce = False
for opt, arg in opts:
if opt in ("-v", "--verbose"):
verbose = True
......@@ -169,6 +179,8 @@ def main(argv=None):
hashfunc = arg
else:
raise Usage("Invalid argument (hash).")
elif opt in ("-a", "--announce"):
pidAnnounce = True
else:
raise Usage("Unhandled option chosen.")
......@@ -182,6 +194,11 @@ def main(argv=None):
raise Usage("Invalid argument.")
if not os.path.isdir(srcpath):
raise Usage("PATH not a valid directory.")
# announce to pidlock
if pidAnnounce:
pidhandler = pidlock.PidHandler()
pidhandler.announce(os.getpid())
# major part
logger.getLogger().debug("Start collecting subdirectories ...")
......@@ -209,10 +226,18 @@ def main(argv=None):
"Updating checksumfile in '{0}' with files from '{1}' ...".format( \
path[0], path[1]))
csfile.CsFile(path[0], path[1], hashfunc=hashfunc).update(regexes)
# remove from pidlock
if pidAnnounce:
pidhandler.cancel(os.getpid())
except Usage as err:
err.display()
return 2
except pidlock.PidLockError as err:
logger.getLogger().error("{0}".format(err.msg))
err.display()
return 2
except csfile.CsFileError as err:
logger.getLogger().error("{0}".format(err.msg))
err.display()
......
......@@ -33,6 +33,7 @@
#
# REVISIONS and CHANGES
# 05/12/2012 V0.1 Daniel Armbruster
# 11/01/2012 V0.4 pid lockfile handler ability added
#
# =============================================================================
"""
......@@ -46,6 +47,7 @@ import re
import smtplib
import logging
import csbacklog
import pidlock
from email.mime.text import MIMEText
from collections import deque
......@@ -73,10 +75,10 @@ class Usage(Error):
def display(self):
usage_text = "Version: "+__version__+"\nLicense: "+__license__+ \
"\n"+__subversion__+"\nAuthor: "+__author__+ """
Usage: csbackmail [-v|--verbose] [-d|--debug] [-l|--logging] [-P|--port ARG]
[-H|--host ARG] [-u|--username ARG] [-p|--password ARG]
Usage: csbackmail [-v|--verbose] [-d|--debug] [-l|--logging] -P|--port ARG
-H|--host ARG -u|--username ARG -p|--password ARG
-r|--receiver ADDRESS [-r|--receiver ADDRESS [...]]
-s|--sender ADDRESS [PATH [PATH [...]]]
-s|--sender ADDRESS [-a|--announce] [PATH [PATH [...]]]
or: csbackmail -h|--help\n"""
sys.stderr.write("csbackmail [line "+self.line+"]: "+self.msg+"\n")
sys.stderr.write(usage_text)
......@@ -88,10 +90,10 @@ def help():
"""
help_text = "Version: "+__version__+"\nLicense: "+__license__+"\n"+ \
__subversion__+"\nAuthor: "+__author__+"""
Usage: csbackmail [-v|--verbose] [-d|--debug] [-l|--logging] [-P|--port ARG]
[-H|--host ARG] [-u|--username ARG] [-p|--password ARG]
Usage: csbackmail [-v|--verbose] [-d|--debug] [-l|--logging] -P|--port ARG
-H|--host ARG -u|--username ARG -p|--password ARG
-r|--receiver ADDRESS [-r|--receiver ADDRESS [...]]
-s|--sender ADDRESS [PATH [PATH [...]]]
-s|--sender ADDRESS [-a|--announce] [PATH [PATH [...]]]
or: csbackmail -h|--help
-------------------------------------------------------------------------------
-v|--verbose Be verbose.
......@@ -106,6 +108,11 @@ def help():
-H|--host ARG Hostname of the SMTP server.
-u|--username ARG Username for SMTP server login.
-p|--password ARG Password for SMTP server login.
-a|--announce Announce csbackmail to the csback pid lockfilehandler. This
flag is neccessary if several csback processes were run
simultaneously and the '-l|--logging' option is enabled.
Otherwise problems with the logging mechanism could
occur.
PATH Path(s) of the logfile(s) to check. If not specified the
logfiles in ~/.csback/log/ were investigated.
"""
......@@ -125,9 +132,9 @@ def main(argv=None):
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhdlr:s:P:H:u:p:", ["help", \
opts, args = getopt.getopt(argv[1:], "vhdlr:s:P:H:u:p:a", ["help", \
"verbose", "debug", "logging", "receiver=", "sender=", "port=", \
"host=", "username=", "password="])
"host=", "username=", "password=", "announce"])
except getopt.GetoptError as err:
raise Usage(131,err.msg)
verbose = False
......@@ -138,6 +145,7 @@ def main(argv=None):
host = ''
username = ''
password = ''
pidAnnounce = False
# collect commandline arguments
for opt, arg in opts:
if opt in ("-v", "--verbose"):
......@@ -163,12 +171,19 @@ def main(argv=None):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-a", "--announce"):
pidAnnounce = True
else:
raise Usage(167,"Unhandled option chosen.")
if verbose or debugMode:
logger.addHandler(console)
# announce to pidlock
if pidAnnounce:
pidhandler = pidlock.PidHandler()
pidhandler.announce(os.getpid())
# fetching and checking arguments
if 1 <= len(args):
logger.getLogger().info("Taking passed PATH(s) as arguments.")
......@@ -297,9 +312,16 @@ def main(argv=None):
else:
session.quit()
# remove from pidlock
if pidAnnounce:
pidhandler.cancel(os.getpid())
except Usage as err:
err.display()
return 2
except pidlock.PidLockError as err:
logger.getLogger().error("{0}".format(err.msg))
err.display()
return 2
except Error as err:
logger.getLogger().error("message: %s [line %s]", err.msg, err.line)
err.display()
......
......@@ -40,7 +40,7 @@ PidLockError which might be caught while announcing a pid.
The path of the lockfile should be configured using the global variables.
Default values are:
---
path = os.path.expanduser("~")
path = os.path.expanduser("~/.csback")
lockfilename = ".lock"
---
"""
......@@ -57,7 +57,7 @@ __copyright__ = "Copyright (c) 2012 by Daniel Armbruster"
# -----------------------------------------------------------------------------
# variables
path = os.path.expanduser("~")
path = os.path.expanduser("~/.csback")
lockfilename = ".lock"
# -----------------------------------------------------------------------------
......@@ -78,8 +78,8 @@ class PidHandler:
"""
Implements a handler for a pid. This handler uses a pidlockfile mechanism to
keep a log of the announced pids. Because of using a lockfile the handler
class should be tread-safe. This means that different instances of handlers
sould be able tp use the same lockfile.
class should be thread-safe. This means that different instances of handlers
should be able to use the same lockfile.
Note that this handler does its work only on *nix platforms properly. Use
inheritance to overload the corresponding functions.
"""
......@@ -91,6 +91,12 @@ class PidHandler:
"""
Announce a process with pid to the handler.
"""
# create directories if not available
if not os.access(path, os.F_OK):
try:
os.makedirs(path, mode='0755')
except OSError:
pass
# remove file if pid not valid anymore
if self.__access() and not self.__pidValid():
os.remove(self.path)
......@@ -102,7 +108,7 @@ class PidHandler:
pidfile = open(self.path, 'w')
pidfile.write("{0}".format(pid))
except IOError as err:
raise PidLockError(105, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise PidLockError(111, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
pidfile.close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment