import os
import subprocess
import shutil

from setuptools import setup, findall, Command
from setuptools.command.sdist import sdist as Sdist


class QxBuildCommand(Command):

  description = 'build the frontend Qooxdoo application'


  def initialize_options(self):
    pass

  def finalize_options(self):
    pass

  def run(self):
    output = subprocess.check_output('python --version', shell = True, stderr = subprocess.STDOUT)
    assert output.decode().startswith('Python 2.7'), 'Qooxdoo SDK requires Python 2'

    assert os.path.exists('/usr/share/javascript/qooxdoo/qooxdoo-5.0.2-sdk'), \
      'Qooxdoo SDK 5.0.2 is not installed'

    assert os.path.exists('generate.py'), 'No Qooxdoo generator script found'

    shutil.rmtree('chronologerui/static', ignore_errors = True)

    subprocess.run('python generate.py load-library', shell = True)
    subprocess.run('python generate.py build', shell = True)

    shutil.move('build', 'chronologerui/static')


class CustomSdist(Sdist):

  def run(self):
    self.run_command('build_qx')
    super().run()


setup(
  name             = 'ChronologerUI',
  version          = '0.2.0',
  author           = 'saaj',
  author_email     = 'mail@saaj.me',
  license          = 'GPL-3.0',
  url              = 'https://bitbucket.org/saaj/chronologer/src/frontend',
  description      = 'Graphical user interface for the Python HTTP logging server',
  long_description = open('README.rst').read(),
  platforms        = ['Any'],
  packages         = ['chronologerui'],
  package_data     = {'chronologerui': [
    os.path.relpath(p, 'chronologerui') for p in findall('chronologerui/static')]},
  classifiers      = [
    'Framework :: CherryPy',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3 :: Only',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: Implementation :: CPython',
    'Intended Audience :: Developers'
  ],
  install_requires = ['cherrypy < 9'],
  cmdclass         = {
    'build_qx' : QxBuildCommand,
    'sdist'    : CustomSdist
  },
)

