#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import re
import ckanutils

from os import system, path as p

try:
    from setuptools import setup, find_packages
except ImportError:
    from distutils.core import setup, find_packages


def read(filename, parent=None):
    parent = (parent or __file__)

    try:
        with open(p.join(p.dirname(parent), filename)) as f:
            return f.read()
    except IOError:
        return ''


def parse_requirements(filename, parent=None, dep=False):
    parent = (parent or __file__)
    filepath = p.join(p.dirname(parent), filename)
    content = read(filename, parent)

    for line_number, line in enumerate(content.splitlines(), 1):
        candidate = line.strip()

        if candidate.startswith('-r'):
            for item in parse_requirements(candidate[2:].strip(), filepath, dep):
                yield item
        elif not dep and '#egg=' in candidate:
            yield re.sub('.*#egg=(.*)-(.*)', r'\1==\2', candidate)
        elif dep and '#egg=' in candidate:
            yield candidate.replace('-e ', '')
        elif not dep:
            yield candidate

# Avoid byte-compiling the shipped template
sys.dont_write_bytecode = True

if sys.argv[-1] == 'publish':
    system('python setup.py sdist upload')
    sys.exit()

# if sys.argv[-1] == 'info':
#     for k, v in ckanutils.items():
#         print('%s: %s' % (k, v))

#     sys.exit()

requirements = list(parse_requirements('requirements.txt'))
dev_requirements = list(parse_requirements('dev-requirements.txt'))
dependencies = list(parse_requirements('requirements.txt', dep=True))
readme = read('README.rst')
changes = read('CHANGES.rst').replace('.. :changelog:', '')
license = ckanutils.__license__
classifier = {'GPL': 'GNU General Public', 'MIT': 'The MIT', 'BSD': 'The BSD'}

setup(
    name=ckanutils.__title__,
    version=ckanutils.__version__,
    description=ckanutils.__description__,
    long_description=readme + '\n\n' + changes,
    author=ckanutils.__author__,
    author_email=ckanutils.__email__,
    url='https://github.com/reubano/ckanutils',
    packages=find_packages(exclude=['docs', 'tests']),
    include_package_data=True,
    install_requires=requirements,
    # dependency_links=dependencies,
    tests_require=dev_requirements,
    license=license,
    zip_safe=False,
    keywords=ckanutils.__title__,
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'License :: OSI Approved :: %s License (%s)' % (classifier[license], license),
        'Natural Language :: English',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: End Users/Desktop',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',
    ],
    platforms=['MacOS X', 'Windows', 'Linux'],
    scripts=[p.join('bin', 'ckanny')],
)

@@ -1209,217 +1209,8 @@
 ue%0A%0A
-if sys.argv%5B-1%5D == 'publish':%0A    system('python setup.py sdist upload')%0A    sys.exit()%0A%0A# if sys.argv%5B-1%5D == 'info':%0A#     for k, v in ckanutils.items():%0A#         print('%25s: %25s' %25 (k, v))%0A%0A#     sys.exit()%0A%0A
 requ

Remove extra setup.py commands