~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to cronscripts/oops-prune.py

  • Committer: Curtis Hovey
  • Date: 2011-08-21 14:21:06 UTC
  • mto: This revision was merged to the branch mainline in revision 13745.
  • Revision ID: curtis.hovey@canonical.com-20110821142106-x93hajd6iguma8gx
Update test that was enforcing bad grammar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -S
 
2
#
 
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
5
 
 
6
# pylint: disable-msg=C0103,W0403
 
7
 
 
8
"""Cronscript to prune old and unreferenced OOPS reports from the archive."""
 
9
 
 
10
__metaclass__ = type
 
11
 
 
12
import _pythonpath
 
13
import os
 
14
 
 
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)
 
21
 
 
22
 
 
23
default_lock_filename = '/var/lock/oops-prune.lock'
 
24
 
 
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."
 
30
                )
 
31
 
 
32
    def main(self):
 
33
        # Default to using the OOPS directory in config file.
 
34
        if not self.args:
 
35
            self.args = [config.error_reports.error_dir]
 
36
 
 
37
        oops_directories = []
 
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)
 
42
 
 
43
            oops_directories.append(oops_dir)
 
44
 
 
45
        for oops_directory in oops_directories:
 
46
            for oops_path in unwanted_oops_files(oops_directory,
 
47
                                                 40, self.logger):
 
48
                self.logger.info("Removing %s", oops_path)
 
49
                if not self.options.dry_run:
 
50
                    os.unlink(oops_path)
 
51
 
 
52
            prune_empty_oops_directories(oops_directory)
 
53
 
 
54
 
 
55
if __name__ == '__main__':
 
56
    script = OOPSPruner('oops-prune', dbuser='oopsprune')
 
57
    script.lock_and_run(isolation=ISOLATION_LEVEL_AUTOCOMMIT)