summaryrefslogtreecommitdiffstats
path: root/selftest.py
blob: 30f212d6bd98a6422e6a84978562c14d786f4f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
# Simple self-test of the bugzilla module

# Copyright (C) 2007 Red Hat Inc.
# Author: Will Woods <wwoods@redhat.com>
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

from bugzilla import Bugzilla
import os, glob, sys
import xmlrpclib

def find_firefox_cookiefile():
    cookieglob = os.path.expanduser('~/.mozilla/firefox/*default*/cookies.txt')
    cookiefiles = glob.glob(cookieglob)
    if cookiefiles:
        # TODO return whichever is newest
        return cookiefiles[0]

def selftest(user='',password=''):
    url = 'https://partner-bugzilla.redhat.com/xmlrpc.cgi'
    public_bug = 1
    private_bug = 250666
    bugidlist = (1,2,3,1337,123456)
    query = {'product':'Fedora',
             'component':'kernel',
             'version':'devel',
             'long_desc':'wireless'}
    
    print "Woo, welcome to the bugzilla.py self-test."
    print "Using bugzilla at " + url
    if user and password:
        print 'Using username "%s", password "%s"' % (user,password)
        bz = Bugzilla(url=url,user=user,password=password)
    else:
        cookies = find_firefox_cookiefile()
        if not cookies:
            print "Could not find any cookies for that URL!"
            print "Log in with firefox or give me a username/password."
            sys.exit(1)
        print "Reading cookies from " + cookies
        bz = Bugzilla(url=url,cookies=cookies)
    print "Reading product list"
    print bz.getproducts()
    print

    print "Reading public bug (#%i)" % public_bug
    print bz.getbugsimple(public_bug)
    print

    print "Reading private bug (#%i)" % private_bug
    try:
        print bz.getbugsimple(private_bug)
    except xmlrpclib.Fault, e:
        if 'NotPermitted' in e.faultString:
            print "Failed: Not authorized."
        else:
            print "Failed: Unknown XMLRPC error: %s"  % e
    q_msg = "%s %s %s %s" % (query['product'],query['component'],
                             query['version'],query['long_desc'])
    print

    print "Reading multiple bugs, one-at-a-time: %s" % str(bugidlist)
    for b in bugidlist:
        print bz.getbugsimple(b)
    print

    print "Reading multiple bugs, all-at-once: %s" % str(bugidlist)
    for b in bz.getbugssimple(bugidlist):
        print b
    print

    print "Querying %s bugs" % q_msg
    bugs = bz.query(query)
    print "%s bugs found." % len(bugs)
    for bug in bugs:
        print "Bug %s" % bug
    print

    print "Awesome. We're done."

if __name__ == '__main__':
    user = ''
    password = ''
    if len(sys.argv) > 2:
        (user,password) = sys.argv[1:3]
    try:
        selftest(user,password)
    except KeyboardInterrupt:
        print "Exiting on keyboard interrupt."