setup.py 5.2 KB

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