setupbase.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/*', 'index.template.js', 'webpack.config.js',
  53. 'package.template.json']
  54. }
  55. def js_prerelease(command, strict=False):
  56. """decorator for building minified js/css prior to another command"""
  57. class DecoratedCommand(command):
  58. def run(self):
  59. jsdeps = self.distribution.get_command_obj('jsdeps')
  60. if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
  61. # sdist, nothing to do
  62. command.run(self)
  63. return
  64. try:
  65. self.distribution.run_command('jsdeps')
  66. except Exception as e:
  67. missing = [t for t in jsdeps.targets if not os.path.exists(t)]
  68. if strict or missing:
  69. log.warn('js check failed')
  70. if missing:
  71. log.error('missing files: %s' % missing)
  72. raise e
  73. else:
  74. log.warn('js check failed (not a problem)')
  75. log.warn(str(e))
  76. command.run(self)
  77. return DecoratedCommand
  78. def update_package_data(distribution):
  79. """update build_py options to get package_data changes"""
  80. build_py = distribution.get_command_obj('build_py')
  81. build_py.finalize_options()
  82. class CheckAssets(Command):
  83. description = 'check for required assets'
  84. user_options = []
  85. # Representative files that should exist after a successful build
  86. targets = [
  87. os.path.join(here, 'jupyterlab', 'build', 'release_data.json'),
  88. os.path.join(here, 'jupyterlab', 'build', 'main.bundle.js'),
  89. ]
  90. def initialize_options(self):
  91. pass
  92. def finalize_options(self):
  93. pass
  94. def run(self):
  95. for t in self.targets:
  96. if not os.path.exists(t):
  97. msg = 'Missing file: %s' % t
  98. raise ValueError(msg)
  99. # update package data in case this created new files
  100. update_package_data(self.distribution)
  101. class bdist_egg_disabled(bdist_egg):
  102. """Disabled version of bdist_egg
  103. Prevents setup.py install performing setuptools' default easy_install,
  104. which it should never ever do.
  105. """
  106. def run(self):
  107. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")