setupbase.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 glob
  14. import pipes
  15. import sys
  16. import shutil
  17. import os.path as osp
  18. from os.path import join as pjoin
  19. from distutils import log
  20. from distutils.cmd import Command
  21. from distutils.version import LooseVersion
  22. from setuptools.command.bdist_egg import bdist_egg
  23. from subprocess import check_call
  24. if sys.platform == 'win32':
  25. from subprocess import list2cmdline
  26. else:
  27. def list2cmdline(cmd_list):
  28. return ' '.join(map(pipes.quote, cmd_list))
  29. # the name of the project
  30. name = 'jupyterlab'
  31. here = osp.dirname(osp.abspath(__file__))
  32. is_repo = osp.exists(pjoin(here, '.git'))
  33. version_ns = {}
  34. with io.open(pjoin(here, name, '_version.py'), encoding="utf8") as f:
  35. exec(f.read(), {}, version_ns)
  36. def run(cmd, *args, **kwargs):
  37. """Echo a command before running it"""
  38. log.info('> ' + list2cmdline(cmd))
  39. kwargs['shell'] = (sys.platform == 'win32')
  40. return check_call(cmd, *args, **kwargs)
  41. #---------------------------------------------------------------------------
  42. # Find packages
  43. #---------------------------------------------------------------------------
  44. def find_packages():
  45. """
  46. Find all of the packages.
  47. """
  48. packages = []
  49. for dir, subdirs, files in os.walk('jupyterlab'):
  50. package = dir.replace(osp.sep, '.')
  51. if '__init__.py' not in files:
  52. # not a package
  53. continue
  54. packages.append(package)
  55. return packages
  56. #---------------------------------------------------------------------------
  57. # Find package data
  58. #---------------------------------------------------------------------------
  59. def find_package_data():
  60. """
  61. Find package_data.
  62. """
  63. return {
  64. 'jupyterlab': ['build/*', 'schemas/*', 'themes/*', 'index.app.js',
  65. 'webpack.config.js', 'package.app.json',
  66. 'released_packages.txt', 'node-version-check.js']
  67. }
  68. def ensure_core_data(command):
  69. """decorator for building minified js/css prior to another command"""
  70. class DecoratedCommand(command):
  71. def run(self):
  72. coredeps = self.distribution.get_command_obj('coredeps')
  73. if not is_repo and all(osp.exists(t) for t in coredeps.targets):
  74. # build_py or build_ext, nothing to do
  75. command.run(self)
  76. return
  77. try:
  78. self.distribution.run_command('coredeps')
  79. except Exception as e:
  80. missing = [t for t in coredeps.targets if not osp.exists(t)]
  81. if missing:
  82. log.warn('file check failed')
  83. if missing:
  84. log.error('missing files: %s' % missing)
  85. raise e
  86. else:
  87. log.warn('core deps check failed (not a problem)')
  88. log.warn(str(e))
  89. command.run(self)
  90. return DecoratedCommand
  91. def js_prerelease(command, strict=False):
  92. """decorator for building minified js/css prior to another command"""
  93. class DecoratedCommand(command):
  94. def run(self):
  95. jsdeps = self.distribution.get_command_obj('jsdeps')
  96. if not is_repo and all(osp.exists(t) for t in jsdeps.targets):
  97. # sdist, nothing to do
  98. command.run(self)
  99. return
  100. try:
  101. self.distribution.run_command('jsdeps')
  102. except Exception as e:
  103. missing = [t for t in jsdeps.targets if not osp.exists(t)]
  104. if strict or missing:
  105. log.warn('js check failed')
  106. if missing:
  107. log.error('missing files: %s' % missing)
  108. raise e
  109. else:
  110. log.warn('js check failed (not a problem)')
  111. log.warn(str(e))
  112. command.run(self)
  113. return DecoratedCommand
  114. def update_package_data(distribution):
  115. """update build_py options to get package_data changes"""
  116. build_py = distribution.get_command_obj('build_py')
  117. build_py.finalize_options()
  118. class CoreDeps(Command):
  119. description = 'ensure required core assets'
  120. user_options = []
  121. # Representative files that should exist after a successful core setup
  122. targets = [
  123. pjoin(here, 'jupyterlab', 'schemas', 'jupyter.extensions.shortcuts.json'),
  124. pjoin(here, 'jupyterlab', 'themes', 'jupyterlab-theme-light-extension',
  125. 'images', 'jupyterlab.svg')
  126. ]
  127. def initialize_options(self):
  128. pass
  129. def finalize_options(self):
  130. pass
  131. def run(self):
  132. # Handle schemas.
  133. schema = pjoin(here, 'jupyterlab', 'schemas')
  134. if osp.exists(schema):
  135. shutil.rmtree(schema)
  136. os.makedirs(schema)
  137. packages = glob.glob(pjoin(here, 'packages', '**', 'package.json'))
  138. for pkg in packages:
  139. with open(pkg) as fid:
  140. data = json.load(fid)
  141. if 'jupyterlab' not in data:
  142. continue
  143. schemaDir = data['jupyterlab'].get('schemaDir', '')
  144. if schemaDir:
  145. parentDir = osp.dirname(pkg)
  146. files = glob.glob(pjoin(parentDir, schemaDir, '*.json'))
  147. for file in files:
  148. shutil.copy(file, schema)
  149. # Handle themes
  150. themes = pjoin(here, 'jupyterlab', 'themes')
  151. if osp.exists(themes):
  152. shutil.rmtree(themes)
  153. os.makedirs(themes)
  154. packages = glob.glob(pjoin(here, 'packages', '**', 'package.json'))
  155. for pkg in packages:
  156. with open(pkg) as fid:
  157. data = json.load(fid)
  158. if 'jupyterlab' not in data:
  159. continue
  160. themeDir = data['jupyterlab'].get('themeDir', '')
  161. if themeDir:
  162. name = data['name'].replace('@', '').replace('/', '-')
  163. src = pjoin(osp.dirname(pkg), themeDir)
  164. shutil.copytree(src, pjoin(themes, name))
  165. # update package data in case this created new files
  166. update_package_data(self.distribution)
  167. class CheckAssets(Command):
  168. description = 'check for required assets'
  169. user_options = []
  170. # Representative files that should exist after a successful build
  171. targets = [
  172. pjoin(here, 'jupyterlab', 'build', 'release_data.json'),
  173. pjoin(here, 'jupyterlab', 'build', 'main.bundle.js'),
  174. ] + CoreDeps.targets
  175. def initialize_options(self):
  176. pass
  177. def finalize_options(self):
  178. pass
  179. def run(self):
  180. for t in self.targets:
  181. if not osp.exists(t):
  182. msg = 'Missing file: %s' % t
  183. raise ValueError(msg)
  184. target = pjoin(here, 'jupyterlab', 'build', 'release_data.json')
  185. with open(target) as fid:
  186. data = json.load(fid)
  187. if (LooseVersion(data['version']) !=
  188. LooseVersion(version_ns['__version__'])):
  189. msg = 'Release assets version mismatch, please run npm publish'
  190. raise ValueError(msg)
  191. # update package data in case this created new files
  192. update_package_data(self.distribution)
  193. class bdist_egg_disabled(bdist_egg):
  194. """Disabled version of bdist_egg
  195. Prevents setup.py install performing setuptools' default easy_install,
  196. which it should never ever do.
  197. """
  198. def run(self):
  199. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")