Created
August 21, 2013 01:57
-
-
Save pshc/6289579 to your computer and use it in GitHub Desktop.
Bugsnag exception dumping script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
API_KEY = "YOUR_API_KEY_HERE" | |
import pickle | |
import re | |
import requests | |
import sys | |
def request(path, **kwargs): | |
if not path.startswith('https://'): | |
path = 'https://api.bugsnag.com/' + path | |
headers = {"Authorization": 'token ' + API_KEY} | |
r = requests.get(path, headers=headers, params=kwargs) | |
try: | |
json = r.json() | |
nextURL = parse_link(r.headers.get('link')) | |
return json, nextURL | |
except ValueError: | |
print r.text | |
sys.exit(1) | |
def parse_link(link): | |
m = link and re.match(r'<(.+)>', link) | |
return m and m.group(1) | |
def cmd_account(): | |
""" | |
Info about the (company) Bugsnag account | |
""" | |
account, _ = request('account') | |
print """ | |
Name: {name} | |
Creator: {account_creator[name]} <{account_creator[email]}> | |
""".format(**account) | |
def cmd_projects(): | |
""" | |
List all projects | |
""" | |
listing, _ = request('account/projects') | |
projects = {} | |
for project in listing: | |
projects[project['name']] = project | |
print """ | |
== {name} == | |
ID: {id} | |
API key: {api_key} | |
Errors: {errors} | |
Type: {type} | |
Errors URL: {errors_url} | |
""".format(**project) | |
pickle.dump(projects, open('bugsnag-projects.cache', 'wb')) | |
print 'Cached project data.' | |
def lookup_project(name): | |
projects = pickle.load(open('bugsnag-projects.cache', 'rb')) | |
project = projects[name] | |
if not project: | |
print "No project named {0}.".format(name) | |
sys.exit(1) | |
return project | |
def cmd_events(project_name): | |
"""<project name> | |
Keep listing exceptions forever | |
""" | |
url = lookup_project(project_name)['events_url'] | |
total = reqs = 0 | |
while True: | |
events, url = request(url, per_page=30) | |
for event in events: | |
email = event.get('meta_data') | |
email = email and email.get('user') | |
email = email and email.get('username') or email | |
email = email or '<none>' | |
print """ | |
User: {email} | |
URL: {html_url} | |
Version: {app_version} | |
Context: {context} | |
OS: {os_version} | |
""".format(email=email, **event) | |
count = len(events) | |
total += count | |
reqs += 1 | |
print 'Got {0} tickets, {1} total in {2} requests'.format(count, total, reqs) | |
def dispatch(): | |
handler = len(sys.argv) > 1 and globals().get('cmd_' + sys.argv[1]) | |
if handler: | |
handler(*sys.argv[2:]) | |
else: | |
print 'Usage: {0} <command>'.format(sys.argv[0]) | |
print 'Commands available:' | |
for cmd, f in globals().iteritems(): | |
if cmd.startswith('cmd_'): | |
print ' * {0} {1}'.format(cmd[4:], f.__doc__) | |
if __name__ == '__main__': | |
dispatch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment