]> piware.de Git - bin.git/blobdiff - workitems.py
workitems: fix parsing of last line
[bin.git] / workitems.py
index b9ee19524cbeee4b42e9f6b4917b69115b86c7ac..f28db44153602ee3079ab5464d6bdd089904ee35 100755 (executable)
@@ -5,7 +5,7 @@ import sqlite3 as dbapi2
 
 blueprints_base_url = 'https://blueprints.launchpad.net'
 
-valid_states = set(['todo', 'done', 'postponed'])
+valid_states = ['todo', 'done', 'postponed']
 
 def get_db(dbpath):
     '''Open/initialize database.
@@ -41,10 +41,15 @@ def parse_argv():
         help='Regex pattern for blueprint name', dest='pattern')
     optparser.add_option('-D', '--dump', action='store_true', default=False,
         help='Dump database', dest='dump')
+    optparser.add_option('-m', '--moin', metavar='URL',
+        help='moin URL for additional work items (can be given multiple times)', 
+        action='append', dest='moin', default=[])
     optparser.add_option('-t', '--text', action='store_true', default=False,
         help='Print work item summary in text format', dest='text')
     optparser.add_option('-c', '--csv', action='store_true', default=False,
-        help='Print work item summary in text format', dest='csv')
+        help='Print work item summary in CSV format', dest='csv')
+    optparser.add_option('-H', '--html', action='store_true', default=False,
+        help='Generate work item HTML report', dest='html')
     optparser.add_option('--from', metavar='YYYY-MM-DD',
         help='Generate CSV data from this day on', dest='from_date')
     optparser.add_option('--to', metavar='YYYY-MM-DD',
@@ -54,7 +59,7 @@ def parse_argv():
 
     if not opts.database:
         optparser.error('No database given')
-    if not opts.dump and not opts.text and not opts.csv:
+    if not opts.dump and not opts.text and not opts.csv and not opts.html:
         if not opts.release:
             optparser.error('No release given')
         if not opts.pattern:
@@ -76,7 +81,7 @@ def get_blueprints(url, name_pattern):
 
     return result
 
-def get_workitems(blueprint_url):
+def get_blueprint_workitems(blueprint_url):
     '''Collect work items from a particular blueprint URL.
 
     This will return a list of ('item', 'status') pairs.
@@ -86,15 +91,18 @@ def get_workitems(blueprint_url):
     found_workitems = False
     result = []
     for l in urllib.urlopen(blueprint_url):
+        end = False
         if not found_workitems:
             if work_items_re.search(l):
                 found_workitems = True
             continue
 
-        l = l.replace('<br />', '').replace('</div>', '').strip()
+        if '</p>' in l:
+            end = True
+        l = l.replace('<br />', '').replace('</div>', '').replace('</p>', '').strip()
 
         # ends with empty line
-        if l.endswith('</p>') or not l:
+        if end or not l:
             break
 
         try:
@@ -106,6 +114,10 @@ def get_workitems(blueprint_url):
         state = state.strip().lower()
         if not state:
             state = 'todo'
+        if state == 'completed':
+            state = 'done'
+        if state == 'inprogress':
+            state = 'todo'
         if state not in valid_states:
             print >> sys.stderr, 'ERROR: invalid state "%s" for work item "%s"' % (
                 state, desc)
@@ -114,6 +126,31 @@ def get_workitems(blueprint_url):
 
     return result
 
+def get_moin_workitems(url):
+    '''Collect work items from a moin wiki URL.
+
+    Every line starting with "|| " is treated as a work item.
+
+    Return a list of ('item', 'status') pairs.
+    '''
+    result = []
+    for line in urllib.urlopen(url):
+        if line.startswith('|| '):
+            fields = line.strip().split('||')
+            assert not fields[0] # should be empty
+            desc = fields[1].strip()
+            for f in fields[2:]:
+                if 'DONE' in f:
+                    result.append((desc, 'done'))
+                    break
+                elif 'POSTPONED' in f:
+                    result.append((desc, 'done'))
+                    break
+            else:
+                result.append((desc, 'todo'))
+
+    return result
+
 def dump(db):
     '''Dump database contents.'''
 
@@ -132,18 +169,20 @@ def add_work_item(db, blueprint, item, status):
 def import_lp(db, name_pattern, release):
     '''Collect blueprint work items from Launchpad into DB.'''
 
-    blueprints = get_blueprints('%s//ubuntu/%s/+specs' % (blueprints_base_url,
+    blueprints = get_blueprints('%s//ubuntu/%s/+specs?batch=300' % (blueprints_base_url,
         opts.release), name_pattern)
 
+    cur = db.cursor()
+    cur.execute('DELETE FROM work_items WHERE date = date(CURRENT_TIMESTAMP)')
+
     for bp in blueprints:
         #print 'Checking', bp
         bpname = bp.split('/')[-1]
-        work_items = get_workitems(bp)
+        work_items = get_blueprint_workitems(bp)
         if not work_items:
             print >> sys.stderr, 'WARNING: %s has no work items' % bpname
         for (item, status) in work_items:
             add_work_item(db, bpname, item, status)
-    db.commit()
 
 def workitems_over_time(db):
     '''Calculate work item development over time.
@@ -227,6 +266,61 @@ def csv(db, from_date, to_date):
                 entry.get('postponed', 0))
         d += datetime.timedelta(days=1)
 
+def html(db):
+    '''Print work item status as HTML.'''
+
+    print '''<html>
+<head>
+  <title>Work item status</title>
+  <style type="text/css">
+    body { background: #CCCCB0; color: black; }
+    a { text-decoration: none; }
+    table { border-collapse: collapse; border-style: solid none; 
+            border-width: 3px; margin-bottom: 3ex; empty-cells: show; }
+    table th { text-align: left; border-style: none none solid none; 
+               border-width: 3px; padding-right: 10px; }
+    table td { text-align: left; border-style: none none dotted none; 
+               border-width: 1px; padding-right: 10px; }
+
+    a { color: blue; }
+  </style>
+</head>
+
+<body>
+
+<h1>History</h1>
+<p><img src="burndown.png" alt="burndown" /></p>
+
+<h1>Status by blueprint</h1>
+<table>
+  <tr><th>Blueprint</th> <th>todo/postponed/done</th> <th>Completion</th></tr>
+'''
+
+    data = blueprint_status(db)
+
+    completion = []
+    for (bp, (todo, done, postponed)) in data.iteritems():
+        completion.append((bp,
+            int(float(postponed+done)/(todo+done+postponed)*100 + 0.5)))
+
+    completion.sort(key=lambda k: k[1], reverse=True)
+
+    for (bp, percent) in completion:
+        print '  <tr><td><a href="%s/ubuntu/+spec/%s">%s</a></td> <td>%i/%i/%i</td> <td>%i%%</td></tr>' % (
+                blueprints_base_url, bp, bp, data[bp][0], data[bp][2],
+                data[bp][1], percent)
+
+    print '</table>'
+
+    print '</body></html>'
+
+def import_moin(db, urls):
+    '''Collect blueprint work items from a moin wiki.'''
+
+    for url in urls:
+        for (d, s) in get_moin_workitems(url):
+            add_work_item(db, url, d, s)
+
 #
 # main
 #
@@ -239,8 +333,12 @@ if opts.dump:
     dump(db)
 elif opts.text:
     text(db)
+elif opts.html:
+    html(db)
 elif opts.csv:
     csv(db, opts.from_date, opts.to_date)
 else:
     import_lp(db, opts.pattern, opts.release)
+    import_moin(db, opts.moin)
+    db.commit()