~ubuntu-branches/ubuntu/natty/quickly/natty

« back to all changes in this revision

Viewing changes to quickly/launchpadaccess.py

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-07-24 18:16:30 UTC
  • Revision ID: james.westby@ubuntu.com-20090724181630-s2wo7hvahk8u0hqb
Tags: 0.1
Initial release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright 2009 Canonical Ltd.
 
3
# Author 2009 Didier Roche
 
4
#
 
5
# This file is part of Quickly
 
6
#
 
7
#This program is free software: you can redistribute it and/or modify it 
 
8
#under the terms of the GNU General Public License version 3, as published 
 
9
#by the Free Software Foundation.
 
10
 
 
11
#This program is distributed in the hope that it will be useful, but 
 
12
#WITHOUT ANY WARRANTY; without even the implied warranties of 
 
13
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
14
#PURPOSE.  See the GNU General Public License for more details.
 
15
 
 
16
#You should have received a copy of the GNU General Public License along 
 
17
#with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import getpass
 
20
import os
 
21
import sys
 
22
import subprocess
 
23
 
 
24
LAUNCHPAD_URL = "https://launchpad.net"
 
25
LAUNCHPAD_STAGING_URL = "https://staging.launchpad.net"
 
26
LAUNCHPAD_CODE_STAGING_URL = "https://code.staging.launchpad.net"
 
27
 
 
28
 
 
29
try:
 
30
    from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT, STAGING_SERVICE_ROOT
 
31
    from launchpadlib.errors import HTTPError
 
32
    from launchpadlib.credentials import Credentials
 
33
except ImportError:
 
34
    suggestion = _("Check whether python-launchpadlib is installed")
 
35
 
 
36
 
 
37
from quickly import bzrbinding
 
38
from quickly import configurationhandler
 
39
 
 
40
import gettext
 
41
from gettext import gettext as _
 
42
 
 
43
 
 
44
# if config not already loaded
 
45
if not configurationhandler.project_config:
 
46
    configurationhandler.loadConfig()
 
47
 
 
48
# check if there is no global variable specifying staging
 
49
if os.getenv('QUICKLY') is not None and "staging" in os.getenv('QUICKLY').lower():
 
50
    launchpad_url = LAUNCHPAD_STAGING_URL
 
51
    lp_server = "staging"
 
52
else:
 
53
    launchpad_url = LAUNCHPAD_URL
 
54
    lp_server = "edge"
 
55
 
 
56
 
 
57
 
 
58
 
 
59
def die(message):
 
60
    print >> sys.stderr, _("Fatal: ") + message
 
61
    sys.exit(1)
 
62
 
 
63
 
 
64
def initialize_lpi():
 
65
    ''' Initialize launchpad binding, asking for crendential
 
66
 
 
67
            :return the launchpad object
 
68
    '''
 
69
 
 
70
    launchpad = None
 
71
 
 
72
    # setup right cache, credential and server
 
73
    lp_cache_dir = os.path.expanduser('~/.quickly-data/cache/')
 
74
    if not os.path.isdir(lp_cache_dir):
 
75
        os.makedirs(lp_cache_dir)
 
76
 
 
77
    lp_cred_dir = os.path.expanduser("~/.quickly-data/lp_credentials/")
 
78
    if not os.path.isdir(lp_cred_dir):
 
79
        os.makedirs(lp_cred_dir)
 
80
        os.chmod(lp_cred_dir, 0700)
 
81
 
 
82
    # check which server to address
 
83
    if lp_server == "staging":
 
84
        lp_cred = lp_cred_dir + "quickly-cred-staging"
 
85
        SERVICE_ROOT = STAGING_SERVICE_ROOT
 
86
        print _("WARNING: you are using staging and not launchpad real production server")
 
87
    else:
 
88
        lp_cred = lp_cred_dir + "quickly-cred"
 
89
        SERVICE_ROOT = EDGE_SERVICE_ROOT
 
90
 
 
91
    # load stored LP credentials
 
92
    try:
 
93
        print _("Get Launchpad Settings")
 
94
        lp_cred_file = open(lp_cred, 'r')
 
95
        credentials = Credentials()
 
96
        credentials.load(lp_cred_file)
 
97
        lp_cred_file.close()
 
98
        launchpad = Launchpad(credentials, SERVICE_ROOT, lp_cache_dir)
 
99
    except IOError:
 
100
        print _('Initial Launchpad binding. You must choose "Change Anything"')
 
101
        launchpad = Launchpad.get_token_and_login('quickly', SERVICE_ROOT, lp_cache_dir)
 
102
        lp_cred_file = open(lp_cred, 'w')
 
103
        launchpad.credentials.save(lp_cred_file)
 
104
        lp_cred_file.close()
 
105
 
 
106
        # try to setup bzr
 
107
        me = launchpad.me
 
108
        bzrbinding.bzr_set_login(me.display_name, me.preferred_email_address.email, me.name)        
 
109
 
 
110
    if launchpad is None:
 
111
        if suggestion is None:
 
112
             suggestion = _("Unknown reason")
 
113
        die(_("Couldn't setup Launchpad for quickly ; %s") % suggestion)
 
114
    print _("Launchpad connexion is ok")
 
115
 
 
116
    return launchpad
 
117
 
 
118
 
 
119
def link_project(launchpad, question):
 
120
    ''' Link to launchpad project, erasing previous one if already set
 
121
    
 
122
    
 
123
        :return project object'''
 
124
 
 
125
    choice = "0"
 
126
    while choice == "0":    
 
127
        
 
128
        lp_id = raw_input("%s, leave blank to abort.\nLaunchpad project name: " % question)
 
129
        if lp_id == "":
 
130
            print _("No launchpad project give, aborting.")
 
131
            exit(1)
 
132
            
 
133
        prospective_projects = launchpad.projects.search(text=lp_id)
 
134
        project_number = 1
 
135
        project_names = []
 
136
        for project in prospective_projects:
 
137
            print ("---------------- [%s] ----------------") % project_number
 
138
            print "  " + project.title
 
139
            print ("--------------------------------------")
 
140
            print _("Project name: %s") % project.display_name
 
141
            print _("Launchpad url: %s/%s") % (launchpad_url, project.name)
 
142
            project_names.append(project.name)
 
143
            print project.summary
 
144
            project_number += 1            
 
145
        print
 
146
 
 
147
        if not list(prospective_projects):
 
148
            message = _("No project found")
 
149
        else:
 
150
            message = _("Choose your project number")
 
151
        choice = raw_input("%s, leave blank to abort, 0 for another search.\nYour choice: " % message)
 
152
 
 
153
    try:
 
154
        choice = int(choice)
 
155
        if choice in range(1, project_number):
 
156
            project = launchpad.projects[project_names[choice - 1]]
 
157
        else:
 
158
            raise ValueError
 
159
    except ValueError:
 
160
        print _("No right number given, aborting.")
 
161
        exit(1)
 
162
    configurationhandler.project_config['lp_id'] = project.name
 
163
    configurationhandler.saveConfig()
 
164
    
 
165
    return project
 
166
 
 
167
def get_project(launchpad):
 
168
    ''' Get quickly project through launchpad.
 
169
    
 
170
        :return project object
 
171
    '''
 
172
        
 
173
    # try to get project
 
174
    try:
 
175
        lp_id = configurationhandler.project_config['lp_id']
 
176
        project = launchpad.projects[lp_id]
 
177
       
 
178
    # else, bind the project to LP
 
179
    except KeyError:        
 
180
        project = link_project(launchpad, "No Launchpad project set")
 
181
        
 
182
    return project
 
183