setupbase.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.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. package = dir.replace(osp.sep, '.')
  52. if '__init__.py' not in files:
  53. # not a package
  54. continue
  55. packages.append(package)
  56. return packages
  57. #---------------------------------------------------------------------------
  58. # Find package data
  59. #---------------------------------------------------------------------------
  60. def find_package_data():
  61. """
  62. Find package_data.
  63. """
  64. theme_dirs = []
  65. for dir, subdirs, files in os.walk(pjoin('jupyterlab', 'themes')):
  66. slice_len = len('jupyterlab' + os.sep)
  67. theme_dirs.append(pjoin(dir[slice_len:], '*'))
  68. schema_dirs = []
  69. for dir, subdirs, files in os.walk(pjoin('jupyterlab', 'schemas')):
  70. slice_len = len('jupyterlab' + os.sep)
  71. schema_dirs.append(pjoin(dir[slice_len:], '*'))
  72. return {
  73. 'jupyterlab': ['build/*', 'index.app.js',
  74. 'webpack.config.js', 'package.app.json',
  75. 'released_packages.txt', 'node-version-check.js'
  76. ] + theme_dirs + schema_dirs
  77. }
  78. def find_data_files():
  79. """
  80. Find data_files.
  81. """
  82. if not os.path.exists(pjoin('jupyterlab', 'build')):
  83. return []
  84. files = []
  85. static_files = os.listdir(pjoin('jupyterlab', 'build'))
  86. files.append(('share/jupyter/lab/static',
  87. ['jupyterlab/build/%s' % f for f in static_files]))
  88. for dir, subdirs, fnames in os.walk(pjoin('jupyterlab', 'schemas')):
  89. dir = dir.replace(os.sep, '/')
  90. schema_files = []
  91. for fname in fnames:
  92. schema_files.append('%s/%s' % (dir, fname))
  93. slice_len = len('jupyterlab/')
  94. files.append(('share/jupyter/lab/%s' % dir[slice_len:], schema_files))
  95. for dir, subdirs, fnames in os.walk(pjoin('jupyterlab', 'themes')):
  96. dir = dir.replace(os.sep, '/')
  97. themes_files = []
  98. for fname in fnames:
  99. themes_files.append('%s/%s' % (dir, fname))
  100. slice_len = len('jupyterlab/')
  101. files.append(('share/jupyter/lab/%s' % dir[slice_len:], themes_files))
  102. return files
  103. def js_prerelease(command, strict=False):
  104. """decorator for building minified js/css prior to another command"""
  105. class DecoratedCommand(command):
  106. def run(self):
  107. jsdeps = self.distribution.get_command_obj('jsdeps')
  108. if not is_repo and all(osp.exists(t) for t in jsdeps.targets):
  109. # sdist, nothing to do
  110. command.run(self)
  111. return
  112. try:
  113. self.distribution.run_command('jsdeps')
  114. except Exception as e:
  115. missing = [t for t in jsdeps.targets if not osp.exists(t)]
  116. if strict or missing:
  117. log.warn('js check failed')
  118. if missing:
  119. log.error('missing files: %s' % missing)
  120. raise e
  121. else:
  122. log.warn('js check failed (not a problem)')
  123. log.warn(str(e))
  124. command.run(self)
  125. return DecoratedCommand
  126. def update_package_data(distribution):
  127. """update build_py options to get package_data changes"""
  128. build_py = distribution.get_command_obj('build_py')
  129. build_py.finalize_options()
  130. class CheckAssets(Command):
  131. description = 'check for required assets'
  132. user_options = []
  133. # Representative files that should exist after a successful build
  134. targets = [
  135. pjoin(here, 'jupyterlab', 'build', 'release_data.json'),
  136. pjoin(here, 'jupyterlab', 'build', 'main.bundle.js'),
  137. pjoin(here, 'jupyterlab', 'schemas', '@jupyterlab',
  138. 'shortcuts-extension', 'plugin.json'),
  139. pjoin(here, 'jupyterlab', 'themes', '@jupyterlab',
  140. 'theme-light-extension',
  141. 'images', 'jupyterlab.svg')
  142. ]
  143. def initialize_options(self):
  144. pass
  145. def finalize_options(self):
  146. pass
  147. def run(self):
  148. for t in self.targets:
  149. if not osp.exists(t):
  150. msg = 'Missing file: %s' % t
  151. raise ValueError(msg)
  152. target = pjoin(here, 'jupyterlab', 'build', 'release_data.json')
  153. with open(target) as fid:
  154. data = json.load(fid)
  155. if (LooseVersion(data['version']) !=
  156. LooseVersion(version_ns['__version__'])):
  157. msg = 'Release assets version mismatch, please run npm publish'
  158. raise ValueError(msg)
  159. # update package data in case this created new files
  160. update_package_data(self.distribution)
  161. class bdist_egg_disabled(bdist_egg):
  162. """Disabled version of bdist_egg
  163. Prevents setup.py install performing setuptools' default easy_install,
  164. which it should never ever do.
  165. """
  166. def run(self):
  167. sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")
  168. class custom_egg_info(egg_info):
  169. """Prune node_modules folders from egg_info to avoid symlink recursion
  170. """
  171. def run(self):
  172. folders = glob.glob(pjoin(here, 'packages', '**', 'node_modules'))
  173. for folder in folders:
  174. shutil.rmtree(folder)
  175. return egg_info.run(self)