Fixed quoting for subproc
[invirt/scripts/update-contacts.git] / update-contacts
1 #!/usr/bin/python
2
3 """Update the xvm-contacts list.
4
5 This script finds all e-mail addresses currently listed as contacts
6 for a VM and updates the xvm-contacts list to match, adding or
7 removing as necessary.
8 """
9
10 import socket
11 import subprocess
12 import datetime
13
14 from invirt import database
15 from invirt import remctl
16
17 def getContacts():
18     contacts = set()
19
20     for m in database.Machine.query():
21         if '@' in m.contact:
22             contacts.add(m.contact.lower())
23         else:
24             contacts.add(m.contact.lower() + '@mit.edu')
25
26     return sorted(contacts)
27
28 def stripDomain(c):
29     if c.endswith('@mit.edu'):
30         return c[:-8]
31     else:
32         return c
33
34 def updateContacts(contacts):
35     now = datetime.datetime.now()
36     p = subprocess.Popen(['blanche', '-f', '-', '-D', 'All contacts for XVM Machines (%02d/%2d/%2d)' % (now.month, now.day, now.year), 'xvm-contacts'],
37                          stdin=subprocess.PIPE,
38                          )
39
40     p.communicate('\n'.join(stripDomain(c) for c in contacts))
41
42 def main():
43     database.connect()
44     subprocess.call(['kinit', '-k'])
45
46     updateContacts(getContacts())
47
48 if __name__ == '__main__':
49     main()