Commit 312907cf authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

Both source:tfsoftware/trunk/src/python/csback/csbackgen.py and

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.
source:tfsoftware/trunk/src/python/csback/csbackchk.py make use of a csback log
module.

The log module centralizes the configuration of the csback logging mechanism. A
file logging mechanism had been implemented as well. File logging can be enabled
in both programs using the '-l'-flag.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4342
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 53f05586
......@@ -39,8 +39,8 @@ import os
import sys
import pwd
import logging
import logging.config
import csfile
import csbacklog
__version__ = "V0.1"
__subversion__ = "$Id$"
......@@ -67,7 +67,7 @@ class Usage(Error):
"\n"+__subversion__+"\nAuthor: "+__author__+ """
Usage: csbackchk [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--tolerant] [SOURCEPATH] PATH
[-t|--tolerant] [-l|--logging] [SOURCEPATH] PATH
or: csbackchk -h|--help\n"""
sys.stderr.write("csbackchk [line "+self.line+"]: "+self.msg+"\n")
sys.stderr.write(usage_text)
......@@ -81,7 +81,7 @@ def help():
__subversion__+"\nAuthor: "+__author__+"""
Usage: csbackchk [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--tolerant] [SOURCEPATH] PATH
[-t|--tolerant] [-l|--logging] [SOURCEPATH] PATH
or: csbackchk -h|--help
-------------------------------------------------------------------------------
-v|--verbose Be verbose.
......@@ -97,6 +97,8 @@ def help():
the checksumfile is missing. This flag is useful if a test
of a directory is executed but this directory is realized
e.g. as a ring buffer.
-l|--logging Switch on logging to files. Logfiles will be located in
~/.csback/log/.
SOURCEPATH Optional sourcepath for comparison with files backed up in
PATH. PATH and its subdirectories (if option '-R' is not
set) must contain the csback checksumfile(s).
......@@ -111,30 +113,21 @@ def help():
# -----------------------------------------------------------------------------
def main(argv=None):
# configure logger
logInfo = {'hostname': os.uname()[1], \
'user': pwd.getpwuid(os.getuid()).pw_name, \
'pid': os.getpid()}
csfile.csfileLogInfo = logInfo
csfile.csfileLoggerName = 'csbackchk.csfile'
log = logging.getLogger('csbackchk')
logger = logging.LoggerAdapter(log, logInfo)
logging.basicConfig(level=logging.DEBUG, \
format='%(asctime)-15s %(hostname)-6s %(name)-8s[%(pid)s]: \
(%(user)-4s) %(levelname)-8s %(message)s')
logger = csbacklog.CsbackLog('csbackchk')
console = logging.StreamHandler()
formatter = logging.Formatter( \
'%(name)-8s [%(lineno)d]: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
console.setFormatter(logging.Formatter( \
'%(name)-8s [%(lineno)d]: %(levelname)-8s %(message)s'))
# fetch arguments
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhe:Rdft", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "tolerant"])
opts, args = getopt.getopt(argv[1:], "vhe:Rdftl", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "tolerant", "logging"])
except getopt.GetoptError as err:
raise Usage(137,err.msg)
raise Usage(130,err.msg)
verbose = False
debugMode = False
notRecursive = False
......@@ -160,11 +153,13 @@ def main(argv=None):
followLinks = True
elif opt in ("-t", "--tolerant"):
beTolerant = True
elif opt in ("-l", "--logging"):
logger.configure()
else:
raise Usage(164,msg="Unhandled option chosen.")
raise Usage(159,msg="Unhandled option chosen.")
if verbose or debugMode:
log.addHandler(console)
logger.addHandler(console)
if 1 == len(args):
sourcepath = str(args[0]).rstrip(os.sep)+os.sep
......@@ -173,14 +168,16 @@ def main(argv=None):
sourcepath = str(args[0]).rstrip(os.sep)+os.sep
inputpath = str(args[1]).rstrip(os.sep)+os.sep
else:
raise Usage(176,"Invalid argument(s).")
raise Usage(171,"Invalid argument(s).")
# major part
logger.getLogger().info("Start checking checksums ...")
if not csfile.hasCsFile(inputpath):
raise Error(180,"PATH does not contain a checksumfile.")
raise Error(175,"PATH does not contain a checksumfile.")
if notRecursive:
logger.debug("Fetching subdirectories for regexes in %s", inputpath)
logger.getLogger().debug("Fetching subdirectories for regexes in %s", \
inputpath)
subdirs = csfile.getSubDirectories(inputpath, regexes, followLinks)
subdirs = [subdir+"*" for subdir in subdirs]
regexes.extend(subdirs)
......@@ -194,15 +191,16 @@ def main(argv=None):
err.display()
return 2
except Error as err:
logger.error("csbackchk.Error occured while checking files.")
logger.getLogger().error("csbackchk.Error occured while checking files.")
err.display()
return 2
except csfile.CsFileError as err:
logger.error("csfile.CsFileError occured while checking files.")
logger.getLogger().error( \
"csfile.CsFileError occured while checking files.")
err.display()
return 2
else:
logger.info("Checks performed.")
logger.getLogger().info("Checks performed.")
return 0
# -----------------------------------------------------------------------------
......
......@@ -31,6 +31,7 @@
#
# REVISIONS and CHANGES
# 02/01/2012 V0.1 Daniel Armbruster
# 05/01/2012 V0.2 added logging flag to enable a file logging mechanism
#
# =============================================================================
......@@ -41,6 +42,7 @@ import re
import pwd
import logging
import csfile
import csbacklog
__version__ = "V0.1"
__subversion__ = "$Id$"
......@@ -65,7 +67,7 @@ class Usage(Error):
"\n"+__subversion__+"\nAuthor: "+__author__+ """
Usage: csbackgen [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--target DIR] PATH
[-t|--target DIR] [-l|--logging] PATH
or: csbackgen -h|--help\n"""
sys.stderr.write("csbackgen: " + self.msg + "\n")
sys.stderr.write(usage_text)
......@@ -79,7 +81,7 @@ def help():
__subversion__+"\nAuthor: "+__author__+"""
Usage: csbackgen [-v|--verbose] [-e REGEX [-e REGEX [...]]]
[-R|--notrecursive] [-d|--debug] [-f|--followlinks]
[-t|--target DIR] PATH
[-t|--target DIR] [-l|--logging] PATH
or: csbackgen -h|--help
-------------------------------------------------------------------------------
-v|--verbose Be verbose.
......@@ -91,6 +93,8 @@ def help():
-f|--followlinks Follow symbolic links. Only available if option -R is not
set. Note that this option can lead to infinite recursion.
-t|--target Target directory for checksumfile.
-l|--logging Switch on logging to files. Logfiles will be located in
~/.csback/log/.
PATH Path to generate checksumfile for.
-------------------------------------------------------------------------------
csbackgen.py will either generate a checksumfile if still no checksumfile is
......@@ -102,27 +106,18 @@ csbackgen.py is working in its update mode.\n"""
# -----------------------------------------------------------------------------
def main(argv=None):
# configure logger
logInfo = {'hostname': os.uname()[1], \
'user': pwd.getpwuid(os.getuid()).pw_name, \
'pid': os.getpid()}
csfile.csfileLogInfo = logInfo
csfile.csfileLoggerName = 'csbackgen.csfile'
log = logging.getLogger('csbackgen')
logger = logging.LoggerAdapter(log, logInfo)
logging.basicConfig(level=logging.DEBUG, \
format='%(asctime)-15s %(hostname)-6s %(name)-8s[%(pid)s]: \
(%(user)-4s) %(levelname)-8s %(message)s')
logger = csbacklog.CsbackLog('csbackgen')
console = logging.StreamHandler()
formatter = logging.Formatter( \
'%(name)-8s [%(lineno)d]: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
console.setFormatter(logging.Formatter( \
'%(name)-8s [%(lineno)d]: %(levelname)-8s %(message)s'))
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhe:Rdft:", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "target="])
opts, args = getopt.getopt(argv[1:], "vhe:Rdft:l", ["help", "verbose", \
"notrecursive", "debug", "followlinks", "target=", "logging"])
except getopt.GetoptError as err:
raise Usage(err.msg)
verbose = False
......@@ -131,6 +126,7 @@ def main(argv=None):
followLinks = False
regexes = []
targetSet = False
enableLogging = False
for opt, arg in opts:
if opt in ("-v", "--verbose"):
verbose = True
......@@ -149,11 +145,13 @@ def main(argv=None):
elif opt in ("-t", "--target"):
targetSet = True
targetDirectory = str(arg)
elif opt in ("-l", "--logging"):
logger.configure()
else:
raise Usage("Unhandled option chosen.")
if verbose or debugMode:
log.addHandler(console)
logger.addHandler(console)
# fetch arguments
if 1 == len(args):
......@@ -169,11 +167,12 @@ def main(argv=None):
# major part
directories = [inputpath]
if not notRecursive:
logger.info("Collecting subdirectories.")
logger.getLogger().info("Collecting subdirectories.")
directories.extend(csfile.getSubDirectories(inputpath, regexes, \
followLinks))
for dir in directories:
logger.info("Update files in directory: %s to checksumfile: %s%s", dir, \
logger.getLogger().info( \
"Update files in directory: %s to checksumfile: %s%s", dir, \
targetDirectory, csfile.CsFile.filename)
csfile.CsFile(targetDirectory, srcpath=dir).update(regexes)
......@@ -184,7 +183,7 @@ def main(argv=None):
err.display()
return 2
else:
logger.info("Checksumfile updated.")
logger.getLogger().info("Checksumfile updated.")
return 0
......
#!/usr/bin/env python
## @file csbacklog.py
# @brief Module which is responsible for the csback logging mechanism.
#
# -----------------------------------------------------------------------------
#
# $Id$
# @author Daniel Armbruster
# \date 05/01/2012
#
# Purpose: Module which is responsible for the csback logging mechanism.
#
# ----
# This file is part of csback.
#
# csback is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# csback is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with csback. If not, see <http://www.gnu.org/licenses/>.
# ----
#
# Copyright (c) 2012 by Daniel Armbruster
#
# REVISIONS and CHANGES
# 05/01/2012 V0.1 Daniel Armbruster
#
# =============================================================================
"""
Module which is responsible for the csback logging mechanism.
"""
import os
import pwd
import logging
import logging.handlers
import csfile
__version__ = "V0.1"
__subversion__ = "$Id$"
__license__ = "GPLv2"
__author__ = "Daniel Armbruster"
__copyright__ = "Copyright (c) 2012 by Daniel Armbruster"
# ----------------------------------------------------------------------------
class CsbackLog :
"""
Handles and configures the csback logging mechansism
"""
def __init__(self, applicationName):
self.applicationName = applicationName
self.logger = logging.getLogger(applicationName)
self.csbacklogger = logging.LoggerAdapter(self.logger, CsbackLog.logInfo)
self.csbacklogger.setLevel(logging.DEBUG)
csfile.csfileLogInfo = CsbackLog.logInfo
csfile.csfileLoggerName = str(applicationName)+'.csfile'
def configure(self):
"""
Function to configure the csback logging mechanism. The only function which
has to be called once if logging to files is desired.
"""
# check log pathes and create directories if necessary
try:
os.makedirs(CsbackLog.logFileDir)
except OSError:
self.csbacklogger.debug("Csback log directories allready exist.")
else:
self.csbacklogger.debug("Created csback log directories.")
# add rotatedFileHandler to logger
filehandler = logging.handlers.RotatingFileHandler(os.path.join( \
CsbackLog.logFileDir,CsbackLog.logFileName), maxBytes=1024*1024*3, \
backupCount=5)
filehandler.setLevel(logging.INFO)
filehandler.setFormatter(logging.Formatter(CsbackLog.formatter))
self.logger.addHandler(filehandler)
def addHandler(self, handler):
"""
Add a handler to the csback logger.
"""
self.logger.addHandler(handler)
def getLogger(self):
return self.csbacklogger
formatter = '%(asctime)-15s %(hostname)-6s %(name)-8s[%(pid)s]: \
(%(user)-4s) %(levelname)-8s %(message)s'
logInfo = {'hostname': os.uname()[1], \
'user': pwd.getpwuid(os.getuid()).pw_name, \
'pid': os.getpid()}
logFileDir = os.path.expanduser("~/.csback/log")
logFileName = "csback.log"
# ----------------------------------------------------------------------------
if __name__ == "__main__":
pass
# ----- END OF csbacklog.py -----
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