setupbase.py 20 KB

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