setupbase.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 io
  11. import json
  12. import os
  13. import pipes
  14. import sys
  15. from os.path import join as pjoin
  16. from distutils import log
  17. from distutils.cmd import Command
  18. from distutils.version import LooseVersion
  19. from setuptools.command.bdist_egg import bdist_egg
  20. from subprocess import check_call
  21. if sys.platform == 'win32':
  22. from subprocess import list2cmdline
  23. else:
  24. def list2cmdline(cmd_list):
  25. return ' '.join(map(pipes.quote, cmd_list))
  26. # the name of the project
  27. name = 'jupyterlab'
  28. here = os.path.dirname(os.path.abspath(__file__))
  29. is_repo = os.path.exists(pjoin(here, '.git'))
  30. version_ns = {}
  31. with io.open(pjoin(here, name, '_version.py'), encoding="utf8") as f:
  32. exec(f.read(), {}, version_ns)
  33. def run(cmd, *args, **kwargs):
  34. """Echo a command before running it"""
  35. log.info('> ' + list2cmdline(cmd))
  36. kwargs['shell'] = (sys.platform == 'win32')
  37. return check_call(cmd, *args, **kwargs)
  38. #---------------------------------------------------------------------------
  39. # Find packages
  40. #---------------------------------------------------------------------------
  41. def find_packages():
  42. """
  43. Find all of the packages.
  44. """
  45. packages = []
  46. for dir, subdirs, files in os.walk('jupyterlab'):
  47. package = dir.replace(os.path.sep, '.')
  48. if '__init__.py' not in files:
  49. # not a package
  50. continue
  51. packages.append(package)
  52. return packages
  53. #---------------------------------------------------------------------------
  54. # Find package data
  55. #---------------------------------------------------------------------------
  56. def find_package_data():
  57. """
  58. Find package_data.
  59. """
  60. return {
  61. 'jupyterlab': ['build/*', 'index.app.js', 'webpack.config.js',
  62. 'package.app.json', 'released_packages.txt']
  63. }
  64. def js_prerelease(command, strict=False):
  65. """decorator for building minified js/css prior to another command"""
  66. class DecoratedCommand(command):
  67. def run(self):
  68. jsdeps = self.distribution.get_command_obj('jsdeps')
  69. if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
  70. # sdist, nothing to do
  71. command.run(self)
  72. return
  73. try:
  74. self.distribution.run_command('jsdeps')
  75. except Exception as e:
  76. missing = [t for t in jsdeps.targets if not os.path.exists(t)]
  77. if strict or missing:
  78. log.warn('js check failed')
  79. if missing:
  80. log.error('missing files: %s' % missing)
  81. raise e
  82. else:
  83. log.warn('js check failed (not a problem)')
  84. log.warn(str(e))
  85. command.run(self)
  86. return DecoratedCommand
  87. def update_package_data(distribution):
  88. """update build_py options to get package_data changes"""
  89. build_py = distribution.get_command_obj('build_py')
  90. build_py.finalize_options()
  91. class CheckAssets(Command):
  92. description = 'check for required assets'
  93. user_options = []
  94. # Representative files that should exist after a successful build
  95. targets = [
  96. pjoin(here, 'jupyterlab', 'build', 'release_data.json'),
  97. pjoin(here, 'jupyterlab', 'build', 'main.bundle.js'),
  98. ]
  99. def initialize_options(self):
  100. pass
  101. def finalize_options(self):
  102. pass
  103. def run(self):
  104. for t in self.targets:
  105. if not os.path.exists(t):
  106. msg = 'Missing file: %s' % t
  107. raise ValueError(msg)
  108. target = pjoin(here, 'jupyterlab', 'build', 'release_data.json')
  109. with open(target) as fid:
  110. data = json.load(fid)
  111. if (LooseVersion(data['version']) !=
  112. LooseVersion(version_ns['__version__'])):
  113. msg = 'Release assets version mismatch, please run npm publish'
  114. raise ValueError(msg)
  115. # update package data in case this created new files
  116. update_package_data(self.distribution)
  117. class bdist_egg_disabled(bdist_egg):
  118. """Disabled version of bdist_egg
  119. Prevents setup.py install performing setuptools' default easy_install,
  120. which it should never ever do.
  121. """
  122. def run(self):
  123. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")