setupbase.py 21 KB

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