/usr/lib/python2.7/dist-packages/bitbucket/api.py is in python-bitbucket 0.1-1.
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 268 269 270 271 272 273 274 275 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Bitbucket API wrapper. Written to be somewhat like py-github:
https://github.com/dustin/py-github
"""
from urllib2 import Request, urlopen, URLError
from urllib import urlencode
from functools import wraps
import datetime
import time
try:
import json
except ImportError:
import simplejson as json
__all__ = ['AuthenticationRequired', 'to_datetime', 'BitBucket']
api_base = 'https://api.bitbucket.org/1.0/'
api_toplevel = 'https://api.bitbucket.org/'
class AuthenticationRequired(Exception):
pass
def requires_authentication(method):
@wraps(method)
def wrapper(self, *args, **kwargs):
username = self.bb.username if hasattr(self, 'bb') else self.username
password = self.bb.password if hasattr(self, 'bb') else self.password
if not all((username, password)):
raise AuthenticationRequired("%s requires authentication" % method.__name__)
return method(self, *args, **kwargs)
return wrapper
def smart_encode(**kwargs):
"""Urlencode's provided keyword arguments. If any kwargs are None, it does
not include those."""
args = dict(kwargs)
for k,v in args.items():
if v is None:
del args[k]
if not args:
return ''
return urlencode(args)
def to_datetime(timestring):
"""Convert one of the bitbucket API's timestamps to a datetime object."""
format = '%Y-%m-%d %H:%M:%S'
return datetime.datetime(*time.strptime(timestring, format)[:7])
class BitBucket(object):
"""Main bitbucket class. Use an instantiated version of this class
to make calls against the REST API."""
def __init__(self, username='', password=''):
self.username = username
self.password = password
# extended API support
def build_request(self, url, data=None):
if not all((self.username, self.password)):
return Request(url,data)
auth = '%s:%s' % (self.username, self.password)
auth = {'Authorization': 'Basic %s' % (auth.encode('base64').strip())}
if data and not isinstance(data, (str, unicode)):
data = urlencode(data)
return Request(url, data, auth)
def load_url(self, url, quiet=False, method=None, data=None):
request = self.build_request(url,data)
if method:
request.get_method = lambda : method
try:
result = urlopen(request).read()
except:
if not quiet:
import traceback
traceback.print_exc()
print "url was: %s" % url
result = "[]"
return result
def user(self, username):
return User(self, username)
def repository(self, username, slug):
return Repository(self, username, slug)
@property
@requires_authentication
def ssh_keys(self):
return SSHKeys(self)
@requires_authentication
def new_repository(self,name,**data):
"""Create a new repository with the given name
for the authenticated user.
Return a Repository object
"""
url = api_base + 'repositories/'
data['name'] = name
response = json.loads(self.load_url(url,data=data))
if 'slug' in response:
return self.repository(self.username,response['slug'])
@requires_authentication
def remove_repository(self,slug):
"""Given a slug, remove a repository from the
authenticated user"""
url = api_base + 'repositories/%s/%s' % (self.username,slug)
method = 'DELETE'
self.load_url(url,method=method)
return True
@requires_authentication
def emails(self):
"""Returns a list of configured email addresses for the authenticated user."""
url = api_base + 'emails/'
return json.loads(self.load_url(url))
def __repr__(self):
extra = ''
if all((self.username, self.password)):
extra = ' (auth: %s)' % self.username
return '<BitBucket API%s>' % extra
class User(object):
"""API encapsulation for user related bitbucket queries."""
def __init__(self, bb, username):
self.bb = bb
self.username = username
def repository(self, slug):
return Repository(self.bb, self.username, slug)
def repositories(self):
user_data = self.get()
return user_data['repositories']
def events(self, start=None, limit=None):
query = smart_encode(start=start, limit=limit)
url = api_base + 'users/%s/events/' % self.username
if query:
url += '?%s' % query
return json.loads(self.bb.load_url(url))
def get(self):
url = api_base + 'users/%s/' % self.username
return json.loads(self.bb.load_url(url))
def __repr__(self):
return '<User: %s>' % self.username
class Repository(object):
def __init__(self, bb, username, slug):
self.bb = bb
self.username = username
self.slug = slug
self.base_url = api_base + 'repositories/%s/%s/' % (self.username, self.slug)
def get(self):
return json.loads(self.bb.load_url(self.base_url))
def changeset(self, revision):
"""Get one changeset from a repos."""
url = self.base_url + 'changesets/%s/' % (revision)
return json.loads(self.bb.load_url(url))
def changesets(self, limit=None):
"""Get information about changesets on a repository."""
url = self.base_url + 'changesets/'
query = smart_encode(limit=limit)
if query: url += '?%s' % query
return json.loads(self.bb.load_url(url, quiet=True))
def tags(self):
"""Get a list of tags for a repository."""
url = self.base_url + 'tags/'
return json.loads(self.bb.load_url(url))
def branches(self):
"""Get a list of branches for a repository."""
url = self.base_url + 'branches/'
return json.loads(self.bb.load_url(url))
def issue(self, number):
return Issue(self.bb, self.username, self.slug, number)
def issues(self, start=None, limit=None):
url = self.base_url + 'issues/'
query = smart_encode(start=start, limit=limit)
if query: url += '?%s' % query
return json.loads(self.bb.load_url(url))
def events(self):
url = self.base_url + 'events/'
return json.loads(self.bb.load_url(url))
def followers(self):
url = self.base_url + 'followers/'
return json.loads(self.bb.load_url(url))
@requires_authentication
def services(self):
url = self.base_url + 'services/'
return json.loads(self.bb.load_url(url))
@requires_authentication
def new_service(self, **data):
url = self.base_url + 'services/'
return json.loads(self.bb.load_url(url, method="POST", data=data))
@requires_authentication
def privileges(self):
url = api_base + 'privileges/%s/%s' % (self.username, self.slug)
return json.loads(self.bb.load_url(url))
@requires_authentication
def set_privilege(self, user, privilege):
url = api_base + 'privileges/%s/%s/%s/' % (self.username, self.slug, user)
return json.loads(self.bb.load_url(url, method="PUT", data=privilege))
def __repr__(self):
return '<Repository: %s\'s %s>' % (self.username, self.slug)
class Issue(object):
def __init__(self, bb, username, slug, number):
self.bb = bb
self.username = username
self.slug = slug
self.number = number
self.base_url = api_base + 'repositories/%s/%s/issues/%s/' % (username, slug, number)
def get(self):
return json.loads(self.bb.load_url(self.base_url))
def followers(self):
url = self.base_url + 'followers/'
return json.loads(self.bb.load_url(url))
def __repr__(self):
return '<Issue #%s on %s\'s %s>' % (self.number, self.username, self.slug)
class SSHKeys(object):
def __init__(self, bb):
self.bb = bb
def _url(self, subPath=None):
_url = r"https://api.bitbucket.org/1.0/ssh-keys/"
if subPath:
_url += subPath.strip("/")
_url += "/"
return _url
def get(self):
return json.loads(self.bb.load_url(self._url()))
def delete(self, pk):
_pk = int(pk) # Key ID has to be integer
return self.bb.load_url(
self._url(str(_pk)),
method="DELETE",
)
def add(self, key):
return json.loads(self.bb.load_url(
self._url(),
method="POST",
data={"key": key},
))
|