3
# Copyright 2009 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6
# pylint: disable-msg=C0103,W0403
8
"""Cronscript to prune old and unreferenced OOPS reports from the archive."""
15
from canonical.config import config
16
from canonical.database.sqlbase import ISOLATION_LEVEL_AUTOCOMMIT
17
from lp.services.scripts.base import (
18
LaunchpadCronScript, LaunchpadScriptFailure)
19
from canonical.launchpad.scripts.oops import (
20
unwanted_oops_files, prune_empty_oops_directories)
23
default_lock_filename = '/var/lock/oops-prune.lock'
25
class OOPSPruner(LaunchpadCronScript):
26
def add_my_options(self):
27
self.parser.add_option(
28
'-n', '--dry-run', default=False, action='store_true',
29
dest="dry_run", help="Do a test run. No files are removed."
33
# Default to using the OOPS directory in config file.
35
self.args = [config.error_reports.error_dir]
38
for oops_dir in self.args:
39
if not os.path.isdir(oops_dir):
40
raise LaunchpadScriptFailure(
41
"%s is not a directory" % oops_dir)
43
oops_directories.append(oops_dir)
45
for oops_directory in oops_directories:
46
for oops_path in unwanted_oops_files(oops_directory,
48
self.logger.info("Removing %s", oops_path)
49
if not self.options.dry_run:
52
prune_empty_oops_directories(oops_directory)
55
if __name__ == '__main__':
56
script = OOPSPruner('oops-prune', dbuser='oopsprune')
57
script.lock_and_run(isolation=ISOLATION_LEVEL_AUTOCOMMIT)