from setuptools import setup
import os
from subprocess import Popen, PIPE
import sys

import emit


# add a few commands for measurement
def cc():
    proc = Popen(['radon', 'cc'] + sys.argv[1:], stdout=PIPE)
    stdout, _ = proc.communicate()
    sys.stdout.write(stdout.decode('utf-8'))
    return 1 if stdout else 0


def mi():
    proc = Popen(['radon', 'mi'] + sys.argv[1:], stdout=PIPE)
    stdout, _ = proc.communicate()
    sys.stdout.write(stdout.decode('utf-8'))
    for line in stdout.decode('utf-8').strip().split('\n'):
        if not line.endswith('A'):
            sys.stdout.write('One or more modules did not get an A. Failing.\n')
            return 1

    return 0

if __name__ == '__main__':
    try:
        arg = sys.argv[1]
    except IndexError:
        arg = ''
    command = {'cc': cc, 'mi': mi}.get(arg, None)
    if command:
        sys.exit(command())

# pass execution to setuptools
setup(
    name='emit',
    version=emit.__version__,
    description='Emitter for stream processing',
    url='http://github.com/brianhicks/emit',
    author='Brian Hicks',
    author_email='brian@brianthicks.com',
    license='MIT',
    packages=['emit'],
    scripts=['emit/bin/emit_digraph'],
    zip_safe=False
)

@@ -678,16 +678,168 @@
 turn 0%0A%0A
+%0Adef read(fname):%0A    try:%0A        return open(os.path.join(os.path.dirname(__file__), fname)).read()%0A    except IOError:  # for tox%0A        return ''%0A%0A
 if __nam
@@ -1120,264 +1120,907 @@
     
-description='Emitter for stream processing',%0A    url='http://github.com/brianhicks/emit',%0A    author='Brian Hicks',%0A    author_email='brian@brianthicks.com',%0A    license='MIT',%0A    packages=%5B'emit'%5D
+packages=%5B'emit'%5D,%0A    scripts=%5B'emit/bin/emit_digraph'%5D,%0A%0A    # PyPI stuff%0A    author='Brian Hicks',%0A    author_email='brian@brianthicks.com',%0A    url='http://github.com/brianhicks/emit',%0A    description='Emitter for stream processing',%0A    keywords='stream processing',%0A    long_description=read('README.rst'),%0A    license='MIT',%0A    classifiers=%5B%0A        'Development Status :: 4 - Beta'
 ,%0A    
+ 
-scripts=%5B'emit/bin/emit_digraph'%5D
+   'Intended Audience :: Developers',%0A        'License :: OSI Approved :: Apache Software License',%0A        'Natural Language :: English',%0A        'Programming Language :: Python :: 2.6',%0A        'Programming Language :: Python :: 2.7',%0A        'Programming Language :: Python :: 3.2',%0A        'Programming Language :: Python :: Implementation :: CPython'
 ,%0A    
+ 
-zip_safe=False
+   'Programming Language :: Python :: Implementation :: PyPy',%0A        'Topic :: Software Development :: Libraries :: Python Modules',%0A    %5D,
 %0A)%0A

update setup.py with better info