~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
8452.3.3 by Karl Fogel
* utilities/: Add copyright header block to source files that were
2
#
8687.15.2 by Karl Fogel
In files modified by r8688, change "<YEARS>" to "2009", as per
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
8687.15.3 by Karl Fogel
Shorten the copyright header block to two lines.
4
# GNU Affero General Public License version 3 (see the file LICENSE).
8452.3.3 by Karl Fogel
* utilities/: Add copyright header block to source files that were
5
3691.315.1 by kiko
Add a script to list all references to a certain Person tuple, script originally from Salgado.
6
import sys
7
from canonical.lp import initZopeless
8
from canonical.database import postgresql
9
from canonical.database.sqlbase import cursor
10
from canonical.launchpad.database import Person, TeamParticipation
11
12
person_handle = sys.argv[1]
13
txn = initZopeless()
14
try:
15
    int(person_handle)
16
except ValueError:
17
    if "@" in person_handle:
18
        person = Person.selectOne("EmailAddress.person = Person.id AND "
19
                               "emailaddress.email = %s" % person_handle)
20
    else:
21
        person = Person.selectOneBy(name=person_handle)
22
else:
23
    person = Person.selectOneBy(id=person_handle)
24
25
if person is None:
26
    print "Person %s not found" % person_handle
27
    sys.exit(1)
28
29
30
skip = []
31
cur = cursor()
32
references = list(postgresql.listReferences(cur, 'person', 'id'))
33
34
print ("Listing references for %s (ID %s, preferred email %s):\n" %
35
       (person.name, person.id,
36
        person.preferredemail and person.preferredemail.email))
37
for src_tab, src_col, ref_tab, ref_col, updact, delact in references:
38
    if (src_tab, src_col) in skip:
39
        continue
40
    query = "SELECT id FROM %s WHERE %s=%s" % (src_tab, src_col, person.id)
41
    cur.execute(query)
42
    rows = cur.fetchall()
43
    for row in rows:
44
        if src_tab.lower() == 'teamparticipation':
45
            tp = TeamParticipation.selectOneBy(
46
                personID=person.id, teamID=person.id)
47
            if tp.id == row[0]:
48
                # Every person has a teamparticipation entry for itself,
49
                # and we already know this. No need to output it, then.
50
                continue
51
        print ("\tColumn %s of table %s with id %s points to this "
52
               "person." % (src_col, src_tab, row[0]))
53
54
print
55