#!/usr/bin/python

# Example how to turn a Python dictionary into a LP bug
# needs python-launchpad-bugs and python-problem-report
# Author: Martin Pitt <martin.pitt@ubuntu.com>

import os, tempfile, urllib

import launchpadbugs.storeblob
from problem_report import ProblemReport

# create some test data
data = {
    'Uname': ' '.join(os.uname()),
    'XorgConf': ('/etc/X11/xorg.conf',), # file reference
    'BinaryCrap': 'AA' + '\001' * 50 + 'ZZ'
}

# extra bug headers like tags, privacy, subscribers, etc.
extra_headers = {
    'Tags': 'auto-generated',
    'Subscribers': 'pitti',
    # 'Private': 'yes', 
}

# package and bug title
srcpackage = 'pmount'
bug_title = 'needs frobnication [dummy bug]'

# convert dictionary into a ProblemReport, since that knows how to convert the
# data to become digestible to LP (multipart/MIME)
pr = ProblemReport(type='Bug')
pr.update(data)

# convert to multipart/MIME, upload to LP cloakroom
mime = tempfile.TemporaryFile()
pr.write_mime(mime, extra_headers)
mime.flush()
mime.seek(0)

ticket = launchpadbugs.storeblob.upload(mime)
assert ticket

url = 'https://bugs.launchpad.net/ubuntu/+source/%s/+filebug/%s?%s' % (
    srcpackage, ticket, urllib.urlencode({'field.title': bug_title}))
print 'Visit the following URL to file the bug:'
print url

