7a44dffe36cd2115147348f4ef159d177b51e3ec
[iron-blogger] / weekly-update.py
1 #!/usr/bin/python
2 import render
3 import os
4 import sys
5 import xmlrpclib
6 import subprocess
7 import datetime
8 import yaml
9
10 XMLRPC_ENDPOINT = 'http://iron-blogger.mako.cc/xmlrpc.php'
11 USER            = 'mako'
12 BLOG_ID         = 1
13
14 dry_run = False
15
16 args = sys.argv[1:]
17 if args[0] == '-n':
18     dry_run = True
19     args = args[1:]
20
21 date = args[0]
22
23 with open('ledger', 'a') as f:
24     f.write("\n")
25     f.write(render.render_template('templates/ledger', date))
26
27 if not dry_run:
28     subprocess.check_call(["git", "commit", "ledger",
29                            "-m", "Update for %s" % (date,)])
30
31 debts = render.get_debts()
32 punt = []
33
34 with open('ledger', 'a') as f:
35     f.write("\n")
36     for (user, debt) in debts:
37         if debt < 30: continue
38         punt.append(user)
39         f.write("""\
40 %(date)s Punt
41   Pool:Owed:%(user)s  $-%(debt)s
42   User:%(user)s
43 """ % {'user': user, 'debt': debt, 'date': date})
44
45
46 if not dry_run:
47     text = render.render_template('templates/week.tmpl', date, punt=punt)
48
49     lines = text.split("\n")
50     title = lines[0]
51     body  = "\n".join(lines[1:])
52
53     page = dict(title = title, description = body)
54
55     try:
56         subprocess.call(['stty', '-echo'])
57         passwd = raw_input("Password for %s: " % (USER,))
58         print
59     finally:
60         subprocess.call(['stty', 'echo'])
61
62     x = xmlrpclib.ServerProxy(XMLRPC_ENDPOINT)
63     x.metaWeblog.newPost(BLOG_ID, USER, passwd, page, True)
64
65 email = render.render_template('templates/email.txt', date, punt=punt)
66
67 if dry_run:
68     print email
69 else:
70     p = subprocess.Popen(['mutt', '-H', '/dev/stdin'],
71                          stdin=subprocess.PIPE)
72     p.communicate(email)
73
74 if punt:
75     with open('bloggers.yml') as b:
76         bloggers = yaml.safe_load(b)
77     for p in punt:
78         if 'end' not in bloggers[p]:
79             bloggers[p]['end'] = date
80     with open('bloggers.yml','w') as b:
81         yaml.safe_dump(bloggers, b)
82
83     if not dry_run:
84         subprocess.check_call(["git", "commit", "ledger", "bloggers.yml",
85                                "-m", "Punts for %s" % (date,)])
86
87 # if it's a dry run, lets set the ledger back to the beginning state
88 if dry_run:
89     subprocess.check_call(["git", "checkout", "ledger"])
90     
91     if punt:
92         subprocess.check_call(["git", "checkout", "bloggers.yml"])

Benjamin Mako Hill || Want to submit a patch?