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

Benjamin Mako Hill || Want to submit a patch?