Skip to content

Commit

Permalink
Start using verboselogs
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jul 8, 2016
1 parent 8338d95 commit 6b28d4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ naturalsort >= 1.4
property-manager >= 2.0
python-dateutil >= 2.2
six >= 1.9.0
verboselogs >= 1.4
22 changes: 11 additions & 11 deletions rotate_backups/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# rotate-backups: Simple command line interface for backup rotation.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: July 8, 2016
# Last Change: July 9, 2016
# URL: https://github.com/xolox/python-rotate-backups

"""
Expand All @@ -16,7 +16,6 @@
import collections
import datetime
import fnmatch
import logging
import os
import re

Expand All @@ -29,12 +28,13 @@
from property_manager import PropertyManager, key_property, required_property
from six import string_types
from six.moves import configparser
from verboselogs import VerboseLogger

# Semi-standard module versioning.
__version__ = '3.2'
__version__ = '3.3'

# Initialize a logger for this module.
logger = logging.getLogger(__name__)
logger = VerboseLogger(__name__)

GLOBAL_CONFIG_FILE = '/etc/rotate-backups.ini'
"""The pathname of the system wide configuration file (a string)."""
Expand Down Expand Up @@ -148,7 +148,7 @@ def load_config_file(configuration_file=None):
"""
parser = configparser.RawConfigParser()
if configuration_file:
logger.debug("Using custom configuration file: %s", format_path(configuration_file))
logger.verbose("Reading configuration file %s ..", format_path(configuration_file))
loaded_files = parser.read(configuration_file)
if len(loaded_files) == 0:
msg = "Failed to read configuration file! (%s)"
Expand All @@ -157,7 +157,7 @@ def load_config_file(configuration_file=None):
for config_file in LOCAL_CONFIG_FILE, GLOBAL_CONFIG_FILE:
pathname = parse_path(config_file)
if parser.read(pathname):
logger.debug("Using configuration file %s ..", format_path(pathname))
logger.verbose("Reading configuration file %s ..", format_path(pathname))
break
for section in parser.sections():
items = dict(parser.items(section))
Expand Down Expand Up @@ -334,7 +334,7 @@ class together to implement backup rotation with an easy to use Python
command = ['ionice', '--class', self.io_scheduling_class] + command
timer = Timer()
location.context.execute(*command)
logger.debug("Deleted %s in %s.", format_path(backup.pathname), timer)
logger.verbose("Deleted %s in %s.", format_path(backup.pathname), timer)
if len(backups_to_preserve) == len(sorted_backups):
logger.info("Nothing to do! (all backups preserved)")

Expand All @@ -348,14 +348,14 @@ def load_config_file(self, location):
location = coerce_location(location)
for configured_location, rotation_scheme, options in load_config_file(self.config_file):
if location == configured_location:
logger.debug("Loading custom configuration for location (%s) ..", location)
logger.verbose("Loading configuration for %s ..", location)
if rotation_scheme:
self.rotation_scheme = rotation_scheme
for name, value in options.items():
if value:
setattr(self, name, value)
return configured_location
logger.debug("No configuration found for location (%s).", location)
logger.verbose("No configuration found for %s.", location)
return location

def collect_backups(self, location):
Expand All @@ -376,9 +376,9 @@ def collect_backups(self, location):
match = TIMESTAMP_PATTERN.search(entry)
if match:
if self.exclude_list and any(fnmatch.fnmatch(entry, p) for p in self.exclude_list):
logger.debug("Excluded %r (it matched the exclude list).", entry)
logger.verbose("Excluded %r (it matched the exclude list).", entry)
elif self.include_list and not any(fnmatch.fnmatch(entry, p) for p in self.include_list):
logger.debug("Excluded %r (it didn't match the include list).", entry)
logger.verbose("Excluded %r (it didn't match the include list).", entry)
else:
backups.append(Backup(
pathname=os.path.join(location.directory, entry),
Expand Down
28 changes: 19 additions & 9 deletions rotate_backups/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# rotate-backups: Simple command line interface for backup rotation.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: April 13, 2016
# Last Change: July 9, 2016
# URL: https://github.com/xolox/python-rotate-backups

"""
Expand Down Expand Up @@ -123,11 +123,11 @@
-v, --verbose
Make more noise (increase logging verbosity).
Make more noise (increase logging verbosity). Can be repeated.
-q, --quiet
Make less noise (decrease logging verbosity).
Make less noise (decrease logging verbosity). Can be repeated.
-h, --help
Expand All @@ -136,13 +136,13 @@

# Standard library modules.
import getopt
import logging
import sys

# External dependencies.
import coloredlogs
from humanfriendly import concatenate, parse_path
from humanfriendly import concatenate, parse_path, pluralize
from humanfriendly.terminal import usage
from verboselogs import VerboseLogger

# Modules included in our package.
from rotate_backups import (
Expand All @@ -153,7 +153,7 @@
)

# Initialize a logger.
logger = logging.getLogger(__name__)
logger = VerboseLogger(__name__)


def main():
Expand Down Expand Up @@ -218,15 +218,25 @@ def main():
else:
assert False, "Unhandled option! (programming error)"
if rotation_scheme:
logger.debug("Parsed rotation scheme: %s", rotation_scheme)
logger.verbose("Rotation scheme defined on command line: %s", rotation_scheme)
if arguments:
# Rotation of the locations given on the command line.
selected_locations.extend(coerce_location(value, sudo=use_sudo) for value in arguments)
location_source = 'command line arguments'
else:
# Rotation of all configured locations.
selected_locations.extend(location for location, rotation_scheme, options in load_config_file(config_file))
# Show the usage message when no directories are given nor configured.
if not selected_locations:
location_source = 'configuration file'
# Inform the user which location(s) will be rotated.
if selected_locations:
logger.verbose("Selected %s based on %s:",
pluralize(len(selected_locations), "location"),
location_source)
for number, location in enumerate(selected_locations, start=1):
logger.verbose(" %i. %s", number, location)
else:
# Show the usage message when no directories are given nor configured.
logger.verbose("No location(s) to rotate selected.")
usage(__doc__)
return
except Exception as e:
Expand Down

0 comments on commit 6b28d4f

Please sign in to comment.