#!/usr/bin/env python

import sys

from optparse import OptionParser

import requests

from slingrpm.utils import hash_file

parser = OptionParser()
parser.add_option('-f', '--file',
                  help="repostiory to update",
                  default=None, action="store", type="string",
                  dest="rpm_file")
parser.add_option('-r', '--repo_dir',
                  help="repostiory to update",
                  default=None, action="store", type="string",
                  dest="repo_dir")
parser.add_option('-u', '--slingrpm-uri',
                  help="URI of the slingrpm endpoint",
                  default=None, action="store", type="string",
                  dest="slingrpm_uri")

(options, args) = parser.parse_args()

_errors = []

if not options.rpm_file:
    _errors.append('RPM_FILE required')
if not options.repo_dir:
    _errors.append('REPO_DIR required')
if not options.slingrpm_uri:
    _errors.append('SLINGRPM_URI required')

if len(_errors) > 0:
    for error in _errors:
        print error
    parser.print_help()
    sys.exit(1)

files = {'package': open(options.rpm_file, 'rb')}
data = {'repository': options.repo_dir,
        'md5hash': hash_file(open(options.rpm_file, 'rb'))}
r = requests.post(options.slingrpm_uri, files=files, data=data)
print r.text
