"""
raven.scripts.runner
~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

import logging
import os
import sys
import pwd

from raven import Client


def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    dsn = ' '.join(sys.argv[2:]) or os.environ.get('SENTRY_DSN')
    if not dsn:
        print "Error: No configuration detected!"
        print "You must either pass a DSN to the command, or set the SENTRY_DSN environment variable."
        sys.exit(1)

    print "Using DSN configuration:"
    print " ", dsn
    print

    client = Client(dsn, include_paths=['raven'])

    print "Client configuration:"
    for k in ('servers', 'project', 'public_key', 'secret_key'):
        print '  %-15s: %s' % (k, getattr(client, k))
    print

    if not all([client.servers, client.project, client.public_key, client.secret_key]):
        print "Error: All values must be set!"
        sys.exit(1)

    print 'Sending a test message...',
    ident = client.get_ident(client.captureMessage(
        message='This is a test message generated using ``raven test``',
        data={
            'culprit': 'raven.scripts.runner',
            'logger': 'raven.test',
            'sentry.interfaces.Http': {
                'method': 'GET',
                'url': 'http://example.com',
            }
        },
        level=logging.INFO,
        stack=True,
        extra={
            'user': pwd.getpwuid(os.geteuid())[0],
            'loadavg': os.getloadavg(),
        }
    ))

    if client.state.did_fail():
        print 'error!'
        return False

    print 'success!'
    print
    print 'The test message can be viewed at the following URL:'
    url = client.servers[0].split('/api/store/', 1)[0]
    print '  %s/%s/search/?q=%s' % (url, client.project, ident)

@@ -252,170 +252,677 @@
 pwd%0A
-%0Afrom raven import Client%0A%0A%0Adef main():%0A    root = logging.getLogger('sentry.errors')%0A    root.setLevel(logging.DEBUG)%0A    root.addHandler(logging.StreamHandler()
+import json%0Afrom optparse import OptionParser%0A%0Afrom raven import Client%0A%0Adef store_json(option, opt_str, value, parser):%0A    try:%0A        value = json.loads(value)%0A    except ValueError:%0A        print %22Invalid was used for option %25s.  Received: %25s%22 %25 (opt_str, value)%0A        sys.exit(1)%0A    setattr(parser.values, option.dest, value)%0A%0Adef main():%0A    root = logging.getLogger('sentry.errors')%0A    root.setLevel(logging.DEBUG)%0A    root.addHandler(logging.StreamHandler())%0A%0A    parser = OptionParser()%0A    parser.add_option(%22--data%22, action=%22callback%22, callback=store_json,%0A                      type=%22string%22, nargs=1, dest=%22data%22)%0A    (opts, args) = parser.parse_args(
 )%0A%0A 
@@ -943,18 +943,14 @@
 oin(
-sys.argv%5B2
+args%5B1
 :%5D) 
@@ -1786,16 +1786,29 @@
    data=
+opts.data or 
 %7B%0A      

Add --data option to 'raven test' to use custom message data.