Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support setting the API URL and user/pass in the environment

  • Loading branch information...
commit 61653363093ef9e54bab0e8b3f6603026d82d21c 1 parent ad533a5
@LegNeato authored
Showing with 85 additions and 71 deletions.
  1. +13 −1 bugzilla/agents.py
  2. +27 −22 bugzilla/utils.py
  3. +45 −48 scripts/attach.py
View
14 bugzilla/agents.py
@@ -1,9 +1,21 @@
from bugzilla.models import *
from bugzilla.utils import *
+class InvalidAPI_ROOT(Exception):
+ def __str__(self):
+ return "Invalid API url specified. " + \
+ "Please set BZ_API_ROOT in your environment " + \
+ "or pass it to the agent constructor"
+
class BugzillaAgent(object):
- def __init__(self, api_root, username=None, password=None):
+ def __init__(self, api_root=None, username=None, password=None):
+
+ if not api_root:
+ api_root = os.environ.get('BZ_API_ROOT')
+ if not api_root:
+ raise InvalidAPI_ROOT
self.API_ROOT = api_root
+
self.username, self.password = username, password
def get_bug(self, bug, include_fields='_default', exclude_fields=None, params={}):
View
49 bugzilla/utils.py
@@ -18,22 +18,27 @@ def qs(**kwargs):
def get_credentials(username=None):
- password = None
- # Try to get it from the system keychain first
- try:
- import keyring
- if not username:
- # Grab the default username as we weren't passed in a specific one
- username = keyring.get_password("bugzilla", 'default_username')
- if username:
- # Get the password for the username
- password = keyring.get_password("bugzilla", username)
- except ImportError:
- # If they don't have the keyring lib, fall back to next method
- pass
+ # Try to get it from the environment first
+ if not username:
+ username = os.environ.get('BZ_USERNAME', None)
+ password = os.environ.get('BZ_PASSWORD', None)
- # Next try the config file
+ # Try to get it from the system keychain next
+ if not username and not password:
+ try:
+ import keyring
+ if not username:
+ # Grab the default username as we weren't passed in a specific one
+ username = keyring.get_password("bugzilla", 'default_username')
+ if username:
+ # Get the password for the username
+ password = keyring.get_password("bugzilla", username)
+ except ImportError:
+ # If they don't have the keyring lib, fall back to next method
+ pass
+
+ # Then try a config file in their home directory
if not (username and password):
rcfile = os.path.expanduser('~/.bztoolsrc')
config = ConfigParser()
@@ -68,13 +73,13 @@ def get_credentials(username=None):
FILE_TYPES = {
- 'text': 'text/plain',
- 'html': 'text/html',
- 'xml': 'application/xml',
- 'gif': 'image/gif',
- 'jpg': 'image/jpeg',
- 'png': 'image/png',
- 'svg': 'image/svg+xml',
+ 'text': 'text/plain',
+ 'html': 'text/html',
+ 'xml': 'application/xml',
+ 'gif': 'image/gif',
+ 'jpg': 'image/jpeg',
+ 'png': 'image/png',
+ 'svg': 'image/svg+xml',
'binary': 'application/octet-stream',
- 'xul': 'application/vnd.mozilla.xul+xml',
+ 'xul': 'application/vnd.mozilla.xul+xml',
}
View
93 scripts/attach.py
@@ -6,36 +6,14 @@
import argparse
from bugzilla.models import Bug, Attachment, Flag, User, Comment
+from bugzilla.agents import BugzillaAgent
from bugzilla.utils import urljoin, qs, get_credentials, FILE_TYPES
-# API_ROOT example: 'https://api-dev.bugzilla.mozilla.org/0.2/'
-API_ROOT = os.environ.get('API_ROOT', 'https://api-dev.bugzilla.mozilla.org/0.2/')
-BZ_USERNAME = os.environ.get('BZ_USERNAME', None)
-BZ_PASSWORD = os.environ.get('BZ_PASSWORD', None)
-
REVIEW = 4
-
-class Agent(object):
+class AttachmentAgent(BugzillaAgent):
"""Stores credentials, navigates the site."""
- def __init__(self):
- if API_ROOT is None:
- raise "Please set the API_ROOT environment variable"
- if BZ_USERNAME && BZ_PASSWORD:
- self.username, self.password = BZ_USERNAME, BZ_PASSWORD
- else:
- username, password = get_credentials()
- self.username, self.password = username, password
-
- def get_bug(self, bug, attachments=True, comments=True, history=True):
- """Fetch Bug ``bug``."""
- tmp = {'attachmentdata': attachments, 'comments': comments,
- 'history': history}
- params = dict((k, int(v)) for k, v in tmp.items())
- url = urljoin(API_ROOT, 'bug/%s?%s' % (bug, self.qs(**params)))
- return Bug.get(url)
-
def attach(self, bug_id, filename, description, patch=False,
reviewer=None, comment='', content_type='text/plain'):
"""Create an attachment, add a comment, obsolete other attachments."""
@@ -56,24 +34,25 @@ def attach(self, bug_id, filename, description, patch=False,
def _attach(self, bug_id, filename, description, is_patch=False,
reviewer=None, content_type='text/plain'):
"""Create a new attachment."""
- fields = {'data': base64.b64encode(open(filename).read()),
- 'encoding': 'base64',
- 'file_name': filename,
- 'content_type': content_type,
- 'description': description,
- 'is_patch': is_patch,
- }
+ fields = {
+ 'data': base64.b64encode(open(filename).read()),
+ 'encoding': 'base64',
+ 'file_name': filename,
+ 'content_type': content_type,
+ 'description': description,
+ 'is_patch': is_patch,
+ }
if reviewer is not None:
fields['flags'] = [Flag(type_id=REVIEW, status='?',
requestee=User(name=reviewer))]
- url = urljoin(API_ROOT, 'bug/%s/attachment?%s' % (bug_id, self.qs()))
+ url = urljoin(self.API_ROOT, 'bug/%s/attachment?%s' % (bug_id, self.qs()))
return Attachment(**fields).post_to(url)
def _comment(self, bug_id, comment):
"""Create a new comment."""
- url = urljoin(API_ROOT, 'bug/%s/comment?%s' % (bug_id, self.qs()))
+ url = urljoin(self.API_ROOT, 'bug/%s/comment?%s' % (bug_id, self.qs()))
return Comment(text=comment).post_to(url)
def obsolete(self, bug):
@@ -109,27 +88,36 @@ def _obsolete(self, attachment):
attachment._location += '?%s' % self.qs()
attachment.put()
- def qs(self, **params):
- if self.username and self.password:
- params['username'] = self.username
- params['password'] = self.password
- return qs(**params)
-
-
def main():
+ # Script options
parser = argparse.ArgumentParser(description='Submit Bugzilla attachments')
- parser.add_argument('bug_id', type=int, metavar='BUG', help='Bug number')
- parser.add_argument('filename', metavar='FILE', help='File to upload')
- parser.add_argument('--description', help='Attachment description',
+ parser.add_argument('bug_id',
+ type=int,
+ metavar='BUG',
+ help='Bug number')
+
+ parser.add_argument('filename',
+ metavar='FILE',
+ help='File to upload')
+
+ parser.add_argument('--description',
+ help='Attachment description',
required=True)
- parser.add_argument('--patch', action='store_true',
+
+ parser.add_argument('--patch',
+ action='store_true',
help='Is this a patch?')
- parser.add_argument('--reviewer', help='Bugzilla name of someone to r?')
- parser.add_argument('--comment', help='Comment for the attachment')
- parser.add_argument('--content_type', choices=FILE_TYPES,
+ parser.add_argument('--reviewer',
+ help='Bugzilla name of someone to r?')
+
+ parser.add_argument('--comment',
+ help='Comment for the attachment')
+
+ parser.add_argument('--content_type',
+ choices=FILE_TYPES,
help="File's content_type")
args = parser.parse_args()
@@ -137,9 +125,18 @@ def main():
if args.content_type:
args.content_type = FILE_TYPES[args.content_type]
+ # Get the API root, default to bugzilla.mozilla.org
+ API_ROOT = os.environ.get('BZ_API_ROOT',
+ 'https://api-dev.bugzilla.mozilla.org/latest/')
+
+ # Authenticate
username, password = get_credentials()
- Agent(username, password).attach(**dict(args._get_kwargs()))
+ # Load the agent
+ bz = AttachmentAgent(API_ROOT, username, password)
+
+ # Attach the file
+ bz.attach(**dict(args._get_kwargs()))
if __name__ == '__main__':
Please sign in to comment.
Something went wrong with that request. Please try again.