setupbase.py 20 KB

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