setupbase.py 7.0 KB

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