#!/usr/bin/python

import logging
from glob import glob
from json import loads as load_json
from hashlib import md5
from urllib2 import urlopen, URLError
from urlparse import urljoin
from argparse import ArgumentParser
from pkg_resources import find_distributions


def md5check(path):
    logger = logging.getLogger(__name__)
    with open(path) as fh:
        md5_hash = md5()
        while True:
            chunk = fh.read(128)
            if chunk == '':
                break
            md5_hash.update(chunk)
    file_hash = md5_hash.hexdigest()
    logger.debug('file md5: {0}'.format(file_hash))
    return file_hash


def get_pypi_hash(baseurl):
    logger = logging.getLogger(__name__)
    dist = find_distributions('.').next()
    logger.debug('dist found: {0}'.format(dist))
    targer_url = urljoin(baseurl, 'pypi/{0}/json'.format(dist.project_name))
    logger.debug('opening url: {0}'.format(targer_url))
    response = urlopen(targer_url)
    pypi_json = load_json(response.read())
    md5_digest = pypi_json['releases'][dist.version][0]['md5_digest']
    logger.debug('pypi md5: {0}'.format(md5_digest))
    return md5_digest


def main():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    logger.addHandler(ch)

    parser = ArgumentParser()
    parser.add_argument('-f', '--file',
                        nargs=1,
                        default=glob('dist/*.tar.gz'))
    parser.add_argument('-u', '--url',
                        nargs=1,
                        default=['https://pypi.python.org/'])
    parser.add_argument('-v', '--verbose', action='store_true')
    args = parser.parse_args()

    ch.setLevel(logging.DEBUG if args.verbose else logging.INFO)

    try:
        file_hash = md5check(args.file[0])
        pypi_hash = get_pypi_hash(args.url[0])
    except (IndexError, IOError):
        logger.error('File not found')
        exit(1)
    except StopIteration:
        logger.error('Distribution not found')
        exit(1)
    except URLError:
        logger.error('Unable to open url')
        exit(1)

    if file_hash == pypi_hash:
        logger.info('Package succesfully uploaded to pypi')
        exit(0)
    else:
        logger.info('Error, package upload failed')
        exit(1)


if __name__ == '__main__':
    main()

@@ -1140,16 +1140,250 @@
 igest%0A%0A%0A
+def get_pypi_url(branch):%0A    if branch == 'pypi-release':%0A        return 'https://pypi.python.org/'%0A    elif branch == 'pypi-test':%0A        return 'https://testpypi.python.org/'%0A    else:%0A        raise ValueError('Unknown branch')%0A%0A%0A
 def main
@@ -1386,16 +1386,16 @@
 main():%0A
-
     logg
@@ -1680,19 +1680,31 @@
 r.gz'))%0A
+%0A
    
+ urlgroup =
  parser.
@@ -1715,16 +1715,71 @@
 argument
+_group('PyPi repository url')%0A    urlgroup.add_argument
 ('-u', '
@@ -1802,32 +1802,34 @@
                 
+  
 nargs=1,%0A       
@@ -1813,32 +1813,34 @@
        nargs=1,%0A
+  
                 
@@ -1884,16 +1884,179 @@
 .org/'%5D)
+%0A%0A    branchgroup = parser.add_argument_group('PyPi repository url by branch')%0A    branchgroup.add_argument('-b', '--branch',%0A                             nargs=1)
 %0A    par
@@ -2258,24 +2258,196 @@
 gs.file%5B0%5D)%0A
+        if args.branch:%0A            baseurl = get_pypi_url(args.branch%5B0%5D)%0A        else:%0A            baseurl = args.url%0A        logger.info('pypi url: %7B%7D'.format(baseurl))%0A
         pypi
@@ -2472,19 +2472,15 @@
 ash(
-args.url%5B0%5D
+baseurl
 )%0A  

add pypi url by branch