/usr/share/pyshared/lpltk/LaunchpadService.py is in python-launchpadlib-toolkit 2.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | #!/usr/bin/python
import os
import sys
import json
import httplib2
import launchpadlib
from xml.etree.ElementTree import ParseError
from launchpadlib.launchpad import *
from debug import *
from bug import Bug
from distributions import Distributions
from projects import Projects
from person import Person
class LaunchpadServiceError(Exception):
"""LaunchpadServiceError
An exception class that will be raised if there are any errors initializing
a LaunchpadService instance.
"""
# __init__
#
def __init__(self, error):
self.msg = error
class LaunchpadService:
"""
Manages connection to Launchpad services.
"""
def __init__(self, config=None):
"""Initialize the Arsenal instance.
The user's configuration (if one exists) is loaded and
incorporated into the standard options. Access to Launchpad is
initialized.
Configuration values to override can be passed in through config.
For example::
lp = LaunchpadService(config={
'launchpad_services_root': 'edge',
'read_only': True
})
lp = LaunchpadService(config={
'launchpad_client_name': 'my-lpltoolkit-project',
'launchpad_services_root': 'https://api.launchpad.dev'
})
lp = LaunchpadService(config={
'launchpad_version': '1.0',
'bot': True
})
"""
self.project = None
# Setting LPLTK_DEBUG environment variable enables debug messages in this
# class and in httplib2 as well.
#
if "LPLTK_DEBUG" in os.environ:
httplib2.debuglevel = os.getenv("LPLTK_DEBUG", None)
sys.stdout = DebugStdOut()
# Default configuration parameters
#
self.config = {}
self.config['launchpad_client_name'] = 'lpltk'
self.config['launchpad_services_root'] = 'production' # 'staging' 'edge' 'production'
self.config['project_name'] = ''
self.config['bot'] = False
self.config['read_only'] = False # FIXME: 'read_only' doesn't seem very descriptive here.
self.config['launchpad_version'] = 'devel'
# The configuration dictionary which is ~/.lpltkrc will override the
# default configuration parameters.
#
self._load_user_config()
# The config dictionary passed into this method override all config parameters.
#
if config != None:
for k in config.keys():
self.config[k] = config[k]
# So that we can use any change a user may have made via their
# config file, we need to set the 'launchpad_cachedir' and
# 'launchpad_creddir' after loading the user's config file.
#
if 'launchpad_cachedir' not in self.config:
self.config['launchpad_cachedir'] = os.path.join(os.path.expanduser('~'),
'.cache',
self.config['launchpad_client_name'])
if 'launchpad_creddir' not in self.config:
self.config['launchpad_creddir'] = os.path.join(os.path.expanduser('~'),
'.config',
self.config['launchpad_client_name'])
if self.config['launchpad_services_root'] == 'qastaging':
self.config['launchpad_services_root'] = 'https://api.qastaging.launchpad.net'
if self.config['launchpad_creddir']:
filename_parts = [
'credentials',
self.config['launchpad_services_root'].replace('/','_')
]
if self.config['bot']:
filename_parts.insert(0, 'bot')
self.config['launchpad_credentials_file'] = os.path.join(
self.config['launchpad_creddir'],
'-'.join(filename_parts))
if not os.path.exists(self.config['launchpad_creddir']):
os.makedirs(self.config['launchpad_creddir'],0700)
self.reset()
return
def reset(self):
"""
Re-establish access to Launchpad and reload the specific project if one is
specified.
"""
dbg("Launchpadlib Version: %s" %(launchpadlib.__version__))
dbg("Login With: %s %s %s" %(self.config['launchpad_client_name'],
self.config['launchpad_services_root'],
self.config['launchpad_credentials_file']))
dbg("Read Only: %s" %(self.config['read_only']))
dbg("Bot: %s" %(self.config['bot']))
try:
if self.config['read_only']:
self.launchpad = Launchpad.login_anonymously(
self.config['launchpad_client_name'],
service_root=self.config['launchpad_services_root'],
version=self.config['launchpad_version'])
else:
self.launchpad = Launchpad.login_with(
self.config['launchpad_client_name'],
service_root=self.config['launchpad_services_root'],
launchpadlib_dir=self.config['launchpad_cachedir'],
credentials_file=self.config['launchpad_credentials_file'],
version=self.config['launchpad_version'])
if self.config['project_name'] != '':
self.load_project(self.config['project_name'])
except ParseError as e:
# This indcates possibly corrupted cache
# TODO: Cleanup cache automatically?
dbg("Error: Error parsing JSON from Launchpad. Cache corruption?")
dbg(" + Try clearing lpltk cache?")
dbg(e)
except:
# Eventually, it would be nice if this handled LP exceptions and gave more helpful
# error messages. But for now it should just raise what it's given.
#
raise
return
# get_bug
#
def get_bug(self, bug_number):
return Bug(self, bug_number)
# get_launchpad_bug
#
def get_launchpad_bug(self, bug_number):
""" Fetch a Launchpad bug object for a specific Launchpad bug id. """
return self.launchpad.bugs[bug_number]
def load_project(self, project):
""" Connect to a specific Launchpad project. """
try:
self.project = self.launchpad.projects[project]
except KeyError:
raise LaunchpadServiceError("%s is not a recognized Launchpad project." % (project))
if self.project is None:
try:
self.project = self.launchpad.distributions[project]
except KeyError:
raise LaunchpadServiceError("%s is not a recognized Launchpad distribution." % (project))
self.config['project_name'] = project
return self.project
def get_person(self, name):
""" Get a Person by the given username. """
try:
person = Person(None, self.launchpad.people[name], service=self)
except KeyError:
raise LaunchpadServiceError("%s is not a person or team in Launchpad." % (name))
return person
# Deprecated
def person(self, name):
""" Alias for get_person """
return self.get_person(name)
def get_team_participants(self, team):
""" Get all direct and indirect members of a Launchpad team. """
team = self.get_person(team)
if team.lpperson.is_team is False:
raise LaunchpadServiceError("%s is not a team so has no members." % (team))
people = []
for person in team.lpperson.participants:
people.append(Person(None, person))
return people
def get_team_members(self, team):
""" Get only direct members of a Launchpad team. """
team = self.get_person(team)
if team.lpperson.is_team is False:
raise LaunchpadServiceError("%s is not a team so has no members." % (team))
people = []
for person in team.lpperson.members:
people.append(Person(None, person))
return people
def _load_user_config(self):
""" Load configuration from ~/.lpltkrc
If the users home directory contains a configuration file, load that in. The
name of the configuration file is '.lpltkrc'. The format of the file is
json. The json format should be an array. The contents of that array will
be merged with the default one 'self.config' in this class.
"""
if 'configuration_file' in self.config:
cfg_path = self.config['configuration_file']
else:
cfg_path = os.path.join(os.path.expanduser('~'), ".lpltkrc")
if os.path.exists(cfg_path):
with open(cfg_path, 'r') as f:
user_config = json.load(f)
for k in user_config.keys():
self.config[k] = user_config[k]
#--------------------------------------------------------------------------
# distributions
#
@property
def distributions(self):
return Distributions(self)
#--------------------------------------------------------------------------
# projects
#
@property
def projects(self):
return Projects(self)
#--------------------------------------------------------------------------
# create_bug
#
# Create a new, launchpad bug.
#
def create_bug(self, project, package, title, description, tags=[]):
proj = self.projects[project]
target = proj.self_link
if package:
target += "/+source/" + package
lp_bug = self.launchpad.bugs.createBug(target=target, title=title, description=description, tags=[])
return self.get_bug(lp_bug.id)
# vi:set ts=4 sw=4 expandtab:
|