reset the ledger after the dry run to the latest git version
[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 today = str(datetime.date.today())
23
24 with open('ledger', 'a') as f:
25     f.write("\n")
26     f.write(render.render_template('templates/ledger', date))
27
28 if not dry_run:
29     subprocess.check_call(["git", "commit", "ledger",
30                            "-m", "Update for %s" % (date,)])
31
32 debts = render.get_debts()
33 punt = []
34
35 with open('ledger', 'a') as f:
36     f.write("\n")
37     for (user, debt) in debts:
38         if debt < 30: continue
39         punt.append(user)
40         f.write("""\
41 %(today)s Punt
42   Pool:Owed:%(user)s  $-%(debt)s
43   User:%(user)s
44 """ % {'user': user, 'debt': debt, 'today': today})
45
46
47 if not dry_run:
48     text = render.render_template('templates/week.tmpl', date, punt=punt)
49
50     lines = text.split("\n")
51     title = lines[0]
52     body  = "\n".join(lines[1:])
53
54     page = dict(title = title, description = body)
55
56     try:
57         subprocess.call(['stty', '-echo'])
58         passwd = raw_input("Password for %s: " % (USER,))
59         print
60     finally:
61         subprocess.call(['stty', 'echo'])
62
63     x = xmlrpclib.ServerProxy(XMLRPC_ENDPOINT)
64     x.metaWeblog.newPost(BLOG_ID, USER, passwd, page, True)
65
66 email = render.render_template('templates/email.txt', date, punt=punt)
67
68 if dry_run:
69     print email
70 else:
71     p = subprocess.Popen(['mutt', '-H', '/dev/stdin'],
72                          stdin=subprocess.PIPE)
73     p.communicate(email)
74
75 if punt:
76     with open('bloggers.yml') as b:
77         bloggers = yaml.safe_load(b)
78     for p in punt:
79         if 'end' not in bloggers[p]:
80             bloggers[p]['end'] = today
81     with open('bloggers.yml','w') as b:
82         yaml.safe_dump(bloggers, b)
83
84     subprocess.check_call(["git", "commit", "ledger", "bloggers.yml",
85                            "-m", "Punts for %s" % (today,)])
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"])

Benjamin Mako Hill || Want to submit a patch?