changeset 0:349a78c460e2

Origination.
author Atul Varma <avarma@mozilla.com>
date Mon, 12 Apr 2010 23:24:41 -0700
parents
children aca76355e4d8
files bugzilla
diffstat 1 files changed, 75 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bugzilla	Mon Apr 12 23:24:41 2010 -0700
@@ -0,0 +1,75 @@
+#! /usr/bin/env python
+
+import httplib
+import urllib
+from urlparse import urlparse
+
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
+def json_request(method, url, query_args=None, body=None):
+    if query_args is None:
+        query_args = {}
+
+    headers = {'Accept': 'application/json',
+               'Content-Type': 'application/json'}
+
+    urlparts = urlparse(url)
+    if urlparts.scheme == 'https':
+        connclass = httplib.HTTPSConnection
+    elif urlparts.scheme == 'http':
+        connclass = httplib.HTTPConnection
+    else:
+        raise ValueError('unknown scheme "%s"' % urlparts.scheme)
+    conn = connclass(urlparts.netloc)
+    path = urlparts.path
+    if query_args:
+        path += '?%s' % urllib.urlencode(query_args)
+    if body is not None:
+        body = json.dumps(body)
+    conn.request(method, path, body, headers)
+    response = conn.getresponse()
+    status, reason = response.status, response.reason
+    mimetype = response.msg.gettype()
+    data = response.read()
+    conn.close()
+
+    if mimetype == 'application/json':
+        data = json.loads(data)
+
+    return {'status': response.status,
+            'reason': response.reason,
+            'content_type': mimetype,
+            'body': data}
+
+def main(config):
+    #print json_request('GET',
+    #                   '%s/attachment/436897' % config['api_server'],
+    #                   query_args={'attachmentdata': 1})
+    raise NotImplementedError()
+
+if __name__ == '__main__':
+    import os
+    import sys
+
+    mypath = __import__('__main__').__file__
+    configfile = os.path.join(os.path.dirname(mypath),
+                              'bugzilla-config.json')
+    config = json.loads(open(configfile).read())
+
+    if 'username' in config and 'password' not in config:
+        from getpass import getpass
+
+        try:
+            config['password'] = getpass('Enter password for %s: ' %
+                                         config['username'])
+        except KeyboardInterrupt:
+            config['password'] = None
+
+        if not config['password']:
+            print "Aborted."
+            sys.exit(1)
+
+    main(config)