setupbase.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # encoding: utf-8
  2. """
  3. This module defines the things that are used in setup.py for building JupyterLab
  4. This includes:
  5. * Functions for finding things like packages, package data, etc.
  6. * A function for checking dependencies.
  7. """
  8. # Copyright (c) Jupyter Development Team.
  9. # Distributed under the terms of the Modified BSD License.
  10. import os
  11. import pipes
  12. import sys
  13. from distutils import log
  14. from distutils.cmd import Command
  15. from setuptools.command.bdist_egg import bdist_egg
  16. from subprocess import check_call
  17. if sys.platform == 'win32':
  18. from subprocess import list2cmdline
  19. else:
  20. def list2cmdline(cmd_list):
  21. return ' '.join(map(pipes.quote, cmd_list))
  22. here = os.path.dirname(os.path.abspath(__file__))
  23. is_repo = os.path.exists(os.path.join(here, '.git'))
  24. def run(cmd, *args, **kwargs):
  25. """Echo a command before running it"""
  26. log.info('> ' + list2cmdline(cmd))
  27. kwargs['shell'] = (sys.platform == 'win32')
  28. return check_call(cmd, *args, **kwargs)
  29. #---------------------------------------------------------------------------
  30. # Find packages
  31. #---------------------------------------------------------------------------
  32. def find_packages():
  33. """
  34. Find all of the packages.
  35. """
  36. packages = []
  37. for dir, subdirs, files in os.walk('jupyterlab'):
  38. package = dir.replace(os.path.sep, '.')
  39. if '__init__.py' not in files:
  40. # not a package
  41. continue
  42. packages.append(package)
  43. return packages
  44. #---------------------------------------------------------------------------
  45. # Find package data
  46. #---------------------------------------------------------------------------
  47. def find_package_data():
  48. """
  49. Find package_data.
  50. """
  51. return {
  52. 'jupyterlab': ['build/*', 'lab.html']
  53. }
  54. def js_prerelease(command, strict=False):
  55. """decorator for building minified js/css prior to another command"""
  56. class DecoratedCommand(command):
  57. def run(self):
  58. jsdeps = self.distribution.get_command_obj('jsdeps')
  59. if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
  60. # sdist, nothing to do
  61. command.run(self)
  62. return
  63. try:
  64. self.distribution.run_command('jsdeps')
  65. except Exception as e:
  66. missing = [t for t in jsdeps.targets if not os.path.exists(t)]
  67. if strict or missing:
  68. log.warn('rebuilding js and css failed')
  69. if missing:
  70. log.error('missing files: %s' % missing)
  71. raise e
  72. else:
  73. log.warn('rebuilding js and css failed (not a problem)')
  74. log.warn(str(e))
  75. command.run(self)
  76. return DecoratedCommand
  77. def update_package_data(distribution):
  78. """update build_py options to get package_data changes"""
  79. build_py = distribution.get_command_obj('build_py')
  80. build_py.finalize_options()
  81. class NPM(Command):
  82. description = 'install package.json dependencies using npm'
  83. user_options = []
  84. # Representative files that should exist after a successful build
  85. targets = [
  86. os.path.join(here, 'jupyterlab', 'build', 'main.css'),
  87. os.path.join(here, 'jupyterlab', 'build', 'main.bundle.js'),
  88. ]
  89. def initialize_options(self):
  90. pass
  91. def finalize_options(self):
  92. pass
  93. def has_npm(self):
  94. try:
  95. run(['npm', '--version'])
  96. return True
  97. except:
  98. return False
  99. def run(self):
  100. has_npm = self.has_npm()
  101. if not has_npm:
  102. log.error("`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo")
  103. log.info("Installing build dependencies with npm. This may take a while...")
  104. run(['npm', 'install'], cwd=here)
  105. run(['npm', 'run', 'clean'], cwd=here)
  106. run(['npm', 'run', 'build:all'], cwd=here)
  107. for t in self.targets:
  108. if not os.path.exists(t):
  109. msg = 'Missing file: %s' % t
  110. if not has_npm:
  111. msg += '\nnpm is required to build the development version'
  112. raise ValueError(msg)
  113. # update package data in case this created new files
  114. update_package_data(self.distribution)
  115. class bdist_egg_disabled(bdist_egg):
  116. """Disabled version of bdist_egg
  117. Prevents setup.py install performing setuptools' default easy_install,
  118. which it should never ever do.
  119. """
  120. def run(self):
  121. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")