#!/usr/bin/env python
"""A python/modelica based simulation environment.

Pymola contains a Python based compiler for the modelica language
and enables interacting with Modelica easily in Python.

"""

from __future__ import print_function
from setuptools import setup

import os
import sys
import subprocess
import pprint
import shutil
import fnmatch

from setuptools import setup, find_packages
from setuptools.command.build_py import build_py
from setuptools.command.build_ext import build_ext
from setuptools import Command

import versioneer

MAJOR = 0
MINOR = 1
MICRO = 5
ISRELEASED = True
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
DOCLINES = __doc__.split("\n")

CLASSIFIERS = """\
Development Status :: 1 - Planning
Intended Audience :: Science/Research
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Other
Topic :: Software Development
Topic :: Scientific/Engineering :: Artificial Intelligence
Topic :: Scientific/Engineering :: Mathematics
Topic :: Scientific/Engineering :: Physics
Topic :: Scientific/Engineering :: Visualization
Operating System :: Microsoft :: Windows
Operating System :: POSIX
Operating System :: Unix
Operating System :: MacOS
Topic :: Software Development :: Code Generators
Topic :: Software Development :: Compilers
Topic :: Software Development :: Embedded Systems
"""


ROOT_DIR = os.path.dirname(os.path.realpath(__file__))

#pylint: disable=no-init, too-few-public-methods


import sys
python_version = '.'.join([str(i) for i in sys.version_info[:3]])
python_version_required = '3.5.0'
if python_version < python_version_required:
    sys.exit("Sorry, only Python >= {:s} is supported".format(python_version_required))

class AntlrBuildCommand(Command):
    """Customized setuptools build command."""
    user_options=[]
    def initialize_options(self):
        pass
    def finalize_options(self):
        pass
    def run(self):
        "Run the build command"
        call_antlr4('Modelica.g4')

def call_antlr4(arg):
    "calls antlr4 on grammar file"
    #pylint: disable=unused-argument, unused-variable
    antlr_path = os.path.join(ROOT_DIR, "java", "antlr-4.7-complete.jar")
    classpath = ".:{:s}:$CLASSPATH".format(antlr_path)
    generated = os.path.join(ROOT_DIR, 'src', 'pymola', 'generated')
    cmd = "java -Xmx500M -cp \"{classpath:s}\" org.antlr.v4.Tool {arg:s}" \
            " -o {generated:s} -visitor -Dlanguage=Python3".format(**locals())
    print(cmd)
    proc = subprocess.Popen(cmd.split(), cwd=os.path.join(ROOT_DIR, 'src', 'pymola'))
    proc.communicate()
    with open(os.path.join(generated, '__init__.py'), 'w') as fid:
        fid.write('')

def setup_package():
    """
    Setup the package.
    """
    src_path = os.path.dirname(os.path.abspath(sys.argv[0]))
    old_path = os.getcwd()
    os.chdir(src_path)
    sys.path.insert(0, src_path)

    # Install requirements
    with open('requirements.txt', 'r') as req_file:
        install_reqs = req_file.read().split('\n')
    # pprint.pprint(install_reqs)
    
    if sys.platform == 'darwin':
        # Disable compiler optimization.  We have to do this, as the default -O3 triggers a bug in clang causing an initialization failure.
        #os.environ['CFLAGS'] = '-O0'
        # Or alternatively, use gcc:
        os.environ['CC'] = 'gcc-7'

    # Without disabling this, it will reach a limit
    # on tracking and disable tracking and then recompile, which is slow
    os.environ['CFLAGS'] = '-fno-var-tracking-assignments'

    cmdclass_ = {'antlr': AntlrBuildCommand}
    cmdclass_.update(versioneer.get_cmdclass())

    metadata = dict(
        version=versioneer.get_version(),
        name='pymola',
        maintainer="James Goppert",
        maintainer_email="james.goppert@gmail.com",
        description=DOCLINES[0],
        long_description="\n".join(DOCLINES[2:]),
        url='https://github.com/pymola/pymola',
        author='James Goppert',
        author_email='james.goppert@gmail.com',
        download_url='https://github.com/pymola/pymola',
        license='BSD',
        classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
        platforms=["Windows", "Linux", "Solaris", "Mac OS-X", "Unix"],
        install_requires=install_reqs,
        tests_require=['coverage >= 3.7.1', 'nose >= 1.3.1'],
        test_suite='nose.collector',
        python_requires='>=3.5',
        packages=find_packages("src"),
        package_dir={"": "src"},
        cmdclass=cmdclass_
    )

    try:
        setup(**metadata)
    finally:
        del sys.path[0]
        os.chdir(old_path)
    return

if __name__ == '__main__':
    setup_package()

@@ -236,37 +236,8 @@
 tion
-%0Afrom setuptools import setup
 %0A%0Aim
@@ -276,51 +276,8 @@
 cess
-%0Aimport pprint%0Aimport shutil%0Aimport fnmatch
 %0A%0Afr
@@ -337,236 +337,43 @@
 ools
-.command.build_py import build_py%0Afrom setuptools.command.build_ext import build_ext%0Afrom setuptools import Command%0A%0Aimport versioneer%0A%0AMAJOR = 0%0AMINOR = 1%0AMICRO = 5%0AISRELEASED = True%0AVERSION = '%25d.%25d.%25d' %25 (MAJOR, MINOR, MICRO)
+ import Command%0A%0Aimport versioneer%0A
 %0ADOC
@@ -1262,27 +1262,16 @@
 thods%0A%0A%0A
-import sys%0A
 python_v
@@ -2522,785 +2522,106 @@
     
-src_path = os.path.dirname(os.path.abspath(sys.argv%5B0%5D))%0A    old_path = os.getcwd()%0A    os.chdir(src_path)%0A    sys.path.insert(0, src_path)%0A%0A    # Install requirements%0A    with open('requirements.txt', 'r') as req_file:%0A        install_reqs = req_file.read().split('%5Cn')%0A    # pprint.pprint(install_reqs)%0A    %0A    if sys.platform == 'darwin':%0A        # Disable compiler optimization.  We have to do this, as the default -O3 triggers a bug in clang causing an initialization failure.%0A        #os.environ%5B'CFLAGS'%5D = '-O0'%0A        # Or alternatively, use gcc:%0A        os.environ%5B'CC'%5D = 'gcc-7'%0A%0A    # Without disabling this, it will reach a limit%0A    # on tracking and disable tracking and then recompile, which is slow%0A    os.environ%5B'CFLAGS'%5D = '-fno-var-tracking-assignments'
+with open('requirements.txt', 'r') as req_file:%0A        install_reqs = req_file.read().split('%5Cn')
 %0A%0A  
@@ -2720,23 +2720,13 @@
     
-metadata = dict
+setup
 (%0A  
@@ -3585,118 +3585,8 @@
  )%0A%0A
-    try:%0A        setup(**metadata)%0A    finally:%0A        del sys.path%5B0%5D%0A        os.chdir(old_path)%0A    return%0A
 %0Aif 

Remove obsolete code in setup.py