~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to cronscripts/check-teamparticipation.py

  • Committer: Gavin Panella
  • Date: 2011-11-03 21:48:28 UTC
  • mto: This revision was merged to the branch mainline in revision 14260.
  • Revision ID: gavin.panella@canonical.com-20111103214828-mal0sao9pdo11its
Enable loading and saving of team participation info. Work in progress.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
situation, but that's not a simple thing and this should do for now.
19
19
"""
20
20
 
 
21
import bz2
 
22
 
21
23
import _pythonpath
 
24
import cPickle as pickle
22
25
 
23
 
from lp.registry.scripts.teamparticipation import check_teamparticipation
 
26
from lp.registry.scripts.teamparticipation import (
 
27
    check_teamparticipation_circular,
 
28
    check_teamparticipation_consistency,
 
29
    check_teamparticipation_self,
 
30
    fetch_team_participation_info,
 
31
    )
24
32
from lp.services.scripts.base import LaunchpadScript
25
33
 
26
34
 
 
35
def save(obj, filename):
 
36
    """Save a bz2 compressed pickle of `obj` to `filename`."""
 
37
    fout = bz2.BZ2File(filename, "w")
 
38
    try:
 
39
        pickle.dump(obj, fout, pickle.HIGHEST_PROTOCOL)
 
40
    finally:
 
41
        fout.close()
 
42
 
 
43
 
 
44
def load(filename):
 
45
    """Load and return a bz2 compressed pickle from `filename`."""
 
46
    fin = bz2.BZ2File(filename, "r")
 
47
    try:
 
48
        return pickle.load(fin)
 
49
    finally:
 
50
        fin.close()
 
51
 
 
52
 
27
53
class CheckTeamParticipationScript(LaunchpadScript):
28
54
    description = "Check for invalid/missing TeamParticipation entries."
29
55
 
 
56
    def add_my_options(self):
 
57
        self.parser.add_option(
 
58
            "--load-participation-info",
 
59
            dest="load_participation_info", metavar="FILE",
 
60
            help=(
 
61
                "File from which to load participation information "
 
62
                "instead of going to the database."))
 
63
        self.parser.add_option(
 
64
            "--save-participation-info",
 
65
            dest="save_participation_info", metavar="FILE",
 
66
            help=(
 
67
                "File in which to save participation information, for "
 
68
                "later processing with --load-participation-info."))
 
69
 
30
70
    def main(self):
31
 
        check_teamparticipation(self.logger)
 
71
        """Perform various checks on the `TeamParticipation` table."""
 
72
        if self.options.load_participation_info:
 
73
            participation_info = load(self.options.load_participation_info)
 
74
        else:
 
75
            participation_info = fetch_team_participation_info(self.logger)
 
76
 
 
77
        check_teamparticipation_self(self.logger)
 
78
        check_teamparticipation_circular(self.logger)
 
79
        check_teamparticipation_consistency(self.logger, participation_info)
 
80
 
 
81
        if self.options.save_participation_info:
 
82
            save(participation_info, self.options.save_participation_info)
32
83
 
33
84
 
34
85
if __name__ == '__main__':