Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e0b6206d0f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 164 lines (134 sloc) 3.932 kb
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#!/usr/bin/env python
# Copyright 2011 Red Hat Inc.
# Author: Kushal Das <kdas@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.

import sys
import urllib2
import json
import os
from optparse import OptionParser

class DarkError:
    CONFIG_ERROR = 5
    NETWORK_ERROR = 2
    OPTION_ERROR = 4
    NODATA_ERROR = 1


def geturls():
    """
Read the config file and get the URL(s)
"""
    if os.path.isfile('./darkclient.conf'):
        path = './darkclient.conf'
    elif os.path.isfile('/etc/darkclient.conf'):
        path = '/etc/darkclient.conf'
    else:
        return
    data = None
    with open(path) as fileobj:
        data = fileobj.readlines()
    return [line.strip('\n') for line in data]

def callback(option, opt_str, value, parser):
    "Callback method"
    args = []
    for arg in parser.rargs:
        if arg[0] != "-":
            args.append(arg)
        else:
            del parser.rargs[:len(args)]
            break
    if getattr(parser.values, option.dest):
        args.extend(getattr(parser.values, option.dest))
    setattr(parser.values, option.dest, args)

def readurl(url):
    """
Reads the URL and returns decoded json objects
"""
    ret = 0
    data = None
    try:
        fileobj = urllib2.urlopen(url)
        textdata = fileobj.read()
        fileobj.close()
        data = json.loads(textdata)
    except urllib2.URLError, error:
        print error
        ret = DarkError.NETWORK_ERROR
    return data, ret

def dark_find_ids(url, ids):
    """
Find the ids given and print the result
"""
    turl = url + "/buildids/"
    for gid in ids:
        turl = turl + gid + ','

    turl = turl[:-1]

    data, ret = readurl(turl)
    if ret != 0:
        return ret

    if len(data) == 0:
        return DarkError.NODATA_ERROR

    for text in data:
        print text['buildid'], text['rpm'], text['elf']
    return 0

def dark_find_rpm(url, name):
    """
Find the ids given and print the result
"""
    turl = url + "/rpm2buildids/%s" % name

    data, ret = readurl(turl)

    if ret != 0:
        return ret
    if not data:
        return DarkError.NODATA_ERROR

    for text in data:
        print text['buildid'], text['rpm'], text['elf']
    return 0

def dark_find_package(url, name):
    """
Find the download URL for the package
"""
    turl = url + "/package/%s" % name

    data, ret = readurl(turl)

    if ret != 0:
        return ret
    if not data:
        return DarkError.NODATA_ERROR


    if 'url' in data:
        print data['url']
        return 0
    else:
        return DarkError.NODATA_ERROR



def main(args):
    """
Main function
"""
    parser = OptionParser()
    parser.add_option('-i', '--ids', help="GNU build-id(s)", dest='ids', \
                      action = 'callback', callback=callback)
    parser.add_option("-r", "--rpm", dest="rpm", \
                      help = "Name of the rpm file to find build-id(s)")

    parser.add_option('-p', '--package', help="Download URl for package", \
                        dest='package')

    (options, args) = parser.parse_args(args[1:])
    url = geturls()
    if url != None:
        url = url[0]
    else:
        sys.exit(DarkError.CONFIG_ERROR)
    code = DarkError.OPTION_ERROR
    if options.ids:
        code = dark_find_ids(url, options.ids)
    elif options.rpm:
        code = dark_find_rpm(url, options.rpm)
    elif options.package:
        code = dark_find_package(url, options.package)
    else:
        sys.stderr.write("Please provide rpm or id details\n")

    return code



if __name__ == '__main__':
    code = main(sys.argv)
    sys.exit(code)
Something went wrong with that request. Please try again.