setup.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. node_modules = os.path.join(here, 'node_modules')
  68. jlab_node_modules = os.path.join(extension_root, 'node_modules')
  69. # Representative files that should exist after a successful build
  70. targets = [
  71. os.path.join(here, 'jupyterlab', 'build', 'jupyterlab.css'),
  72. os.path.join(here, 'jupyterlab', 'build', 'jupyterlab.bundle.js'),
  73. ]
  74. def initialize_options(self):
  75. pass
  76. def finalize_options(self):
  77. pass
  78. def has_npm(self):
  79. try:
  80. run('npm --version')
  81. return True
  82. except:
  83. return False
  84. def run(self):
  85. has_npm = self.has_npm()
  86. if not has_npm:
  87. log.error("`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo")
  88. if not os.path.exists(self.node_modules):
  89. log.info("Installing build dependencies with npm. This may take a while...")
  90. run('npm install', cwd=here)
  91. if not os.path.exists(self.jlab_node_modules):
  92. log.info("Installing extension build dependencies with npm. This may take a while...")
  93. run('npm install', cwd=extension_root)
  94. run('npm run build:all')
  95. for t in self.targets:
  96. if not os.path.exists(t):
  97. msg = 'Missing file: %s' % t
  98. if not has_npm:
  99. msg += '\nnpm is required to build the development version'
  100. raise ValueError(msg)
  101. # update package data in case this created new files
  102. update_package_data(self.distribution)
  103. import json
  104. with open(os.path.join(here, 'package.json')) as f:
  105. packagejson = json.load(f)
  106. setup_args = {
  107. 'name': 'jupyterlab',
  108. 'version': packagejson['version'],
  109. 'description': DESCRIPTION,
  110. 'long_description': LONG_DESCRIPTION,
  111. 'License': 'BSD',
  112. 'include_package_data': True,
  113. 'install_requires': ['notebook>=4.2.0'],
  114. 'packages': find_packages(),
  115. 'zip_safe': False,
  116. 'package_data': {'jupyterlab': [
  117. 'build/*',
  118. 'lab.html'
  119. ]},
  120. 'cmdclass': {
  121. 'build_py': js_prerelease(build_py),
  122. 'egg_info': js_prerelease(egg_info),
  123. 'sdist': js_prerelease(sdist, strict=True),
  124. 'jsdeps': NPM,
  125. 'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled,
  126. },
  127. 'entry_points': {
  128. 'console_scripts': [
  129. 'jupyter-lab = jupyterlab.labapp:main',
  130. ]
  131. },
  132. 'author': 'Jupyter Development Team',
  133. 'author_email': 'jupyter@googlegroups.com',
  134. 'url': 'https://github.com/jupyter/jupyterlab',
  135. 'keywords': ['ipython', 'jupyter', 'Web'],
  136. 'classifiers': [
  137. 'Development Status :: 2 - Pre-Alpha',
  138. 'Intended Audience :: Developers',
  139. 'Intended Audience :: Science/Research',
  140. 'License :: OSI Approved :: BSD License',
  141. 'Programming Language :: Python :: 2',
  142. 'Programming Language :: Python :: 2.7',
  143. 'Programming Language :: Python :: 3',
  144. ],
  145. }
  146. setup(**setup_args)