from os.path import join, abspath, dirname, exists
from slybot import __version__
from setuptools import setup, find_packages
from setuptools.command.bdist_egg import bdist_egg
from setuptools.command.sdist import sdist


def build_js():
    root = abspath(dirname(__file__))
    base_path = abspath(join(root, '..', 'splash_utils'))
    if not exists(base_path):
        base_path = abspath(join(root, '..', 'slyd', 'splash_utils'))

    files = ('waitAsync.js', 'perform_actions.js')
    fdata = []
    for fname in files:
        with open(join(base_path, fname)) as f:
            fdata.append(f.read())
    js_file = abspath(join(root, 'slybot', 'splash-script-combined.js'))
    with open(js_file, 'w') as f:
        f.write(';(function(){\n%s\n})();' % '\n'.join(fdata))


class bdist_egg_command(bdist_egg):
    def run(self):
        build_js()
        bdist_egg.run(self)


class sdist_command(sdist):
    def run(self):
        build_js()
        sdist.run(self)



install_requires = ['Scrapy', 'scrapely', 'loginform', 'lxml', 'jsonschema',
                    'dateparser', 'scrapyjs', 'page_finder', 'six']
extras = {
    'tests': ['nose', 'nose-timer'],
    'clustering': ['page_clustering']
}

setup(name='slybot',
      version=__version__,
      license='BSD',
      description='Slybot crawler',
      author='Scrapy project',
      author_email='info@scrapy.org',
      url='http://github.com/scrapinghub/portia',
      packages=find_packages(exclude=('tests', 'tests.*')),
      platforms=['Any'],
      scripts=['bin/slybot', 'bin/portiacrawl'],
      install_requires=install_requires,
      extras_require=extras,
      package_data={'': ['slybot/splash-script-combined.js']},
      include_package_data=True,

      cmdclass={
          'bdist_egg': bdist_egg_command,
          'sdist': sdist_command
      },

      classifiers=[
          'Development Status :: 4 - Beta',
          'License :: OSI Approved :: BSD License',
          'Operating System :: OS Independent',
          'Programming Language :: Python',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 2.7'
      ])

[((9, 19, 9, 36), 'os.path.dirname', 'dirname', ({(9, 27, 9, 35): '__file__'}, {}), '(__file__)', False, 'from os.path import join, abspath, dirname, exists\n'), ((10, 24, 10, 56), 'os.path.join', 'join', ({(10, 29, 10, 33): 'root', (10, 35, 10, 39): '""".."""', (10, 41, 10, 55): '"""splash_utils"""'}, {}), "(root, '..', 'splash_utils')", False, 'from os.path import join, abspath, dirname, exists\n'), ((11, 11, 11, 28), 'os.path.exists', 'exists', ({(11, 18, 11, 27): 'base_path'}, {}), '(base_path)', False, 'from os.path import join, abspath, dirname, exists\n'), ((19, 22, 19, 71), 'os.path.join', 'join', ({(19, 27, 19, 31): 'root', (19, 33, 19, 41): '"""slybot"""', (19, 43, 19, 70): '"""splash-script-combined.js"""'}, {}), "(root, 'slybot', 'splash-script-combined.js')", False, 'from os.path import join, abspath, dirname, exists\n'), ((27, 8, 27, 27), 'setuptools.command.bdist_egg.bdist_egg.run', 'bdist_egg.run', ({(27, 22, 27, 26): 'self'}, {}), '(self)', False, 'from setuptools.command.bdist_egg import bdist_egg\n'), ((33, 8, 33, 23), 'setuptools.command.sdist.sdist.run', 'sdist.run', ({(33, 18, 33, 22): 'self'}, {}), '(self)', False, 'from setuptools.command.sdist import sdist\n'), ((51, 15, 51, 58), 'setuptools.find_packages', 'find_packages', (), '', False, 'from setuptools import setup, find_packages\n'), ((12, 28, 12, 68), 'os.path.join', 'join', ({(12, 33, 12, 37): 'root', (12, 39, 12, 43): '""".."""', (12, 45, 12, 51): '"""slyd"""', (12, 53, 12, 67): '"""splash_utils"""'}, {}), "(root, '..', 'slyd', 'splash_utils')", False, 'from os.path import join, abspath, dirname, exists\n'), ((17, 18, 17, 40), 'os.path.join', 'join', ({(17, 23, 17, 32): 'base_path', (17, 34, 17, 39): 'fname'}, {}), '(base_path, fname)', False, 'from os.path import join, abspath, dirname, exists\n')]