Skip to content

Commit 8baf1fe

Browse files
committed
Reformat all code with black
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent b4cd164 commit 8baf1fe

35 files changed

+1038
-646
lines changed

check_dns.py

+57-28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/python
22

3-
import site,os.path
3+
import site, os.path
4+
45
site.addsitedir(os.path.dirname(__file__))
56

67
import urllib2
78

89
import transaction
910

1011
from scriptspony.model import queue
11-
from scriptspony import mail,vhosts
12-
from scripts import hosts,keytab,log,auth
12+
from scriptspony import mail, vhosts
13+
from scripts import hosts, keytab, log, auth
14+
1315

1416
@log.exceptions
1517
def check_dns():
@@ -18,36 +20,57 @@ def check_dns():
1820

1921
# Use a list so all the ids are resolved early and transactions aren't
2022
# a problem
21-
for tid in [t.id for t in queue.Ticket.query.filter(queue.Ticket.state.in_([u'moira', u'dns'])).all()]:
23+
for tid in [
24+
t.id
25+
for t in queue.Ticket.query.filter(
26+
queue.Ticket.state.in_([u"moira", u"dns"])
27+
).all()
28+
]:
2229
t = queue.Ticket.get(tid)
23-
30+
2431
if hosts.points_at_scripts(t.hostname):
25-
path = '/mit/%s/web_scripts/%s' % (t.locker,
26-
vhosts.get_path(t.locker,t.hostname))
27-
wordpress = "This site looks like a WordPress blog; for the new URL to work properly, you'll need to access the WordPress admin interface via your old URL, go to General Settings, and change the WordPress address and Blog address to 'http://%s'." % t.hostname
28-
32+
path = "/mit/%s/web_scripts/%s" % (
33+
t.locker,
34+
vhosts.get_path(t.locker, t.hostname),
35+
)
36+
wordpress = (
37+
"This site looks like a WordPress blog; for the new URL to work properly, you'll need to access the WordPress admin interface via your old URL, go to General Settings, and change the WordPress address and Blog address to 'http://%s'."
38+
% t.hostname
39+
)
40+
2941
# Try to figure out what's up with the hostname currently
3042
try:
31-
page = urllib2.urlopen('http://%s/'%t.hostname)
43+
page = urllib2.urlopen("http://%s/" % t.hostname)
3244
content = page.read()
33-
if ('<meta name="generator" content="WordPress' in content
34-
or 'wp-login' in page.geturl()):
45+
if (
46+
'<meta name="generator" content="WordPress' in content
47+
or "wp-login" in page.geturl()
48+
):
3549
sitestatus = wordpress
3650
else:
3751
sitestatus = "Your site appears to be working properly. Have fun!"
3852
except urllib2.HTTPError as e:
39-
if 'wp-login' in e.geturl():
53+
if "wp-login" in e.geturl():
4054
sitestatus = wordpress
4155
elif e.code == 404:
42-
sitestatus = "There doesn't seem to be any content currently at %s; make sure that directory exists and has an index.html, index.cgi, or similar, or change this hostname to point somewhere else at http://pony.scripts.mit.edu." % path
56+
sitestatus = (
57+
"There doesn't seem to be any content currently at %s; make sure that directory exists and has an index.html, index.cgi, or similar, or change this hostname to point somewhere else at http://pony.scripts.mit.edu."
58+
% path
59+
)
4360
elif e.code == 403:
44-
sitestatus = "Visiting that page yields a Forbidden error; this is often caused by a lack of valid content at %s. Putting an index.html, index.cgi, or similar there may solve this. Alternately, you may just have your site password-protected or cert-protected." % path
61+
sitestatus = (
62+
"Visiting that page yields a Forbidden error; this is often caused by a lack of valid content at %s. Putting an index.html, index.cgi, or similar there may solve this. Alternately, you may just have your site password-protected or cert-protected."
63+
% path
64+
)
4565
elif e.code == 401:
4666
sitestatus = "Visiting that page yields an Unauthorized error. This generally means that you have your site password-protected or cert-protected, so we can't confirm whether it's working."
4767
else:
48-
sitestatus = "Visiting that page yields a %s error, suggesting a problem with the content at %s. Email us at [email protected] if you need help resolving this." % (e.code, path)
49-
50-
subject = u"Re: Request for hostname %s"%t.hostname
68+
sitestatus = (
69+
"Visiting that page yields a %s error, suggesting a problem with the content at %s. Email us at [email protected] if you need help resolving this."
70+
% (e.code, path)
71+
)
72+
73+
subject = u"Re: Request for hostname %s" % t.hostname
5174
body = u"""Hello,
5275
5376
Just wanted to let you know that the hostname %(hostname)s is now configured and working. It currently points to %(path)s. Visit http://%(hostname)s/ to check it out.
@@ -60,19 +83,25 @@ def check_dns():
6083
http://scripts.mit.edu/
6184
6285
/set status=resolved
63-
""" % dict(hostname=t.hostname,locker=t.locker,path=path,sitestatus=sitestatus)
64-
mail.send_correspondence(subject,body,
65-
rtid=t.rtid)
66-
t.addEvent(type=u'mail',state=u'resolved',by=u'dns',
67-
target=u'user',
68-
subject=subject,
69-
body=body)
86+
""" % dict(
87+
hostname=t.hostname, locker=t.locker, path=path, sitestatus=sitestatus
88+
)
89+
mail.send_correspondence(subject, body, rtid=t.rtid)
90+
t.addEvent(
91+
type=u"mail",
92+
state=u"resolved",
93+
by=u"dns",
94+
target=u"user",
95+
subject=subject,
96+
body=body,
97+
)
7098

7199
transaction.commit()
72100

73-
if __name__ == '__main__':
101+
102+
if __name__ == "__main__":
74103
auth.set_user_from_parent_process()
75104
from paste.deploy import loadapp
76-
loadapp('config:development.ini',
77-
relative_to=os.path.dirname(__file__))
105+
106+
loadapp("config:development.ini", relative_to=os.path.dirname(__file__))
78107
check_dns()

find_expiring_certs.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77
from scripts import cert
88
from scriptspony import vhosts
99

10+
1011
def main():
1112
now = datetime.utcnow()
1213

1314
res = vhosts.conn.search_s(
14-
'ou=VirtualHosts,dc=scripts,dc=mit,dc=edu',
15+
"ou=VirtualHosts,dc=scripts,dc=mit,dc=edu",
1516
ldap.SCOPE_ONELEVEL,
16-
'(&(objectClass=scriptsVhost)(scriptsVhostCertificate=*))',
17-
['scriptsVhostName', 'scriptsVhostAlias', 'uid', 'scriptsVhostCertificate'])
17+
"(&(objectClass=scriptsVhost)(scriptsVhostCertificate=*))",
18+
["scriptsVhostName", "scriptsVhostAlias", "uid", "scriptsVhostCertificate"],
19+
)
1820

1921
expiring = []
2022
for dn, attrs in res:
21-
vhost, = attrs['scriptsVhostName']
22-
aliases = attrs.get('scriptsVhostAlias', [])
23-
uid, = attrs['uid']
24-
scripts, = attrs['scriptsVhostCertificate']
23+
vhost, = attrs["scriptsVhostName"]
24+
aliases = attrs.get("scriptsVhostAlias", [])
25+
uid, = attrs["uid"]
26+
scripts, = attrs["scriptsVhostCertificate"]
2527
chain = cert.scripts_to_chain(scripts)
2628
expires = cert.chain_notAfter(chain) - now
2729
if expires < timedelta(days=14):
@@ -30,5 +32,6 @@ def main():
3032
for expires, uid, hostnames in expiring:
3133
print(expires, uid, *hostnames)
3234

33-
if __name__ == '__main__':
35+
36+
if __name__ == "__main__":
3437
main()

handle_cert_mail.py

+44-27
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,74 @@
66
from scripts import cert, log
77
from scriptspony import vhosts
88

9-
BLACKLIST = ['scripts.mit.edu', 'notfound.example.com']
9+
BLACKLIST = ["scripts.mit.edu", "notfound.example.com"]
10+
1011

1112
@log.exceptions
1213
def main():
1314
msg = sys.stdin.read()
1415
pem = cert.msg_to_pem(msg)
1516
if pem is None:
16-
log.info('handle_cert_mail.py: No certificate')
17+
log.info("handle_cert_mail.py: No certificate")
1718
return
1819
chain = cert.pem_to_chain(pem)
1920
names = cert.chain_subject_names(chain)
2021

2122
filter = ldap.filter.filter_format(
22-
'(&(objectClass=scriptsVhost)(|' +
23-
''.join('(scriptsVhostName=%s)' for name in names) +
24-
''.join('(scriptsVhostAlias=%s)' for name in names) +
25-
')' +
26-
''.join('(!(scriptsVhostName=%s))' for name in BLACKLIST) +
27-
')',
28-
list(names) +
29-
list(names) +
30-
BLACKLIST)
23+
"(&(objectClass=scriptsVhost)(|"
24+
+ "".join("(scriptsVhostName=%s)" for name in names)
25+
+ "".join("(scriptsVhostAlias=%s)" for name in names)
26+
+ ")"
27+
+ "".join("(!(scriptsVhostName=%s))" for name in BLACKLIST)
28+
+ ")",
29+
list(names) + list(names) + BLACKLIST,
30+
)
3131

3232
res = vhosts.conn.search_s(
33-
'ou=VirtualHosts,dc=scripts,dc=mit,dc=edu',
33+
"ou=VirtualHosts,dc=scripts,dc=mit,dc=edu",
3434
ldap.SCOPE_ONELEVEL,
3535
filter,
36-
['scriptsVhostName', 'scriptsVhostAccount', 'scriptsVhostCertificate'])
36+
["scriptsVhostName", "scriptsVhostAccount", "scriptsVhostCertificate"],
37+
)
3738
if res:
3839
for dn, attrs in res:
39-
vhost, = attrs['scriptsVhostName']
40-
account, = attrs['scriptsVhostAccount']
41-
if 'scriptsVhostCertificate' in attrs:
42-
old_scripts, = attrs['scriptsVhostCertificate']
40+
vhost, = attrs["scriptsVhostName"]
41+
account, = attrs["scriptsVhostAccount"]
42+
if "scriptsVhostCertificate" in attrs:
43+
old_scripts, = attrs["scriptsVhostCertificate"]
4344
old_chain = cert.scripts_to_chain(old_scripts)
4445
else:
4546
old_chain = None
4647
if cert.chain_should_install(chain, old_chain):
47-
log.info('handle_cert_mail.py: Installing certificate for %s on %s' % (vhost, account))
48+
log.info(
49+
"handle_cert_mail.py: Installing certificate for %s on %s"
50+
% (vhost, account)
51+
)
4852
vhosts.conn.modify_s(
4953
dn,
50-
[(ldap.MOD_REPLACE,
51-
'scriptsVhostCertificate',
52-
cert.chain_to_scripts(chain)),
53-
(ldap.MOD_REPLACE,
54-
'scriptsVhostCertificateKeyFile',
55-
'scripts-2048.key')])
54+
[
55+
(
56+
ldap.MOD_REPLACE,
57+
"scriptsVhostCertificate",
58+
cert.chain_to_scripts(chain),
59+
),
60+
(
61+
ldap.MOD_REPLACE,
62+
"scriptsVhostCertificateKeyFile",
63+
"scripts-2048.key",
64+
),
65+
],
66+
)
5667
else:
57-
log.info('handle_cert_mail.py: IGNORING certificate for %s on %s' % (vhost, account))
68+
log.info(
69+
"handle_cert_mail.py: IGNORING certificate for %s on %s"
70+
% (vhost, account)
71+
)
5872
else:
59-
log.error('handle_cert_mail.py: Certificate for %s matches no vhost' % list(names))
73+
log.error(
74+
"handle_cert_mail.py: Certificate for %s matches no vhost" % list(names)
75+
)
76+
6077

61-
if __name__ == '__main__':
78+
if __name__ == "__main__":
6279
main()

handle_mail.py

+41-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

3-
import site,os.path
3+
import site, os.path
4+
45
site.addsitedir(os.path.dirname(__file__))
56

67
import transaction
@@ -14,57 +15,73 @@
1415
from email.header import make_header, decode_header
1516
from email.utils import parseaddr
1617

18+
1719
@log.exceptions
1820
def handle_mail():
19-
21+
2022
message = email.message_from_file(sys.stdin)
2123

2224
if keytab.exists():
2325
keytab.auth()
2426

25-
if ('subject' not in message
26-
or 'delivered-to' not in message
27-
or 'from' not in message):
27+
if (
28+
"subject" not in message
29+
or "delivered-to" not in message
30+
or "from" not in message
31+
):
2832
return
2933

30-
ID_PATTERN = re.compile(r'pony\+(\d+)\@')
31-
toname, to = parseaddr(unicode(make_header(decode_header(message['delivered-to']))))
34+
ID_PATTERN = re.compile(r"pony\+(\d+)\@")
35+
toname, to = parseaddr(unicode(make_header(decode_header(message["delivered-to"]))))
3236
m = ID_PATTERN.search(to)
3337
if m is None:
3438
return
3539
id = int(m.group(1))
3640

37-
byname, by = parseaddr(unicode(make_header(decode_header(message['from']))))
41+
byname, by = parseaddr(unicode(make_header(decode_header(message["from"]))))
3842
by = by.lower()
39-
if by.endswith(u'@mit.edu'):
40-
by = by[:-len(u'@mit.edu')]
43+
if by.endswith(u"@mit.edu"):
44+
by = by[: -len(u"@mit.edu")]
4145

4246
t = queue.Ticket.get(id)
4347

44-
RTID_PATTERN = re.compile(r'\[help.mit.edu\s+\#(\d+)\]')
45-
subject = unicode(make_header(decode_header(message['subject'])))
48+
RTID_PATTERN = re.compile(r"\[help.mit.edu\s+\#(\d+)\]")
49+
subject = unicode(make_header(decode_header(message["subject"])))
4650
m = RTID_PATTERN.search(subject)
4751
if m:
4852
if t.rtid is None:
49-
by = u'rt'
53+
by = u"rt"
5054
t.rtid = int(m.group(1))
51-
55+
5256
newstate = t.state
5357
# TODO: blanche accounts-internal
54-
if by in (u'aswayze', u'bowser', u'jtravers', u'kwitt', u'mannys', u'mwollman', u'ovidio', u'thorn'):
55-
newstate = u'dns'
56-
body = u''
58+
if by in (
59+
u"aswayze",
60+
u"bowser",
61+
u"jtravers",
62+
u"kwitt",
63+
u"mannys",
64+
u"mwollman",
65+
u"ovidio",
66+
u"thorn",
67+
):
68+
newstate = u"dns"
69+
body = u""
5770
for part in message.walk():
58-
if (part.get_content_maintype() == 'text'):
59-
body += unicode(part.get_payload(decode=True), part.get_content_charset('us-ascii'))
60-
t.addEvent(type=u'mail',state=newstate,by=by,target=u'us',
61-
subject=subject, body=body)
71+
if part.get_content_maintype() == "text":
72+
body += unicode(
73+
part.get_payload(decode=True), part.get_content_charset("us-ascii")
74+
)
75+
t.addEvent(
76+
type=u"mail", state=newstate, by=by, target=u"us", subject=subject, body=body
77+
)
6278

6379
transaction.commit()
6480

65-
if __name__ == '__main__':
81+
82+
if __name__ == "__main__":
6683
auth.set_user_from_parent_process()
6784
from paste.deploy import loadapp
68-
loadapp('config:development.ini',
69-
relative_to=os.path.dirname(__file__))
85+
86+
loadapp("config:development.ini", relative_to=os.path.dirname(__file__))
7087
handle_mail()

0 commit comments

Comments
 (0)