~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to cronscripts/check-teamparticipation.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-19 04:01:37 UTC
  • mfrom: (13970.7.23 port-to-LaunchpadScript)
  • Revision ID: launchpad@pqm.canonical.com-20110919040137-sr8154o9tfptnqir
[r=sinzui][no-qa] Port a dozen scripts to LaunchpadScript,
        removing their direct initZopeless dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
import _pythonpath
22
22
 
23
 
import optparse
24
 
import sys
 
23
import transaction
25
24
 
26
25
from canonical.database.sqlbase import cursor
27
 
from canonical.launchpad.scripts import (
28
 
    execute_zcml_for_scripts, logger_options, logger)
29
 
from canonical.lp import initZopeless
30
 
 
31
 
 
32
 
if __name__ == '__main__':
33
 
    parser = optparse.OptionParser(
34
 
        description="Check for invalid/missing TeamParticipation entries.")
35
 
    logger_options(parser)
36
 
    options, args = parser.parse_args(sys.argv[1:])
37
 
    log = logger(options, 'check-teamparticipation')
38
 
 
39
 
    execute_zcml_for_scripts()
40
 
    ztm = initZopeless()
41
 
 
 
26
from lp.services.scripts.base import LaunchpadScript, LaunchpadScriptFailure
 
27
 
 
28
 
 
29
def check_teamparticipation(log):
42
30
    # Check self-participation.
43
31
    query = """
44
32
        SELECT id, name
46
34
            SELECT person FROM Teamparticipation WHERE person = team
47
35
            ) AND merged IS NULL
48
36
        """
49
 
    ztm.begin()
50
37
    cur = cursor()
51
38
    cur.execute(query)
52
39
    non_self_participants = cur.fetchall()
64
51
        """)
65
52
    circular_references = cur.fetchall()
66
53
    if len(circular_references) > 0:
67
 
        log.warn("Circular references found: %s" % circular_references)
68
 
        sys.exit(1)
 
54
        raise LaunchpadScriptFailure(
 
55
            "Circular references found: %s" % circular_references)
69
56
 
70
57
    # Check if there are any missing/spurious TeamParticipation entries.
71
58
    cur.execute("SELECT id FROM Person WHERE teamowner IS NOT NULL")
72
59
    team_ids = cur.fetchall()
73
 
    ztm.abort()
 
60
    transaction.abort()
74
61
 
75
62
    def get_participants(team):
76
63
        """Recurse through the team's members to get all its participants."""
86
73
    team_ids = team_ids[50:]
87
74
    while batch:
88
75
        for [id] in batch:
89
 
            ztm.begin()
90
76
            team = Person.get(id)
91
77
            expected = get_participants(team)
92
78
            found = set(team.allmembers)
102
88
                                   for person in reverse_difference)
103
89
                log.warn("%s (%s): spurious TeamParticipation entries for %s."
104
90
                         % (team.name, team.id, people))
105
 
            ztm.abort()
 
91
            transaction.abort()
106
92
        batch = team_ids[:50]
107
93
        team_ids = team_ids[50:]
 
94
 
 
95
 
 
96
class CheckTeamParticipationScript(LaunchpadScript):
 
97
    description = "Check for invalid/missing TeamParticipation entries."
 
98
 
 
99
    def main(self):
 
100
        check_teamparticipation(self.logger)
 
101
 
 
102
if __name__ == '__main__':
 
103
    CheckTeamParticipationScript("check-teamparticipation").run()