setup.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. LONG_DESCRIPTION = 'This is a very early pre-alpha developer preview. It is not ready for general usage yet.'
  29. def js_prerelease(command, strict=False):
  30. """decorator for building minified js/css prior to another command"""
  31. class DecoratedCommand(command):
  32. def run(self):
  33. jsdeps = self.distribution.get_command_obj('jsdeps')
  34. if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
  35. # sdist, nothing to do
  36. command.run(self)
  37. return
  38. try:
  39. self.distribution.run_command('jsdeps')
  40. except Exception as e:
  41. missing = [t for t in jsdeps.targets if not os.path.exists(t)]
  42. if strict or missing:
  43. log.warn('rebuilding js and css failed')
  44. if missing:
  45. log.error('missing files: %s' % missing)
  46. raise e
  47. else:
  48. log.warn('rebuilding js and css failed (not a problem)')
  49. log.warn(str(e))
  50. command.run(self)
  51. return DecoratedCommand
  52. def update_package_data(distribution):
  53. """update build_py options to get package_data changes"""
  54. build_py = distribution.get_command_obj('build_py')
  55. build_py.finalize_options()
  56. class bdist_egg_disabled(bdist_egg):
  57. """Disabled version of bdist_egg
  58. Prevents setup.py install performing setuptools' default easy_install,
  59. which it should never ever do.
  60. """
  61. def run(self):
  62. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")
  63. class NPM(Command):
  64. description = 'install package.json dependencies using npm'
  65. user_options = []
  66. node_modules = os.path.join(here, 'node_modules')
  67. jlab_node_modules = os.path.join(extension_root, 'node_modules')
  68. # Representative files that should exist after a successful build
  69. targets = [
  70. os.path.join(here, 'jupyterlab', 'build', 'lab.css'),
  71. os.path.join(here, 'jupyterlab', 'build', 'lab.bundle.js'),
  72. ]
  73. def initialize_options(self):
  74. pass
  75. def finalize_options(self):
  76. pass
  77. def has_npm(self):
  78. try:
  79. run('npm --version')
  80. return True
  81. except:
  82. return False
  83. def run(self):
  84. has_npm = self.has_npm()
  85. if not has_npm:
  86. log.error("`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo")
  87. if not os.path.exists(self.node_modules):
  88. log.info("Installing build dependencies with npm. This may take a while...")
  89. run('npm install', cwd=here)
  90. if not os.path.exists(self.jlab_node_modules):
  91. log.info("Installing extension build dependencies with npm. This may take a while...")
  92. run('npm install', cwd=extension_root)
  93. run('npm run build:serverextension')
  94. for t in self.targets:
  95. if not os.path.exists(t):
  96. msg = 'Missing file: %s' % t
  97. if not has_npm:
  98. msg += '\nnpm is required to build the development version'
  99. raise ValueError(msg)
  100. # update package data in case this created new files
  101. update_package_data(self.distribution)
  102. import json
  103. with open(os.path.join(here, 'package.json')) as f:
  104. packagejson = json.load(f)
  105. setup_args = {
  106. 'name': 'jupyterlab',
  107. 'version': packagejson['version'],
  108. 'description': 'A pre-alpha Jupyter lab environment notebook server extension.',
  109. 'long_description': LONG_DESCRIPTION,
  110. 'License': 'BSD',
  111. 'include_package_data': True,
  112. 'install_requires': ['notebook>=4.2.0'],
  113. 'packages': find_packages(),
  114. 'zip_safe': False,
  115. 'package_data': {'jupyterlab': [
  116. 'build/*',
  117. 'lab.html'
  118. ]},
  119. 'cmdclass': {
  120. 'build_py': js_prerelease(build_py),
  121. 'egg_info': js_prerelease(egg_info),
  122. 'sdist': js_prerelease(sdist, strict=True),
  123. 'jsdeps': NPM,
  124. 'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled,
  125. },
  126. 'entry_points': {
  127. 'console_scripts': [
  128. 'jupyter-lab = jupyterlab.labapp:main',
  129. ]
  130. },
  131. 'author': 'Jupyter Development Team',
  132. 'author_email': 'jupyter@googlegroups.com',
  133. 'url': 'http://jupyter.org',
  134. 'keywords': ['ipython', 'jupyter', 'Web'],
  135. 'classifiers': [
  136. 'Development Status :: 2 - Pre-Alpha',
  137. 'Intended Audience :: Developers',
  138. 'Intended Audience :: Science/Research',
  139. 'License :: OSI Approved :: BSD License',
  140. 'Programming Language :: Python :: 2',
  141. 'Programming Language :: Python :: 2.7',
  142. 'Programming Language :: Python :: 3',
  143. ],
  144. }
  145. setup(**setup_args)