import os

from setuptools import setup, find_packages
from setuptools.command.develop import develop
from setuptools.command.install import install


def delete_old_slimit_files():
    """
    slimit generates two files with code generation lextab.py and yacctab.py
    slimit install ships with out of date versions of these files that have been created by ply 3.4
    To use ply 3.8 with slimit 0.8.1 you have to delete thses two files, they will be lazily recreated by slimit using the up to date ply
    """

    import slimit
    file_names = ["lextab.py", "yacctab.py"]
    dir = os.path.dirname(slimit.__file__)
    for file_name in file_names:
        file_path = os.path.join(dir, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)


class PostDevelopCommand(develop):
    """Post-installation for development mode."""
    def finalize_options(self):
        delete_old_slimit_files()
        develop.finalize_options(self)


class finalize_options(install):
    """Post-installation for installation mode."""
    def run(self):
        delete_old_slimit_files()
        install.finalize_options(self)


setup(name='fam',
    version='2.0.0',
    description="Simple Python ORM for CouchDB, and Sync Gateway",
    url="https://github.com/paulharter/fam",
    classifiers=[
      'Development Status :: 4 - Beta',
      'Natural Language :: English',
      'Operating System :: OS Independent',
      'Programming Language :: Python :: 3.6',
      'License :: OSI Approved :: MIT License'
    ],
    author='Paul Harter',
    author_email='paul@glowinthedark.co.uk',
    license="LICENSE",
    install_requires=['js2py', 'requests', 'simplejson', 'jsonschema', 'mock', 'pytz', 'slimit', 'ply==3.8',
                    'firebase_admin'],
    packages=find_packages('src'),
    package_dir={'': 'src'},
    include_package_data=True,
    zip_safe=False,
    cmdclass={
      'develop': PostDevelopCommand,
      'install': PostInstallCommand,
    }
)

import os

from setuptools import setup, find_packages
from setuptools.command.develop import develop
from setuptools.command.install import install


def delete_old_slimit_files():
    """
    slimit generates two files with code generation lextab.py and yacctab.py
    slimit install ships with out of date versions of these files that have been created by ply 3.4
    To use ply 3.8 with slimit 0.8.1 you have to delete thses two files, they will be lazily recreated by slimit using the up to date ply
    """

    import slimit
    file_names = ["lextab.py", "yacctab.py"]
    dir = os.path.dirname(slimit.__file__)
    for file_name in file_names:
        file_path = os.path.join(dir, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)


class PostDevelopCommand(develop):
    """Post-installation for development mode."""
    def finalize_options(self):
        delete_old_slimit_files()
        develop.finalize_options(self)


class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def finalize_options(self):
        delete_old_slimit_files()
        install.finalize_options(self)


setup(name='fam',
    version='2.0.0',
    description="Simple Python ORM for CouchDB, and Sync Gateway",
    url="https://github.com/paulharter/fam",
    classifiers=[
      'Development Status :: 4 - Beta',
      'Natural Language :: English',
      'Operating System :: OS Independent',
      'Programming Language :: Python :: 3.6',
      'License :: OSI Approved :: MIT License'
    ],
    author='Paul Harter',
    author_email='paul@glowinthedark.co.uk',
    license="LICENSE",
    install_requires=['js2py', 'requests', 'simplejson', 'jsonschema', 'mock', 'pytz', 'slimit', 'ply==3.8',
                    'firebase_admin'],
    packages=find_packages('src'),
    package_dir={'': 'src'},
    include_package_data=True,
    zip_safe=False,
    cmdclass={
      'develop': PostDevelopCommand,
      'install': PostInstallCommand,
    }
)

fix for broken slimit/ply again