1
#!/usr/bin/env python
2
3
# usage: pep-hook.py $REPOS $REV
4
# (standard post-commit args)
5
6
import os, glob, time, datetime, stat, re, sys
7
import codecs
8
import PyRSS2Gen as rssgen
9
10
RSS_PATH = os.path.join(sys.argv[1], 'peps.rss')
11
12
def firstline_startingwith(full_path, text):
13
    for line in codecs.open(full_path, encoding="utf-8"):
14
        if line.startswith(text):
15
            return line[len(text):].strip()
16
    return None
17
18
# get list of peps with creation time (from "Created:" string in pep .txt)
19
peps = glob.glob('pep-*.txt')
20
def pep_creation_dt(full_path):
21
    created_str = firstline_startingwith(full_path, 'Created:')
22
    # bleh, I was hoping to avoid re but some PEPs editorialize
23
    # on the Created line
24
    m = re.search(r'''(\d+-\w+-\d{4})''', created_str)
25
    if not m:
26
        # some older ones have an empty line, that's okay, if it's old
27
        # we ipso facto don't care about it.
28
        # "return None" would make the most sense but datetime objects
29
        # refuse to compare with that. :-|
30
        return datetime.datetime(*time.localtime(0)[:6])
31
    created_str = m.group(1)
32
    try:
33
        t = time.strptime(created_str, '%d-%b-%Y')
34
    except ValueError:
35
        t = time.strptime(created_str, '%d-%B-%Y')
36
    return datetime.datetime(*t[:6])
37
peps_with_dt = [(pep_creation_dt(full_path), full_path) for full_path in peps]
38
# sort peps by date, newest first
39
peps_with_dt.sort(reverse=True)
40
41
# generate rss items for 10 most recent peps
42
items = []
43
for dt, full_path in peps_with_dt[:10]:
44
    try:
45
        n = int(full_path.split('-')[-1].split('.')[0])
46
    except ValueError:
47
        pass
48
    title = firstline_startingwith(full_path, 'Title:')
49
    author = firstline_startingwith(full_path, 'Author:')
50
    url = 'http://www.python.org/dev/peps/pep-%0.4d' % n
51
    item = rssgen.RSSItem(
52
        title = 'PEP %d: %s' % (n, title),
53
        link = url,
54
        description = 'Author: %s' % author,
55
        guid = rssgen.Guid(url),
56
        pubDate = dt)
57
    items.append(item)
58
59
# the rss envelope
60
desc = """
61
Newest Python Enhancement Proposals (PEPs) - Information on new
62
language features, and some meta-information like release
63
procedure and schedules
64
""".strip()
65
rss = rssgen.RSS2(
66
    title = 'Newest Python PEPs',
67
    link = 'http://www.python.org/dev/peps',
68
    description = desc,
69
    lastBuildDate = datetime.datetime.now(),
70
    items = items)
71
72
file(RSS_PATH, 'w').write(rss.to_xml())