setupbase.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. """
  6. This file originates from the 'jupyter-packaging' package, and
  7. contains a set of useful utilities for including npm packages
  8. within a Python package.
  9. """
  10. from collections import defaultdict
  11. from os.path import join as pjoin
  12. from shutil import which
  13. import io
  14. import os
  15. import functools
  16. import pipes
  17. import re
  18. import shlex
  19. import subprocess
  20. import sys
  21. # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
  22. # update it when the contents of directories change.
  23. if os.path.exists('MANIFEST'): os.remove('MANIFEST') # noqa
  24. from distutils.cmd import Command
  25. from distutils.command.build_py import build_py
  26. from distutils.command.sdist import sdist
  27. from distutils import log
  28. from setuptools.command.develop import develop
  29. from setuptools.command.bdist_egg import bdist_egg
  30. try:
  31. from wheel.bdist_wheel import bdist_wheel
  32. except ImportError:
  33. bdist_wheel = None
  34. if sys.platform == 'win32':
  35. from subprocess import list2cmdline
  36. else:
  37. def list2cmdline(cmd_list):
  38. return ' '.join(map(pipes.quote, cmd_list))
  39. __version__ = '0.2.0'
  40. # ---------------------------------------------------------------------------
  41. # Top Level Variables
  42. # ---------------------------------------------------------------------------
  43. HERE = os.path.abspath(os.path.dirname(__file__))
  44. is_repo = os.path.exists(pjoin(HERE, '.git'))
  45. node_modules = pjoin(HERE, 'node_modules')
  46. SEPARATORS = os.sep if os.altsep is None else os.sep + os.altsep
  47. npm_path = ':'.join([
  48. pjoin(HERE, 'node_modules', '.bin'),
  49. os.environ.get('PATH', os.defpath),
  50. ])
  51. if "--skip-npm" in sys.argv:
  52. print("Skipping npm install as requested.")
  53. skip_npm = True
  54. sys.argv.remove("--skip-npm")
  55. else:
  56. skip_npm = False
  57. # ---------------------------------------------------------------------------
  58. # Public Functions
  59. # ---------------------------------------------------------------------------
  60. def get_version(file, name='__version__'):
  61. """Get the version of the package from the given file by
  62. executing it and extracting the given `name`.
  63. """
  64. path = os.path.realpath(file)
  65. version_ns = {}
  66. with io.open(path, encoding="utf8") as f:
  67. exec(f.read(), {}, version_ns)
  68. return version_ns[name]
  69. def find_packages(top=HERE):
  70. """
  71. Find all of the packages.
  72. """
  73. packages = []
  74. for d, dirs, _ in os.walk(top, followlinks=True):
  75. if os.path.exists(pjoin(d, '__init__.py')):
  76. packages.append(os.path.relpath(d, top).replace(os.path.sep, '.'))
  77. elif d != top:
  78. # Don't look for packages in subfolders if current isn't a package.
  79. dirs[:] = []
  80. return packages
  81. def update_package_data(distribution):
  82. """update build_py options to get package_data changes"""
  83. build_py = distribution.get_command_obj('build_py')
  84. build_py.finalize_options()
  85. class bdist_egg_disabled(bdist_egg):
  86. """Disabled version of bdist_egg
  87. Prevents setup.py install performing setuptools' default easy_install,
  88. which it should never ever do.
  89. """
  90. def run(self):
  91. sys.exit("Aborting implicit building of eggs. Use `pip install .` "
  92. " to install from source.")
  93. def create_cmdclass(prerelease_cmd=None, package_data_spec=None,
  94. data_files_spec=None, exclude=None):
  95. """Create a command class with the given optional prerelease class.
  96. Parameters
  97. ----------
  98. prerelease_cmd: (name, Command) tuple, optional
  99. The command to run before releasing.
  100. package_data_spec: dict, optional
  101. A dictionary whose keys are the dotted package names and
  102. whose values are a list of glob patterns.
  103. data_files_spec: list, optional
  104. A list of (path, dname, pattern) tuples where the path is the
  105. `data_files` install path, dname is the source directory, and the
  106. pattern is a glob pattern.
  107. exclude: function
  108. A function which takes a string filename and returns True if the
  109. file should be excluded from package data and data files, False otherwise.
  110. Notes
  111. -----
  112. We use specs so that we can find the files *after* the build
  113. command has run.
  114. The package data glob patterns should be relative paths from the package
  115. folder containing the __init__.py file, which is given as the package
  116. name.
  117. e.g. `dict(foo=['./bar/*', './baz/**'])`
  118. The data files directories should be absolute paths or relative paths
  119. from the root directory of the repository. Data files are specified
  120. differently from `package_data` because we need a separate path entry
  121. for each nested folder in `data_files`, and this makes it easier to
  122. parse.
  123. e.g. `('share/foo/bar', 'pkgname/bizz, '*')`
  124. """
  125. wrapped = [prerelease_cmd] if prerelease_cmd else []
  126. if package_data_spec or data_files_spec:
  127. wrapped.append('handle_files')
  128. wrapper = functools.partial(_wrap_command, wrapped)
  129. handle_files = _get_file_handler(package_data_spec, data_files_spec, exclude)
  130. if 'bdist_egg' in sys.argv:
  131. egg = wrapper(bdist_egg, strict=True)
  132. else:
  133. egg = bdist_egg_disabled
  134. cmdclass = dict(
  135. build_py=wrapper(build_py, strict=is_repo),
  136. bdist_egg=egg,
  137. sdist=wrapper(sdist, strict=True),
  138. handle_files=handle_files,
  139. )
  140. if bdist_wheel:
  141. cmdclass['bdist_wheel'] = wrapper(bdist_wheel, strict=True)
  142. cmdclass['develop'] = wrapper(develop, strict=True)
  143. return cmdclass
  144. def command_for_func(func):
  145. """Create a command that calls the given function."""
  146. class FuncCommand(BaseCommand):
  147. def run(self):
  148. func()
  149. update_package_data(self.distribution)
  150. return FuncCommand
  151. def run(cmd, **kwargs):
  152. """Echo a command before running it. Defaults to repo as cwd"""
  153. log.info('> ' + list2cmdline(cmd))
  154. kwargs.setdefault('cwd', HERE)
  155. kwargs.setdefault('shell', os.name == 'nt')
  156. if not isinstance(cmd, (list, tuple)) and os.name != 'nt':
  157. cmd = shlex.split(cmd)
  158. cmd[0] = which(cmd[0])
  159. return subprocess.check_call(cmd, **kwargs)
  160. def is_stale(target, source):
  161. """Test whether the target file/directory is stale based on the source
  162. file/directory.
  163. """
  164. if not os.path.exists(target):
  165. return True
  166. target_mtime = recursive_mtime(target) or 0
  167. return compare_recursive_mtime(source, cutoff=target_mtime)
  168. class BaseCommand(Command):
  169. """Empty command because Command needs subclasses to override too much"""
  170. user_options = []
  171. def initialize_options(self):
  172. pass
  173. def finalize_options(self):
  174. pass
  175. def get_inputs(self):
  176. return []
  177. def get_outputs(self):
  178. return []
  179. def combine_commands(*commands):
  180. """Return a Command that combines several commands."""
  181. class CombinedCommand(Command):
  182. user_options = []
  183. def initialize_options(self):
  184. self.commands = []
  185. for C in commands:
  186. self.commands.append(C(self.distribution))
  187. for c in self.commands:
  188. c.initialize_options()
  189. def finalize_options(self):
  190. for c in self.commands:
  191. c.finalize_options()
  192. def run(self):
  193. for c in self.commands:
  194. c.run()
  195. return CombinedCommand
  196. def compare_recursive_mtime(path, cutoff, newest=True):
  197. """Compare the newest/oldest mtime for all files in a directory.
  198. Cutoff should be another mtime to be compared against. If an mtime that is
  199. newer/older than the cutoff is found it will return True.
  200. E.g. if newest=True, and a file in path is newer than the cutoff, it will
  201. return True.
  202. """
  203. if os.path.isfile(path):
  204. mt = mtime(path)
  205. if newest:
  206. if mt > cutoff:
  207. return True
  208. elif mt < cutoff:
  209. return True
  210. for dirname, _, filenames in os.walk(path, topdown=False):
  211. for filename in filenames:
  212. mt = mtime(pjoin(dirname, filename))
  213. if newest: # Put outside of loop?
  214. if mt > cutoff:
  215. return True
  216. elif mt < cutoff:
  217. return True
  218. return False
  219. def recursive_mtime(path, newest=True):
  220. """Gets the newest/oldest mtime for all files in a directory."""
  221. if os.path.isfile(path):
  222. return mtime(path)
  223. current_extreme = None
  224. for dirname, dirnames, filenames in os.walk(path, topdown=False):
  225. for filename in filenames:
  226. mt = mtime(pjoin(dirname, filename))
  227. if newest: # Put outside of loop?
  228. if mt >= (current_extreme or mt):
  229. current_extreme = mt
  230. elif mt <= (current_extreme or mt):
  231. current_extreme = mt
  232. return current_extreme
  233. def mtime(path):
  234. """shorthand for mtime"""
  235. return os.stat(path).st_mtime
  236. def install_npm(path=None, build_dir=None, source_dir=None, build_cmd='build',
  237. force=False, npm=None):
  238. """Return a Command for managing an npm installation.
  239. Note: The command is skipped if the `--skip-npm` flag is used.
  240. Parameters
  241. ----------
  242. path: str, optional
  243. The base path of the node package. Defaults to the repo root.
  244. build_dir: str, optional
  245. The target build directory. If this and source_dir are given,
  246. the JavaScript will only be build if necessary.
  247. source_dir: str, optional
  248. The source code directory.
  249. build_cmd: str, optional
  250. The npm command to build assets to the build_dir.
  251. npm: str or list, optional.
  252. The npm executable name, or a tuple of ['node', executable].
  253. """
  254. class NPM(BaseCommand):
  255. description = 'install package.json dependencies using npm'
  256. def run(self):
  257. if skip_npm:
  258. log.info('Skipping npm-installation')
  259. return
  260. node_package = path or HERE
  261. node_modules = pjoin(node_package, 'node_modules')
  262. is_yarn = os.path.exists(pjoin(node_package, 'yarn.lock'))
  263. npm_cmd = npm
  264. if npm is None:
  265. if is_yarn:
  266. npm_cmd = ['yarn']
  267. else:
  268. npm_cmd = ['npm']
  269. if not which(npm_cmd[0]):
  270. log.error("`{0}` unavailable. If you're running this command "
  271. "using sudo, make sure `{0}` is available to sudo"
  272. .format(npm_cmd[0]))
  273. return
  274. stale_package = is_stale(node_modules,
  275. pjoin(node_package, 'package.json'))
  276. if force or stale_package:
  277. log.info('Installing build dependencies with npm. This may '
  278. 'take a while...')
  279. run(npm_cmd + ['install'], cwd=node_package)
  280. if build_dir and source_dir and not force:
  281. should_build = is_stale(build_dir, source_dir)
  282. else:
  283. should_build = True
  284. if should_build:
  285. run(npm_cmd + ['run', build_cmd], cwd=node_package)
  286. return NPM
  287. def ensure_targets(targets):
  288. """Return a Command that checks that certain files exist.
  289. Raises a ValueError if any of the files are missing.
  290. Note: The check is skipped if the `--skip-npm` flag is used.
  291. """
  292. class TargetsCheck(BaseCommand):
  293. def run(self):
  294. if skip_npm:
  295. log.info('Skipping target checks')
  296. return
  297. missing = [t for t in targets if not os.path.exists(t)]
  298. if missing:
  299. raise ValueError(('missing files: %s' % missing))
  300. return TargetsCheck
  301. # ---------------------------------------------------------------------------
  302. # Private Functions
  303. # ---------------------------------------------------------------------------
  304. def _wrap_command(cmds, cls, strict=True):
  305. """Wrap a setup command
  306. Parameters
  307. ----------
  308. cmds: list(str)
  309. The names of the other commands to run prior to the command.
  310. strict: boolean, optional
  311. Whether to raise errors when a pre-command fails.
  312. """
  313. class WrappedCommand(cls):
  314. def run(self):
  315. if not getattr(self, 'uninstall', None):
  316. try:
  317. [self.run_command(cmd) for cmd in cmds]
  318. except Exception:
  319. if strict:
  320. raise
  321. else:
  322. pass
  323. # update package data
  324. update_package_data(self.distribution)
  325. result = cls.run(self)
  326. return result
  327. return WrappedCommand
  328. def _get_file_handler(package_data_spec, data_files_spec, exclude=None):
  329. """Get a package_data and data_files handler command.
  330. """
  331. class FileHandler(BaseCommand):
  332. def run(self):
  333. package_data = self.distribution.package_data
  334. package_spec = package_data_spec or dict()
  335. for (key, patterns) in package_spec.items():
  336. files = _get_package_data(key, patterns)
  337. if exclude is not None:
  338. files = [f for f in files if not exclude(f)]
  339. package_data[key] = files
  340. self.distribution.data_files = _get_data_files(
  341. data_files_spec, self.distribution.data_files, exclude
  342. )
  343. return FileHandler
  344. def _get_data_files(data_specs, existing, exclude=None):
  345. """Expand data file specs into valid data files metadata.
  346. Parameters
  347. ----------
  348. data_specs: list of tuples
  349. See [createcmdclass] for description.
  350. existing: list of tuples
  351. The existing distribution data_files metadata.
  352. Returns
  353. -------
  354. A valid list of data_files items.
  355. """
  356. # Extract the existing data files into a staging object.
  357. file_data = defaultdict(list)
  358. for (path, files) in existing or []:
  359. file_data[path] = files
  360. # Extract the files and assign them to the proper data
  361. # files path.
  362. for (path, dname, pattern) in data_specs or []:
  363. dname = dname.replace(os.sep, '/')
  364. offset = len(dname) + 1
  365. files = _get_files(pjoin(dname, pattern))
  366. for fname in files:
  367. # Normalize the path.
  368. root = os.path.dirname(fname)
  369. full_path = '/'.join([path, root[offset:]])
  370. if full_path.endswith('/'):
  371. full_path = full_path[:-1]
  372. if exclude is not None and exclude(fname):
  373. continue
  374. file_data[full_path].append(fname)
  375. # Construct the data files spec.
  376. data_files = []
  377. for (path, files) in file_data.items():
  378. data_files.append((path, files))
  379. return data_files
  380. def _get_files(file_patterns, top=HERE):
  381. """Expand file patterns to a list of paths.
  382. Parameters
  383. -----------
  384. file_patterns: list or str
  385. A list of glob patterns for the data file locations.
  386. The globs can be recursive if they include a `**`.
  387. They should be relative paths from the top directory or
  388. absolute paths.
  389. top: str
  390. the directory to consider for data files
  391. Note:
  392. Files in `node_modules` are ignored.
  393. """
  394. if not isinstance(file_patterns, (list, tuple)):
  395. file_patterns = [file_patterns]
  396. for i, p in enumerate(file_patterns):
  397. if os.path.isabs(p):
  398. file_patterns[i] = os.path.relpath(p, top)
  399. matchers = [_compile_pattern(p) for p in file_patterns]
  400. files = set()
  401. for root, dirnames, filenames in os.walk(top):
  402. # Don't recurse into node_modules
  403. if 'node_modules' in dirnames:
  404. dirnames.remove('node_modules')
  405. for m in matchers:
  406. for filename in filenames:
  407. fn = os.path.relpath(pjoin(root, filename), top)
  408. if m(fn):
  409. files.add(fn.replace(os.sep, '/'))
  410. return list(files)
  411. def _get_package_data(root, file_patterns=None):
  412. """Expand file patterns to a list of `package_data` paths.
  413. Parameters
  414. -----------
  415. root: str
  416. The relative path to the package root from `HERE`.
  417. file_patterns: list or str, optional
  418. A list of glob patterns for the data file locations.
  419. The globs can be recursive if they include a `**`.
  420. They should be relative paths from the root or
  421. absolute paths. If not given, all files will be used.
  422. Note:
  423. Files in `node_modules` are ignored.
  424. """
  425. if file_patterns is None:
  426. file_patterns = ['*']
  427. return _get_files(file_patterns, pjoin(HERE, root))
  428. def _compile_pattern(pat, ignore_case=True):
  429. """Translate and compile a glob pattern to a regular expression matcher."""
  430. if isinstance(pat, bytes):
  431. pat_str = pat.decode('ISO-8859-1')
  432. res_str = _translate_glob(pat_str)
  433. res = res_str.encode('ISO-8859-1')
  434. else:
  435. res = _translate_glob(pat)
  436. flags = re.IGNORECASE if ignore_case else 0
  437. return re.compile(res, flags=flags).match
  438. def _iexplode_path(path):
  439. """Iterate over all the parts of a path.
  440. Splits path recursively with os.path.split().
  441. """
  442. (head, tail) = os.path.split(path)
  443. if not head or (not tail and head == path):
  444. if head:
  445. yield head
  446. if tail or not head:
  447. yield tail
  448. return
  449. for p in _iexplode_path(head):
  450. yield p
  451. yield tail
  452. def _translate_glob(pat):
  453. """Translate a glob PATTERN to a regular expression."""
  454. translated_parts = []
  455. for part in _iexplode_path(pat):
  456. translated_parts.append(_translate_glob_part(part))
  457. os_sep_class = '[%s]' % re.escape(SEPARATORS)
  458. res = _join_translated(translated_parts, os_sep_class)
  459. return '{res}\\Z(?ms)'.format(res=res)
  460. def _join_translated(translated_parts, os_sep_class):
  461. """Join translated glob pattern parts.
  462. This is different from a simple join, as care need to be taken
  463. to allow ** to match ZERO or more directories.
  464. """
  465. res = ''
  466. for part in translated_parts[:-1]:
  467. if part == '.*':
  468. # drop separator, since it is optional
  469. # (** matches ZERO or more dirs)
  470. res += part
  471. else:
  472. res += part + os_sep_class
  473. if translated_parts[-1] == '.*':
  474. # Final part is **
  475. res += '.+'
  476. # Follow stdlib/git convention of matching all sub files/directories:
  477. res += '({os_sep_class}?.*)?'.format(os_sep_class=os_sep_class)
  478. else:
  479. res += translated_parts[-1]
  480. return res
  481. def _translate_glob_part(pat):
  482. """Translate a glob PATTERN PART to a regular expression."""
  483. # Code modified from Python 3 standard lib fnmatch:
  484. if pat == '**':
  485. return '.*'
  486. i, n = 0, len(pat)
  487. res = []
  488. while i < n:
  489. c = pat[i]
  490. i = i + 1
  491. if c == '*':
  492. # Match anything but path separators:
  493. res.append('[^%s]*' % SEPARATORS)
  494. elif c == '?':
  495. res.append('[^%s]?' % SEPARATORS)
  496. elif c == '[':
  497. j = i
  498. if j < n and pat[j] == '!':
  499. j = j + 1
  500. if j < n and pat[j] == ']':
  501. j = j + 1
  502. while j < n and pat[j] != ']':
  503. j = j + 1
  504. if j >= n:
  505. res.append('\\[')
  506. else:
  507. stuff = pat[i:j].replace('\\', '\\\\')
  508. i = j + 1
  509. if stuff[0] == '!':
  510. stuff = '^' + stuff[1:]
  511. elif stuff[0] == '^':
  512. stuff = '\\' + stuff
  513. res.append('[%s]' % stuff)
  514. else:
  515. res.append(re.escape(c))
  516. return ''.join(res)