setup.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import print_function
  5. from setuptools import setup, find_packages, Command
  6. from setuptools.command.sdist import sdist
  7. from setuptools.command.build_py import build_py
  8. from setuptools.command.egg_info import egg_info
  9. from setuptools.command.bdist_egg import bdist_egg
  10. from subprocess import check_call
  11. import os
  12. import sys
  13. import platform
  14. import shutil
  15. here = os.path.dirname(os.path.abspath(__file__))
  16. extension_root = os.path.join(here, 'jupyterlab')
  17. is_repo = os.path.exists(os.path.join(here, '.git'))
  18. def run(cmd, cwd=None):
  19. """Run a command
  20. >>> run('npm install', cwd='./subdir')
  21. """
  22. # On Windows, shell should be True so that the path is searched for the command.
  23. shell = (sys.platform == 'win32')
  24. check_call(cmd.split(), shell=shell, cwd=cwd, stdout=sys.stdout, stderr=sys.stderr)
  25. from distutils import log
  26. log.set_verbosity(log.DEBUG)
  27. log.info('setup.py entered')
  28. DESCRIPTION = 'An alpha preview of the JupyterLab notebook server extension.'
  29. LONG_DESCRIPTION = 'This is an alpha preview of JupyterLab. It is not ready for general usage yet. Development happens on https://github.com/jupyter/jupyterlab, with chat on https://gitter.im/jupyter/jupyterlab.'
  30. def js_prerelease(command, strict=False):
  31. """decorator for building minified js/css prior to another command"""
  32. class DecoratedCommand(command):
  33. def run(self):
  34. jsdeps = self.distribution.get_command_obj('jsdeps')
  35. if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
  36. # sdist, nothing to do
  37. command.run(self)
  38. return
  39. try:
  40. self.distribution.run_command('jsdeps')
  41. except Exception as e:
  42. missing = [t for t in jsdeps.targets if not os.path.exists(t)]
  43. if strict or missing:
  44. log.warn('rebuilding js and css failed')
  45. if missing:
  46. log.error('missing files: %s' % missing)
  47. raise e
  48. else:
  49. log.warn('rebuilding js and css failed (not a problem)')
  50. log.warn(str(e))
  51. command.run(self)
  52. return DecoratedCommand
  53. def update_package_data(distribution):
  54. """update build_py options to get package_data changes"""
  55. build_py = distribution.get_command_obj('build_py')
  56. build_py.finalize_options()
  57. class bdist_egg_disabled(bdist_egg):
  58. """Disabled version of bdist_egg
  59. Prevents setup.py install performing setuptools' default easy_install,
  60. which it should never ever do.
  61. """
  62. def run(self):
  63. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")
  64. class NPM(Command):
  65. description = 'install package.json dependencies using npm'
  66. user_options = []
  67. # Representative files that should exist after a successful build
  68. targets = [
  69. os.path.join(here, 'jupyterlab', 'build', 'main.css'),
  70. os.path.join(here, 'jupyterlab', 'build', 'main.bundle.js'),
  71. ]
  72. def initialize_options(self):
  73. pass
  74. def finalize_options(self):
  75. pass
  76. def has_npm(self):
  77. try:
  78. run('npm --version')
  79. return True
  80. except:
  81. return False
  82. def run(self):
  83. has_npm = self.has_npm()
  84. if not has_npm:
  85. log.error("`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo")
  86. log.info("Installing build dependencies with npm. This may take a while...")
  87. run('npm install', cwd=here)
  88. run('npm run build:serverextension', cwd=here)
  89. for t in self.targets:
  90. if not os.path.exists(t):
  91. msg = 'Missing file: %s' % t
  92. if not has_npm:
  93. msg += '\nnpm is required to build the development version'
  94. raise ValueError(msg)
  95. # update package data in case this created new files
  96. update_package_data(self.distribution)
  97. import json
  98. with open(os.path.join(here, 'package.json')) as f:
  99. packagejson = json.load(f)
  100. setup_args = {
  101. 'name': 'jupyterlab',
  102. 'version': packagejson['version'],
  103. 'description': DESCRIPTION,
  104. 'long_description': LONG_DESCRIPTION,
  105. 'License': 'BSD',
  106. 'include_package_data': True,
  107. 'install_requires': ['notebook>=4.2.0'],
  108. 'packages': find_packages(),
  109. 'zip_safe': False,
  110. 'package_data': {'jupyterlab': [
  111. 'build/*',
  112. 'lab.html'
  113. ]},
  114. 'cmdclass': {
  115. 'build_py': js_prerelease(build_py),
  116. 'egg_info': js_prerelease(egg_info),
  117. 'sdist': js_prerelease(sdist, strict=True),
  118. 'jsdeps': NPM,
  119. 'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled,
  120. },
  121. 'entry_points': {
  122. 'console_scripts': [
  123. 'jupyter-lab = jupyterlab.labapp:main',
  124. 'jupyter-labextension = jupyterlab.labextensions:main',
  125. ]
  126. },
  127. 'author': 'Jupyter Development Team',
  128. 'author_email': 'jupyter@googlegroups.com',
  129. 'url': 'https://github.com/jupyter/jupyterlab',
  130. 'keywords': ['ipython', 'jupyter', 'Web'],
  131. 'classifiers': [
  132. 'Development Status :: 2 - Pre-Alpha',
  133. 'Intended Audience :: Developers',
  134. 'Intended Audience :: Science/Research',
  135. 'License :: OSI Approved :: BSD License',
  136. 'Programming Language :: Python :: 2',
  137. 'Programming Language :: Python :: 2.7',
  138. 'Programming Language :: Python :: 3',
  139. ],
  140. }
  141. setup(**setup_args)