#!/usr/bin/env python
# encoding: utf-8
import logging
import unittest
import sys

try:
    from setuptools import setup
    from setuptools import Command
    from setuptools.command.build_py import build_py
except ImportError:
    from distutils.core import setup
    from distutils.core  import Command
    from distutils.command.build_py import build_py


logging.basicConfig(level=logging.DEBUG)


class BuildCommand(build_py):
    
    def run(self):
        import xtuml
        from bridgepoint import oal

        l = xtuml.ModelLoader()
        l.input('', name='<empty string>')
        l.build_metamodel()
        oal.parse('')
        build_py.run(self)


class TestCommand(Command):
    description = "Execute unit tests"
    user_options = [('name=', None, 'Limit testing to a single test case or test method')]

    def initialize_options(self):
        self.name = None
    
    def finalize_options(self):
        if self.name and not self.name.startswith('tests.'):
            self.name = 'tests.' + self.name

    def run(self):
        if self.name:
            suite = unittest.TestLoader().loadTestsFromName(self.name)
        else:
            suite = unittest.TestLoader().discover('tests')
        
        runner = unittest.TextTestRunner(verbosity=2, buffer=True)
        exit_code = not runner.run(suite).wasSuccessful()
        sys.exit(exit_code)


setup(name='pymc',
      version='1.0.0',
      description='Python executor for MC-3020',
      author='Levi Starrett',
      author_email='levi@roxsoftware.com',
      url='https://github.com/xtuml/mc',
      license='Apache 2.0',
      keywords='xtuml bridgepoint',
      packages=['mc3020'],
      requires=['pyxtuml', 'pyrsl'],
      include_package_data=True,
      zip_safe=False,
      cmdclass={'build_py': BuildCommand,
                'test': TestCommand}
      )


@@ -19,1365 +19,103 @@
 hon%0A
-# encoding: utf-8%0Aimport logging%0Aimport unittest%0Aimport sys%0A%0Atry:%0A    from setuptools import setup%0A    from setuptools import Command%0A    from setuptools.command.build_py import build_py%0Aexcept ImportError:%0A    from distutils.core import setup%0A    from distutils.core  import Command%0A    from distutils.command.build_py import build_py%0A%0A%0Alogging.basicConfig(level=logging.DEBUG)%0A%0A%0Aclass BuildCommand(build_py):%0A    %0A    def run(self):%0A        import xtuml%0A        from bridgepoint import oal%0A%0A        l = xtuml.ModelLoader()%0A        l.input('', name='%3Cempty string%3E')%0A        l.build_metamodel()%0A        oal.parse('')%0A        build_py.run(self)%0A%0A%0Aclass TestCommand(Command):%0A    description = %22Execute unit tests%22%0A    user_options = %5B('name=', None, 'Limit testing to a single test case or test method')%5D%0A%0A    def initialize_options(self):%0A        self.name = None%0A    %0A    def finalize_options(self):%0A        if self.name and not self.name.startswith('tests.'):%0A            self.name = 'tests.' + self.name%0A%0A    def run(self):%0A        if self.name:%0A            suite = unittest.TestLoader().loadTestsFromName(self.name)%0A        else:%0A            suite = unittest.TestLoader().discover('tests')%0A        %0A        runner = unittest.TextTestRunner(verbosity=2, buffer=True)%0A        exit_code = not runner.run(suite).wasSuccessful()%0A        sys.exit(exit_code)%0A
+%0Atry:%0A    from setuptools import setup%0Aexcept ImportError:%0A    from distutils.core import setup
 %0A%0Ase
@@ -502,94 +502,6 @@
 alse
-,%0A      cmdclass=%7B'build_py': BuildCommand,%0A                'test': TestCommand%7D%0A      
 )%0A
-%0A

remove cruft from setup.py