~yasumoto7/five-a-day/icon_countdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python

# Copyright 2008 (C) Canonical Ltd.

import textwrap
import time
import sys
import os

def checkout_branch(branch_dir, remote_branch):
    err = os.system("bzr checkout %s %s" % (remote_branch, branch_dir))
    return err #return 0 if 'bzr checkout' was successfull or the bzr-error otherwise

def update_branch(branch_dir, remote_branch):
    if not os.path.exists(branch_dir):
        err = checkout_branch(branch_dir, remote_branch)
        if err: return err #if unable to checkout the branch return bzr-error
    cwd = os.getcwd()
    err = os.system("cd %s; bzr update" % (branch_dir))
    os.system("cd %s" % cwd) #always cd back to working-dir, this should not fail
    return err #return 0 if 'bzr update' was successfull or the bzr-error otherwise


def main():
    username = ""
    lpbugs = False
    try:
        import launchpadbugs.connector as Connector
        Bug = Connector.ConnectBug(method="Text")
        lpbugs = True
    except:
        print "Warning: Please install  python-launchpad-bugs  - it will add more information about the bug to your signature."

    if not os.path.exists(os.path.expanduser("~/.5-a-day-data/%s" % username)):
        print >> sys.stderr, "You need to use  add-5-a-day  first to add a few bug reports to your list."
        sys.exit(1)

    if not os.path.exists(os.path.expanduser("~/.5-a-day")):
        print >> sys.stderr, """Please type in:
        echo <Your Launchpad ID> > ~/.5-a-day"""
        sys.exit(1)
    else:
        username = open(os.path.expanduser("~/.5-a-day")).read().strip()

    branch = "bzr+ssh://%s@bazaar.launchpad.net/~5-a-day/5-a-day-data/main" % username
    update_branch(os.path.expanduser("~/.5-a-day-data"), branch)

    sig = os.path.expanduser("~/.signature")
    oldsig = os.path.expanduser("~/.signature.old")
    if os.path.exists(sig):
        if not open(sig).read().count("https://wiki.ubuntu.com/5-A-Day") and \
           not os.path.exists(os.path.expanduser(oldsig)):
            os.system("mv %s %s" % (sig, oldsig))
        else:
            os.remove(sig)
        
    date = time.localtime(time.time())
    date_str = "%s-%s-%s" % (date[0], date[1], date[2])

    lines = open(os.path.expanduser("~/.5-a-day-data/%s" % username)).readlines()
    lines = set(filter(lambda a: a.startswith(date_str), lines))
    if len(lines)>5:
        lines = list(lines)[-5:]
    text = "My 5 today: "
    html = """<font color='lightslategray'>-- <br />My 5 today: """
    for line in lines:
        bugnr = line.split()[1]
        if lpbugs:
            affects = set(map(lambda a: a.affects.longname.lower(), 
                              Bug(int(bugnr)).infotable))
            affects = filter(lambda a: a.count("ubuntu"), affects)
            affects = map(lambda a: a.replace(" (ubuntu)", ""), affects)
            if affects:
                text += "#%s (%s), " % (bugnr, str(", ".join(affects)))
                html += "#<a href='https://launchpad.net/bugs/%s'>%s</a> (%s), " % (bugnr, bugnr, str(", ".join(affects)))
            else:
                text += "#%s, " % (bugnr)
                html += "#<a href='https://launchpad.net/bugs/%s'>%s</a>, " % (bugnr, bugnr)
        else:
            text += "#%s, " % (bugnr)
            html += "#<a href='https://launchpad.net/bugs/%s'>%s</a>, " % (bugnr, bugnr)
    text = "\n".join(textwrap.wrap(text[:-2], 75))
    html = html[:-2]
    text += """
    Do 5 a day - every day! https://wiki.ubuntu.com/5-A-Day\n"""
    html += """<br />Do 5 a day - every day! <a href='https://wiki.ubuntu.com/5-A-Day'>https://wiki.ubuntu.com/5-A-Day</a><br /></font>"""

    f = open(sig, "a")
    if os.path.exists(oldsig):
        f.write(open(oldsig).read())
        f.write("\n")
    f.write(text)
    f.close()

    if len(sys.argv)>1 and sys.argv[1] == "--html":
        print html

if __name__ == "__main__":
    sys.exit(main())