setup.py 5.0 KB

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