~elopio/ubuntuone-launchpad-scripts/count-tags

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
from __future__ import with_statement

import sys

from BeautifulSoup import BeautifulSoup
from ClientCookie import Cookie
from ClientCookie import CookieJar
from launchpadlib.credentials import Credentials
from launchpadlib.launchpad import EDGE_SERVICE_ROOT
from launchpadlib.launchpad import Launchpad
from launchpadlib.launchpad import STAGING_SERVICE_ROOT
from mechanize import Browser
from mechanize import FormNotFoundError
import os
from urlparse import urljoin
try:
    from xml.etree import ElementTree
except ImportError:
    from elementtree import ElementTree
import gdata.spreadsheet.service
import gdata.service
import atom.service
import gdata.spreadsheet
import atom
import gdata.spreadsheet.service

SERVICE_ROOT = EDGE_SERVICE_ROOT
#SERVICE_ROOT = STAGING_SERVICE_ROOT

home = os.path.expanduser("~")
cachedir = os.path.join(home, ".launchpadlib/cache/")
credentialfile = os.path.join(home, ".launchpadlib/credentials")

if os.path.exists(credentialfile):
    credentials = Credentials()
    with file(credentialfile) as f:
        credentials.load(f)
    launchpad = Launchpad(credentials, SERVICE_ROOT)
else:
    launchpad = Launchpad.get_token_and_login('joshuahoover-taskstatus', SERVICE_ROOT, cachedir)
    launchpad.credentials.save(file(credentialfile, "w"))

me = launchpad.me
print "Hello %s, lets get started." % me.name

# This means that we search across the whole ubuntuone project
# suite, including all sub projects.
ubunet = launchpad.projects['ubuntuone']
counter = 1

TEST_SPREADSHEET_KEY = 'riXzoej2Qo9VVokUoXpKxaA'
WHOS_WORKING_ON_WHAT_KEY = 'p-Ix7K2HxhBAVFobBY53YQg'
SPREADSHEET_KEY = WHOS_WORKING_ON_WHAT_KEY

def authenticate_to_google_spreadsheet():
    """Get an authenticated  google data client object."""
    gd_client = gdata.spreadsheet.service.SpreadsheetsService()
    # A multi session token
    gd_client.SetAuthSubToken('CN2z5oKlCRCfqfig-v____8B')
    return gd_client

def get_auth_sub_url():
    """Generate a URL that will return a temp auth token to localhost."""
    next = 'http://localhost'
    scope = 'http://spreadsheets.google.com/feeds/'
    domain = 'canonical.com'
    secure = False
    session = True
    gd_client = gdata.spreadsheet.service.SpreadsheetsService()
    return gd_client.GenerateAuthSubURL(next, scope, secure, session, domain);

def delete_row(feed, index):
    gd_client.DeleteRow(feed.entry[index])
    print 'Deleted row: ' + str(index)

def insert_task_status_rows(tasks, spreadsheet_key, worksheet_id, spreadsheet_list):
    for task in tasks:
        task_name, status, username, importance, milestone = '', '', '', '', ''
        row_data = {}
        task_name = unicode(task.title)
        status = unicode(task.status)
        importance = unicode(task.importance)
        # Remove spaces for Google to accept as column label
        status = status.replace(' ', '').lower()

        if task.assignee:
            username = str(task.assignee.name)
        else:
            username = 'Unassigned'

        if task.milestone:
            milestone = str(task.milestone.title)

        task_name = importance + ' - ' + task_name
            
        row_data['name'] = username
        row_data['storyid'] = ''
        row_data[status] = task_name
        row_data['milestone'] = milestone

        entry = gd_client.InsertRow(row_data, spreadsheet_key, worksheet_id)
        if isinstance(entry, spreadsheet_list):
            print '\n***************************************'
            print row_data
            print '***************************************'

def get_this_spreadsheet_worksheet_id(feed, sheet_name):
    for i, entry in enumerate(feed.entry):
        if entry.title.text == sheet_name:
            return entry.id.text.rsplit('/', 1)[1]

gd_client = authenticate_to_google_spreadsheet()

# get the list of worksheets
feed = gd_client.GetWorksheetsFeed(SPREADSHEET_KEY)

# get worksheet id's for task status tab
task_status_tab = get_this_spreadsheet_worksheet_id(feed, 'Task Status')
if task_status_tab is None:
    print "Error finding the test data tab in the spreadsheet."
task_worksheet = gd_client.GetListFeed(SPREADSHEET_KEY, task_status_tab)

feed = gd_client.GetListFeed(SPREADSHEET_KEY, task_status_tab)
for i, entry in enumerate(feed.entry):
    if i > 0:
        delete_row(feed, i)

tag = 'ubuntuone-karmic' 
tasks = ubunet.searchTasks(tags=tag)
insert_task_status_rows(tasks, SPREADSHEET_KEY, task_status_tab, gdata.spreadsheet.SpreadsheetsList)