You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1157 lines
43 KiB

  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. import re
  5. import shutil
  6. import sys
  7. import tempfile
  8. import warnings
  9. import zipfile
  10. from distutils.util import change_root
  11. from distutils import sysconfig
  12. from email.parser import FeedParser
  13. from pip._vendor import pkg_resources, six
  14. from pip._vendor.distlib.markers import interpret as markers_interpret
  15. from pip._vendor.six.moves import configparser
  16. import pip.wheel
  17. from pip.compat import native_str, WINDOWS
  18. from pip.download import is_url, url_to_path, path_to_url, is_archive_file
  19. from pip.exceptions import (
  20. InstallationError, UninstallationError, UnsupportedWheel,
  21. )
  22. from pip.locations import (
  23. bin_py, running_under_virtualenv, PIP_DELETE_MARKER_FILENAME, bin_user,
  24. )
  25. from pip.utils import (
  26. display_path, rmtree, ask_path_exists, backup_dir, is_installable_dir,
  27. dist_in_usersite, dist_in_site_packages, egg_link_path, make_path_relative,
  28. call_subprocess, read_text_file, FakeFile, _make_build_dir, ensure_dir,
  29. )
  30. from pip.utils.deprecation import RemovedInPip8Warning
  31. from pip.utils.logging import indent_log
  32. from pip.req.req_uninstall import UninstallPathSet
  33. from pip.vcs import vcs
  34. from pip.wheel import move_wheel_files, Wheel
  35. from pip._vendor.packaging.version import Version
  36. logger = logging.getLogger(__name__)
  37. def _strip_extras(path):
  38. m = re.match(r'^(.+)(\[[^\]]+\])$', path)
  39. extras = None
  40. if m:
  41. path_no_extras = m.group(1)
  42. extras = m.group(2)
  43. else:
  44. path_no_extras = path
  45. return path_no_extras, extras
  46. class InstallRequirement(object):
  47. def __init__(self, req, comes_from, source_dir=None, editable=False,
  48. link=None, as_egg=False, update=True, editable_options=None,
  49. pycompile=True, markers=None, isolated=False, options=None,
  50. wheel_cache=None, constraint=False):
  51. self.extras = ()
  52. if isinstance(req, six.string_types):
  53. req = pkg_resources.Requirement.parse(req)
  54. self.extras = req.extras
  55. self.req = req
  56. self.comes_from = comes_from
  57. self.constraint = constraint
  58. self.source_dir = source_dir
  59. self.editable = editable
  60. if editable_options is None:
  61. editable_options = {}
  62. self.editable_options = editable_options
  63. self._wheel_cache = wheel_cache
  64. self.link = link
  65. self.as_egg = as_egg
  66. self.markers = markers
  67. self._egg_info_path = None
  68. # This holds the pkg_resources.Distribution object if this requirement
  69. # is already available:
  70. self.satisfied_by = None
  71. # This hold the pkg_resources.Distribution object if this requirement
  72. # conflicts with another installed distribution:
  73. self.conflicts_with = None
  74. # Temporary build location
  75. self._temp_build_dir = None
  76. # Used to store the global directory where the _temp_build_dir should
  77. # have been created. Cf _correct_build_location method.
  78. self._ideal_global_dir = None
  79. # True if the editable should be updated:
  80. self.update = update
  81. # Set to True after successful installation
  82. self.install_succeeded = None
  83. # UninstallPathSet of uninstalled distribution (for possible rollback)
  84. self.uninstalled = None
  85. self.use_user_site = False
  86. self.target_dir = None
  87. self.options = options if options else {}
  88. self.pycompile = pycompile
  89. # Set to True after successful preparation of this requirement
  90. self.prepared = False
  91. self.isolated = isolated
  92. @classmethod
  93. def from_editable(cls, editable_req, comes_from=None, default_vcs=None,
  94. isolated=False, options=None, wheel_cache=None,
  95. constraint=False):
  96. from pip.index import Link
  97. name, url, extras_override, editable_options = parse_editable(
  98. editable_req, default_vcs)
  99. if url.startswith('file:'):
  100. source_dir = url_to_path(url)
  101. else:
  102. source_dir = None
  103. res = cls(name, comes_from, source_dir=source_dir,
  104. editable=True,
  105. link=Link(url),
  106. constraint=constraint,
  107. editable_options=editable_options,
  108. isolated=isolated,
  109. options=options if options else {},
  110. wheel_cache=wheel_cache)
  111. if extras_override is not None:
  112. res.extras = extras_override
  113. return res
  114. @classmethod
  115. def from_line(
  116. cls, name, comes_from=None, isolated=False, options=None,
  117. wheel_cache=None, constraint=False):
  118. """Creates an InstallRequirement from a name, which might be a
  119. requirement, directory containing 'setup.py', filename, or URL.
  120. """
  121. from pip.index import Link
  122. if is_url(name):
  123. marker_sep = '; '
  124. else:
  125. marker_sep = ';'
  126. if marker_sep in name:
  127. name, markers = name.split(marker_sep, 1)
  128. markers = markers.strip()
  129. if not markers:
  130. markers = None
  131. else:
  132. markers = None
  133. name = name.strip()
  134. req = None
  135. path = os.path.normpath(os.path.abspath(name))
  136. link = None
  137. extras = None
  138. if is_url(name):
  139. link = Link(name)
  140. else:
  141. p, extras = _strip_extras(path)
  142. if (os.path.isdir(p) and
  143. (os.path.sep in name or name.startswith('.'))):
  144. if not is_installable_dir(p):
  145. raise InstallationError(
  146. "Directory %r is not installable. File 'setup.py' "
  147. "not found." % name
  148. )
  149. link = Link(path_to_url(p))
  150. elif is_archive_file(p):
  151. if not os.path.isfile(p):
  152. logger.warning(
  153. 'Requirement %r looks like a filename, but the '
  154. 'file does not exist',
  155. name
  156. )
  157. link = Link(path_to_url(p))
  158. # it's a local file, dir, or url
  159. if link:
  160. # Handle relative file URLs
  161. if link.scheme == 'file' and re.search(r'\.\./', link.url):
  162. link = Link(
  163. path_to_url(os.path.normpath(os.path.abspath(link.path))))
  164. # wheel file
  165. if link.is_wheel:
  166. wheel = Wheel(link.filename) # can raise InvalidWheelFilename
  167. if not wheel.supported():
  168. raise UnsupportedWheel(
  169. "%s is not a supported wheel on this platform." %
  170. wheel.filename
  171. )
  172. req = "%s==%s" % (wheel.name, wheel.version)
  173. else:
  174. # set the req to the egg fragment. when it's not there, this
  175. # will become an 'unnamed' requirement
  176. req = link.egg_fragment
  177. # a requirement specifier
  178. else:
  179. req = name
  180. options = options if options else {}
  181. res = cls(req, comes_from, link=link, markers=markers,
  182. isolated=isolated, options=options,
  183. wheel_cache=wheel_cache, constraint=constraint)
  184. if extras:
  185. res.extras = pkg_resources.Requirement.parse('__placeholder__' +
  186. extras).extras
  187. return res
  188. def __str__(self):
  189. if self.req:
  190. s = str(self.req)
  191. if self.link:
  192. s += ' from %s' % self.link.url
  193. else:
  194. s = self.link.url if self.link else None
  195. if self.satisfied_by is not None:
  196. s += ' in %s' % display_path(self.satisfied_by.location)
  197. if self.comes_from:
  198. if isinstance(self.comes_from, six.string_types):
  199. comes_from = self.comes_from
  200. else:
  201. comes_from = self.comes_from.from_path()
  202. if comes_from:
  203. s += ' (from %s)' % comes_from
  204. return s
  205. def __repr__(self):
  206. return '<%s object: %s editable=%r>' % (
  207. self.__class__.__name__, str(self), self.editable)
  208. def populate_link(self, finder, upgrade):
  209. """Ensure that if a link can be found for this, that it is found.
  210. Note that self.link may still be None - if Upgrade is False and the
  211. requirement is already installed.
  212. """
  213. if self.link is None:
  214. self.link = finder.find_requirement(self, upgrade)
  215. @property
  216. def link(self):
  217. return self._link
  218. @link.setter
  219. def link(self, link):
  220. # Lookup a cached wheel, if possible.
  221. if self._wheel_cache is None:
  222. self._link = link
  223. else:
  224. self._link = self._wheel_cache.cached_wheel(link, self.name)
  225. @property
  226. def specifier(self):
  227. return self.req.specifier
  228. def from_path(self):
  229. if self.req is None:
  230. return None
  231. s = str(self.req)
  232. if self.comes_from:
  233. if isinstance(self.comes_from, six.string_types):
  234. comes_from = self.comes_from
  235. else:
  236. comes_from = self.comes_from.from_path()
  237. if comes_from:
  238. s += '->' + comes_from
  239. return s
  240. def build_location(self, build_dir):
  241. if self._temp_build_dir is not None:
  242. return self._temp_build_dir
  243. if self.req is None:
  244. # for requirement via a path to a directory: the name of the
  245. # package is not available yet so we create a temp directory
  246. # Once run_egg_info will have run, we'll be able
  247. # to fix it via _correct_build_location
  248. self._temp_build_dir = tempfile.mkdtemp('-build', 'pip-')
  249. self._ideal_build_dir = build_dir
  250. return self._temp_build_dir
  251. if self.editable:
  252. name = self.name.lower()
  253. else:
  254. name = self.name
  255. # FIXME: Is there a better place to create the build_dir? (hg and bzr
  256. # need this)
  257. if not os.path.exists(build_dir):
  258. logger.debug('Creating directory %s', build_dir)
  259. _make_build_dir(build_dir)
  260. return os.path.join(build_dir, name)
  261. def _correct_build_location(self):
  262. """Move self._temp_build_dir to self._ideal_build_dir/self.req.name
  263. For some requirements (e.g. a path to a directory), the name of the
  264. package is not available until we run egg_info, so the build_location
  265. will return a temporary directory and store the _ideal_build_dir.
  266. This is only called by self.egg_info_path to fix the temporary build
  267. directory.
  268. """
  269. if self.source_dir is not None:
  270. return
  271. assert self.req is not None
  272. assert self._temp_build_dir
  273. assert self._ideal_build_dir
  274. old_location = self._temp_build_dir
  275. self._temp_build_dir = None
  276. new_location = self.build_location(self._ideal_build_dir)
  277. if os.path.exists(new_location):
  278. raise InstallationError(
  279. 'A package already exists in %s; please remove it to continue'
  280. % display_path(new_location))
  281. logger.debug(
  282. 'Moving package %s from %s to new location %s',
  283. self, display_path(old_location), display_path(new_location),
  284. )
  285. shutil.move(old_location, new_location)
  286. self._temp_build_dir = new_location
  287. self._ideal_build_dir = None
  288. self.source_dir = new_location
  289. self._egg_info_path = None
  290. @property
  291. def name(self):
  292. if self.req is None:
  293. return None
  294. return native_str(self.req.project_name)
  295. @property
  296. def setup_py(self):
  297. assert self.source_dir, "No source dir for %s" % self
  298. try:
  299. import setuptools # noqa
  300. except ImportError:
  301. # Setuptools is not available
  302. raise InstallationError(
  303. "setuptools must be installed to install from a source "
  304. "distribution"
  305. )
  306. setup_file = 'setup.py'
  307. if self.editable_options and 'subdirectory' in self.editable_options:
  308. setup_py = os.path.join(self.source_dir,
  309. self.editable_options['subdirectory'],
  310. setup_file)
  311. else:
  312. setup_py = os.path.join(self.source_dir, setup_file)
  313. # Python2 __file__ should not be unicode
  314. if six.PY2 and isinstance(setup_py, six.text_type):
  315. setup_py = setup_py.encode(sys.getfilesystemencoding())
  316. return setup_py
  317. def run_egg_info(self):
  318. assert self.source_dir
  319. if self.name:
  320. logger.debug(
  321. 'Running setup.py (path:%s) egg_info for package %s',
  322. self.setup_py, self.name,
  323. )
  324. else:
  325. logger.debug(
  326. 'Running setup.py (path:%s) egg_info for package from %s',
  327. self.setup_py, self.link,
  328. )
  329. with indent_log():
  330. script = self._run_setup_py
  331. script = script.replace('__SETUP_PY__', repr(self.setup_py))
  332. script = script.replace('__PKG_NAME__', repr(self.name))
  333. base_cmd = [sys.executable, '-c', script]
  334. if self.isolated:
  335. base_cmd += ["--no-user-cfg"]
  336. egg_info_cmd = base_cmd + ['egg_info']
  337. # We can't put the .egg-info files at the root, because then the
  338. # source code will be mistaken for an installed egg, causing
  339. # problems
  340. if self.editable:
  341. egg_base_option = []
  342. else:
  343. egg_info_dir = os.path.join(self.source_dir, 'pip-egg-info')
  344. ensure_dir(egg_info_dir)
  345. egg_base_option = ['--egg-base', 'pip-egg-info']
  346. cwd = self.source_dir
  347. if self.editable_options and \
  348. 'subdirectory' in self.editable_options:
  349. cwd = os.path.join(cwd, self.editable_options['subdirectory'])
  350. call_subprocess(
  351. egg_info_cmd + egg_base_option,
  352. cwd=cwd,
  353. show_stdout=False,
  354. command_level=logging.DEBUG,
  355. command_desc='python setup.py egg_info')
  356. if not self.req:
  357. if isinstance(
  358. pkg_resources.parse_version(self.pkg_info()["Version"]),
  359. Version):
  360. op = "=="
  361. else:
  362. op = "==="
  363. self.req = pkg_resources.Requirement.parse(
  364. "".join([
  365. self.pkg_info()["Name"],
  366. op,
  367. self.pkg_info()["Version"],
  368. ]))
  369. self._correct_build_location()
  370. # FIXME: This is a lame hack, entirely for PasteScript which has
  371. # a self-provided entry point that causes this awkwardness
  372. _run_setup_py = """
  373. __file__ = __SETUP_PY__
  374. from setuptools.command import egg_info
  375. import pkg_resources
  376. import os
  377. import tokenize
  378. def replacement_run(self):
  379. self.mkpath(self.egg_info)
  380. installer = self.distribution.fetch_build_egg
  381. for ep in pkg_resources.iter_entry_points('egg_info.writers'):
  382. # require=False is the change we're making:
  383. writer = ep.load(require=False)
  384. if writer:
  385. writer(self, ep.name, os.path.join(self.egg_info,ep.name))
  386. self.find_sources()
  387. egg_info.egg_info.run = replacement_run
  388. exec(compile(
  389. getattr(tokenize, 'open', open)(__file__).read().replace('\\r\\n', '\\n'),
  390. __file__,
  391. 'exec'
  392. ))
  393. """
  394. def egg_info_data(self, filename):
  395. if self.satisfied_by is not None:
  396. if not self.satisfied_by.has_metadata(filename):
  397. return None
  398. return self.satisfied_by.get_metadata(filename)
  399. assert self.source_dir
  400. filename = self.egg_info_path(filename)
  401. if not os.path.exists(filename):
  402. return None
  403. data = read_text_file(filename)
  404. return data
  405. def egg_info_path(self, filename):
  406. if self._egg_info_path is None:
  407. if self.editable:
  408. base = self.source_dir
  409. else:
  410. base = os.path.join(self.source_dir, 'pip-egg-info')
  411. filenames = os.listdir(base)
  412. if self.editable:
  413. filenames = []
  414. for root, dirs, files in os.walk(base):
  415. for dir in vcs.dirnames:
  416. if dir in dirs:
  417. dirs.remove(dir)
  418. # Iterate over a copy of ``dirs``, since mutating
  419. # a list while iterating over it can cause trouble.
  420. # (See https://github.com/pypa/pip/pull/462.)
  421. for dir in list(dirs):
  422. # Don't search in anything that looks like a virtualenv
  423. # environment
  424. if (
  425. os.path.exists(
  426. os.path.join(root, dir, 'bin', 'python')
  427. ) or
  428. os.path.exists(
  429. os.path.join(
  430. root, dir, 'Scripts', 'Python.exe'
  431. )
  432. )):
  433. dirs.remove(dir)
  434. # Also don't search through tests
  435. elif dir == 'test' or dir == 'tests':
  436. dirs.remove(dir)
  437. filenames.extend([os.path.join(root, dir)
  438. for dir in dirs])
  439. filenames = [f for f in filenames if f.endswith('.egg-info')]
  440. if not filenames:
  441. raise InstallationError(
  442. 'No files/directories in %s (from %s)' % (base, filename)
  443. )
  444. assert filenames, \
  445. "No files/directories in %s (from %s)" % (base, filename)
  446. # if we have more than one match, we pick the toplevel one. This
  447. # can easily be the case if there is a dist folder which contains
  448. # an extracted tarball for testing purposes.
  449. if len(filenames) > 1:
  450. filenames.sort(
  451. key=lambda x: x.count(os.path.sep) +
  452. (os.path.altsep and x.count(os.path.altsep) or 0)
  453. )
  454. self._egg_info_path = os.path.join(base, filenames[0])
  455. return os.path.join(self._egg_info_path, filename)
  456. def pkg_info(self):
  457. p = FeedParser()
  458. data = self.egg_info_data('PKG-INFO')
  459. if not data:
  460. logger.warning(
  461. 'No PKG-INFO file found in %s',
  462. display_path(self.egg_info_path('PKG-INFO')),
  463. )
  464. p.feed(data or '')
  465. return p.close()
  466. _requirements_section_re = re.compile(r'\[(.*?)\]')
  467. @property
  468. def installed_version(self):
  469. # Create a requirement that we'll look for inside of setuptools.
  470. req = pkg_resources.Requirement.parse(self.name)
  471. # We want to avoid having this cached, so we need to construct a new
  472. # working set each time.
  473. working_set = pkg_resources.WorkingSet()
  474. # Get the installed distribution from our working set
  475. dist = working_set.find(req)
  476. # Check to see if we got an installed distribution or not, if we did
  477. # we want to return it's version.
  478. if dist:
  479. return dist.version
  480. def assert_source_matches_version(self):
  481. assert self.source_dir
  482. version = self.pkg_info()['version']
  483. if version not in self.req:
  484. logger.warning(
  485. 'Requested %s, but installing version %s',
  486. self,
  487. self.installed_version,
  488. )
  489. else:
  490. logger.debug(
  491. 'Source in %s has version %s, which satisfies requirement %s',
  492. display_path(self.source_dir),
  493. version,
  494. self,
  495. )
  496. def update_editable(self, obtain=True):
  497. if not self.link:
  498. logger.debug(
  499. "Cannot update repository at %s; repository location is "
  500. "unknown",
  501. self.source_dir,
  502. )
  503. return
  504. assert self.editable
  505. assert self.source_dir
  506. if self.link.scheme == 'file':
  507. # Static paths don't get updated
  508. return
  509. assert '+' in self.link.url, "bad url: %r" % self.link.url
  510. if not self.update:
  511. return
  512. vc_type, url = self.link.url.split('+', 1)
  513. backend = vcs.get_backend(vc_type)
  514. if backend:
  515. vcs_backend = backend(self.link.url)
  516. if obtain:
  517. vcs_backend.obtain(self.source_dir)
  518. else:
  519. vcs_backend.export(self.source_dir)
  520. else:
  521. assert 0, (
  522. 'Unexpected version control type (in %s): %s'
  523. % (self.link, vc_type))
  524. def uninstall(self, auto_confirm=False):
  525. """
  526. Uninstall the distribution currently satisfying this requirement.
  527. Prompts before removing or modifying files unless
  528. ``auto_confirm`` is True.
  529. Refuses to delete or modify files outside of ``sys.prefix`` -
  530. thus uninstallation within a virtual environment can only
  531. modify that virtual environment, even if the virtualenv is
  532. linked to global site-packages.
  533. """
  534. if not self.check_if_exists():
  535. raise UninstallationError(
  536. "Cannot uninstall requirement %s, not installed" % (self.name,)
  537. )
  538. dist = self.satisfied_by or self.conflicts_with
  539. paths_to_remove = UninstallPathSet(dist)
  540. develop_egg_link = egg_link_path(dist)
  541. develop_egg_link_egg_info = '{0}.egg-info'.format(
  542. pkg_resources.to_filename(dist.project_name))
  543. egg_info_exists = dist.egg_info and os.path.exists(dist.egg_info)
  544. # Special case for distutils installed package
  545. distutils_egg_info = getattr(dist._provider, 'path', None)
  546. # Uninstall cases order do matter as in the case of 2 installs of the
  547. # same package, pip needs to uninstall the currently detected version
  548. if (egg_info_exists and dist.egg_info.endswith('.egg-info') and
  549. not dist.egg_info.endswith(develop_egg_link_egg_info)):
  550. # if dist.egg_info.endswith(develop_egg_link_egg_info), we
  551. # are in fact in the develop_egg_link case
  552. paths_to_remove.add(dist.egg_info)
  553. if dist.has_metadata('installed-files.txt'):
  554. for installed_file in dist.get_metadata(
  555. 'installed-files.txt').splitlines():
  556. path = os.path.normpath(
  557. os.path.join(dist.egg_info, installed_file)
  558. )
  559. paths_to_remove.add(path)
  560. # FIXME: need a test for this elif block
  561. # occurs with --single-version-externally-managed/--record outside
  562. # of pip
  563. elif dist.has_metadata('top_level.txt'):
  564. if dist.has_metadata('namespace_packages.txt'):
  565. namespaces = dist.get_metadata('namespace_packages.txt')
  566. else:
  567. namespaces = []
  568. for top_level_pkg in [
  569. p for p
  570. in dist.get_metadata('top_level.txt').splitlines()
  571. if p and p not in namespaces]:
  572. path = os.path.join(dist.location, top_level_pkg)
  573. paths_to_remove.add(path)
  574. paths_to_remove.add(path + '.py')
  575. paths_to_remove.add(path + '.pyc')
  576. elif distutils_egg_info:
  577. warnings.warn(
  578. "Uninstalling a distutils installed project ({0}) has been "
  579. "deprecated and will be removed in a future version. This is "
  580. "due to the fact that uninstalling a distutils project will "
  581. "only partially uninstall the project.".format(self.name),
  582. RemovedInPip8Warning,
  583. )
  584. paths_to_remove.add(distutils_egg_info)
  585. elif dist.location.endswith('.egg'):
  586. # package installed by easy_install
  587. # We cannot match on dist.egg_name because it can slightly vary
  588. # i.e. setuptools-0.6c11-py2.6.egg vs setuptools-0.6rc11-py2.6.egg
  589. paths_to_remove.add(dist.location)
  590. easy_install_egg = os.path.split(dist.location)[1]
  591. easy_install_pth = os.path.join(os.path.dirname(dist.location),
  592. 'easy-install.pth')
  593. paths_to_remove.add_pth(easy_install_pth, './' + easy_install_egg)
  594. elif develop_egg_link:
  595. # develop egg
  596. with open(develop_egg_link, 'r') as fh:
  597. link_pointer = os.path.normcase(fh.readline().strip())
  598. assert (link_pointer == dist.location), (
  599. 'Egg-link %s does not match installed location of %s '
  600. '(at %s)' % (link_pointer, self.name, dist.location)
  601. )
  602. paths_to_remove.add(develop_egg_link)
  603. easy_install_pth = os.path.join(os.path.dirname(develop_egg_link),
  604. 'easy-install.pth')
  605. paths_to_remove.add_pth(easy_install_pth, dist.location)
  606. elif egg_info_exists and dist.egg_info.endswith('.dist-info'):
  607. for path in pip.wheel.uninstallation_paths(dist):
  608. paths_to_remove.add(path)
  609. else:
  610. logger.debug(
  611. 'Not sure how to uninstall: %s - Check: %s',
  612. dist, dist.location)
  613. # find distutils scripts= scripts
  614. if dist.has_metadata('scripts') and dist.metadata_isdir('scripts'):
  615. for script in dist.metadata_listdir('scripts'):
  616. if dist_in_usersite(dist):
  617. bin_dir = bin_user
  618. else:
  619. bin_dir = bin_py
  620. paths_to_remove.add(os.path.join(bin_dir, script))
  621. if WINDOWS:
  622. paths_to_remove.add(os.path.join(bin_dir, script) + '.bat')
  623. # find console_scripts
  624. if dist.has_metadata('entry_points.txt'):
  625. config = configparser.SafeConfigParser()
  626. config.readfp(
  627. FakeFile(dist.get_metadata_lines('entry_points.txt'))
  628. )
  629. if config.has_section('console_scripts'):
  630. for name, value in config.items('console_scripts'):
  631. if dist_in_usersite(dist):
  632. bin_dir = bin_user
  633. else:
  634. bin_dir = bin_py
  635. paths_to_remove.add(os.path.join(bin_dir, name))
  636. if WINDOWS:
  637. paths_to_remove.add(
  638. os.path.join(bin_dir, name) + '.exe'
  639. )
  640. paths_to_remove.add(
  641. os.path.join(bin_dir, name) + '.exe.manifest'
  642. )
  643. paths_to_remove.add(
  644. os.path.join(bin_dir, name) + '-script.py'
  645. )
  646. paths_to_remove.remove(auto_confirm)
  647. self.uninstalled = paths_to_remove
  648. def rollback_uninstall(self):
  649. if self.uninstalled:
  650. self.uninstalled.rollback()
  651. else:
  652. logger.error(
  653. "Can't rollback %s, nothing uninstalled.", self.project_name,
  654. )
  655. def commit_uninstall(self):
  656. if self.uninstalled:
  657. self.uninstalled.commit()
  658. else:
  659. logger.error(
  660. "Can't commit %s, nothing uninstalled.", self.project_name,
  661. )
  662. def archive(self, build_dir):
  663. assert self.source_dir
  664. create_archive = True
  665. archive_name = '%s-%s.zip' % (self.name, self.pkg_info()["version"])
  666. archive_path = os.path.join(build_dir, archive_name)
  667. if os.path.exists(archive_path):
  668. response = ask_path_exists(
  669. 'The file %s exists. (i)gnore, (w)ipe, (b)ackup ' %
  670. display_path(archive_path), ('i', 'w', 'b'))
  671. if response == 'i':
  672. create_archive = False
  673. elif response == 'w':
  674. logger.warning('Deleting %s', display_path(archive_path))
  675. os.remove(archive_path)
  676. elif response == 'b':
  677. dest_file = backup_dir(archive_path)
  678. logger.warning(
  679. 'Backing up %s to %s',
  680. display_path(archive_path),
  681. display_path(dest_file),
  682. )
  683. shutil.move(archive_path, dest_file)
  684. if create_archive:
  685. zip = zipfile.ZipFile(
  686. archive_path, 'w', zipfile.ZIP_DEFLATED,
  687. allowZip64=True
  688. )
  689. dir = os.path.normcase(os.path.abspath(self.source_dir))
  690. for dirpath, dirnames, filenames in os.walk(dir):
  691. if 'pip-egg-info' in dirnames:
  692. dirnames.remove('pip-egg-info')
  693. for dirname in dirnames:
  694. dirname = os.path.join(dirpath, dirname)
  695. name = self._clean_zip_name(dirname, dir)
  696. zipdir = zipfile.ZipInfo(self.name + '/' + name + '/')
  697. zipdir.external_attr = 0x1ED << 16 # 0o755
  698. zip.writestr(zipdir, '')
  699. for filename in filenames:
  700. if filename == PIP_DELETE_MARKER_FILENAME:
  701. continue
  702. filename = os.path.join(dirpath, filename)
  703. name = self._clean_zip_name(filename, dir)
  704. zip.write(filename, self.name + '/' + name)
  705. zip.close()
  706. logger.info('Saved %s', display_path(archive_path))
  707. def _clean_zip_name(self, name, prefix):
  708. assert name.startswith(prefix + os.path.sep), (
  709. "name %r doesn't start with prefix %r" % (name, prefix)
  710. )
  711. name = name[len(prefix) + 1:]
  712. name = name.replace(os.path.sep, '/')
  713. return name
  714. def match_markers(self):
  715. if self.markers is not None:
  716. return markers_interpret(self.markers)
  717. else:
  718. return True
  719. def install(self, install_options, global_options=[], root=None, strip_file_prefix=None):
  720. if self.editable:
  721. self.install_editable(install_options, global_options)
  722. return
  723. if self.is_wheel:
  724. version = pip.wheel.wheel_version(self.source_dir)
  725. pip.wheel.check_compatibility(version, self.name)
  726. self.move_wheel_files(
  727. self.source_dir,
  728. root=root,
  729. strip_file_prefix=strip_file_prefix
  730. )
  731. self.install_succeeded = True
  732. return
  733. # Extend the list of global and install options passed on to
  734. # the setup.py call with the ones from the requirements file.
  735. # Options specified in requirements file override those
  736. # specified on the command line, since the last option given
  737. # to setup.py is the one that is used.
  738. global_options += self.options.get('global_options', [])
  739. install_options += self.options.get('install_options', [])
  740. if self.isolated:
  741. global_options = list(global_options) + ["--no-user-cfg"]
  742. temp_location = tempfile.mkdtemp('-record', 'pip-')
  743. record_filename = os.path.join(temp_location, 'install-record.txt')
  744. try:
  745. install_args = [sys.executable]
  746. install_args.append('-c')
  747. install_args.append(
  748. "import setuptools, tokenize;__file__=%r;"
  749. "exec(compile(getattr(tokenize, 'open', open)(__file__).read()"
  750. ".replace('\\r\\n', '\\n'), __file__, 'exec'))" % self.setup_py
  751. )
  752. install_args += list(global_options) + \
  753. ['install', '--record', record_filename]
  754. if not self.as_egg:
  755. install_args += ['--single-version-externally-managed']
  756. if root is not None:
  757. install_args += ['--root', root]
  758. if self.pycompile:
  759. install_args += ["--compile"]
  760. else:
  761. install_args += ["--no-compile"]
  762. if running_under_virtualenv():
  763. py_ver_str = 'python' + sysconfig.get_python_version()
  764. install_args += ['--install-headers',
  765. os.path.join(sys.prefix, 'include', 'site',
  766. py_ver_str, self.name)]
  767. logger.info('Running setup.py install for %s', self.name)
  768. with indent_log():
  769. call_subprocess(
  770. install_args + install_options,
  771. cwd=self.source_dir,
  772. show_stdout=False,
  773. )
  774. if not os.path.exists(record_filename):
  775. logger.debug('Record file %s not found', record_filename)
  776. return
  777. self.install_succeeded = True
  778. if self.as_egg:
  779. # there's no --always-unzip option we can pass to install
  780. # command so we unable to save the installed-files.txt
  781. return
  782. def prepend_root(path):
  783. if root is None or not os.path.isabs(path):
  784. return path
  785. else:
  786. return change_root(root, path)
  787. with open(record_filename) as f:
  788. for line in f:
  789. directory = os.path.dirname(line)
  790. if directory.endswith('.egg-info'):
  791. egg_info_dir = prepend_root(directory)
  792. break
  793. else:
  794. logger.warning(
  795. 'Could not find .egg-info directory in install record'
  796. ' for %s',
  797. self,
  798. )
  799. # FIXME: put the record somewhere
  800. # FIXME: should this be an error?
  801. return
  802. new_lines = []
  803. with open(record_filename) as f:
  804. for line in f:
  805. filename = line.strip()
  806. if os.path.isdir(filename):
  807. filename += os.path.sep
  808. new_lines.append(
  809. make_path_relative(
  810. prepend_root(filename), egg_info_dir)
  811. )
  812. inst_files_path = os.path.join(egg_info_dir, 'installed-files.txt')
  813. with open(inst_files_path, 'w') as f:
  814. f.write('\n'.join(new_lines) + '\n')
  815. finally:
  816. if os.path.exists(record_filename):
  817. os.remove(record_filename)
  818. rmtree(temp_location)
  819. def ensure_has_source_dir(self, parent_dir):
  820. """Ensure that a source_dir is set.
  821. This will create a temporary build dir if the name of the requirement
  822. isn't known yet.
  823. :param parent_dir: The ideal pip parent_dir for the source_dir.
  824. Generally src_dir for editables and build_dir for sdists.
  825. :return: self.source_dir
  826. """
  827. if self.source_dir is None:
  828. self.source_dir = self.build_location(parent_dir)
  829. return self.source_dir
  830. def remove_temporary_source(self):
  831. """Remove the source files from this requirement, if they are marked
  832. for deletion"""
  833. if self.source_dir and os.path.exists(
  834. os.path.join(self.source_dir, PIP_DELETE_MARKER_FILENAME)):
  835. logger.debug('Removing source in %s', self.source_dir)
  836. rmtree(self.source_dir)
  837. self.source_dir = None
  838. if self._temp_build_dir and os.path.exists(self._temp_build_dir):
  839. rmtree(self._temp_build_dir)
  840. self._temp_build_dir = None
  841. def install_editable(self, install_options, global_options=()):
  842. logger.info('Running setup.py develop for %s', self.name)
  843. if self.isolated:
  844. global_options = list(global_options) + ["--no-user-cfg"]
  845. with indent_log():
  846. # FIXME: should we do --install-headers here too?
  847. cwd = self.source_dir
  848. if self.editable_options and \
  849. 'subdirectory' in self.editable_options:
  850. cwd = os.path.join(cwd, self.editable_options['subdirectory'])
  851. call_subprocess(
  852. [
  853. sys.executable,
  854. '-c',
  855. "import setuptools, tokenize; __file__=%r; exec(compile("
  856. "getattr(tokenize, 'open', open)(__file__).read().replace"
  857. "('\\r\\n', '\\n'), __file__, 'exec'))" % self.setup_py
  858. ] +
  859. list(global_options) +
  860. ['develop', '--no-deps'] +
  861. list(install_options),
  862. cwd=cwd,
  863. show_stdout=False)
  864. self.install_succeeded = True
  865. def check_if_exists(self):
  866. """Find an installed distribution that satisfies or conflicts
  867. with this requirement, and set self.satisfied_by or
  868. self.conflicts_with appropriately.
  869. """
  870. if self.req is None:
  871. return False
  872. try:
  873. self.satisfied_by = pkg_resources.get_distribution(self.req)
  874. except pkg_resources.DistributionNotFound:
  875. return False
  876. except pkg_resources.VersionConflict:
  877. existing_dist = pkg_resources.get_distribution(
  878. self.req.project_name
  879. )
  880. if self.use_user_site:
  881. if dist_in_usersite(existing_dist):
  882. self.conflicts_with = existing_dist
  883. elif (running_under_virtualenv() and
  884. dist_in_site_packages(existing_dist)):
  885. raise InstallationError(
  886. "Will not install to the user site because it will "
  887. "lack sys.path precedence to %s in %s" %
  888. (existing_dist.project_name, existing_dist.location)
  889. )
  890. else:
  891. self.conflicts_with = existing_dist
  892. return True
  893. @property
  894. def is_wheel(self):
  895. return self.link and self.link.is_wheel
  896. def move_wheel_files(self, wheeldir, root=None, strip_file_prefix=None):
  897. move_wheel_files(
  898. self.name, self.req, wheeldir,
  899. user=self.use_user_site,
  900. home=self.target_dir,
  901. root=root,
  902. pycompile=self.pycompile,
  903. isolated=self.isolated,
  904. strip_file_prefix=strip_file_prefix,
  905. )
  906. def get_dist(self):
  907. """Return a pkg_resources.Distribution built from self.egg_info_path"""
  908. egg_info = self.egg_info_path('').rstrip('/')
  909. base_dir = os.path.dirname(egg_info)
  910. metadata = pkg_resources.PathMetadata(base_dir, egg_info)
  911. dist_name = os.path.splitext(os.path.basename(egg_info))[0]
  912. return pkg_resources.Distribution(
  913. os.path.dirname(egg_info),
  914. project_name=dist_name,
  915. metadata=metadata)
  916. def _strip_postfix(req):
  917. """
  918. Strip req postfix ( -dev, 0.2, etc )
  919. """
  920. # FIXME: use package_to_requirement?
  921. match = re.search(r'^(.*?)(?:-dev|-\d.*)$', req)
  922. if match:
  923. # Strip off -dev, -0.2, etc.
  924. req = match.group(1)
  925. return req
  926. def _build_req_from_url(url):
  927. parts = [p for p in url.split('#', 1)[0].split('/') if p]
  928. req = None
  929. if parts[-2] in ('tags', 'branches', 'tag', 'branch'):
  930. req = parts[-3]
  931. elif parts[-1] == 'trunk':
  932. req = parts[-2]
  933. return req
  934. def _build_editable_options(req):
  935. """
  936. This method generates a dictionary of the query string
  937. parameters contained in a given editable URL.
  938. """
  939. regexp = re.compile(r"[\?#&](?P<name>[^&=]+)=(?P<value>[^&=]+)")
  940. matched = regexp.findall(req)
  941. if matched:
  942. ret = dict()
  943. for option in matched:
  944. (name, value) = option
  945. if name in ret:
  946. raise Exception("%s option already defined" % name)
  947. ret[name] = value
  948. return ret
  949. return None
  950. def parse_editable(editable_req, default_vcs=None):
  951. """Parses an editable requirement into:
  952. - a requirement name
  953. - an URL
  954. - extras
  955. - editable options
  956. Accepted requirements:
  957. svn+http://blahblah@rev#egg=Foobar[baz]&subdirectory=version_subdir
  958. .[some_extra]
  959. """
  960. url = editable_req
  961. extras = None
  962. # If a file path is specified with extras, strip off the extras.
  963. m = re.match(r'^(.+)(\[[^\]]+\])$', url)
  964. if m:
  965. url_no_extras = m.group(1)
  966. extras = m.group(2)
  967. else:
  968. url_no_extras = url
  969. if os.path.isdir(url_no_extras):
  970. if not os.path.exists(os.path.join(url_no_extras, 'setup.py')):
  971. raise InstallationError(
  972. "Directory %r is not installable. File 'setup.py' not found." %
  973. url_no_extras
  974. )
  975. # Treating it as code that has already been checked out
  976. url_no_extras = path_to_url(url_no_extras)
  977. if url_no_extras.lower().startswith('file:'):
  978. if extras:
  979. return (
  980. None,
  981. url_no_extras,
  982. pkg_resources.Requirement.parse(
  983. '__placeholder__' + extras
  984. ).extras,
  985. {},
  986. )
  987. else:
  988. return None, url_no_extras, None, {}
  989. for version_control in vcs:
  990. if url.lower().startswith('%s:' % version_control):
  991. url = '%s+%s' % (version_control, url)
  992. break
  993. if '+' not in url:
  994. if default_vcs:
  995. url = default_vcs + '+' + url
  996. else:
  997. raise InstallationError(
  998. '%s should either be a path to a local project or a VCS url '
  999. 'beginning with svn+, git+, hg+, or bzr+' %
  1000. editable_req
  1001. )
  1002. vc_type = url.split('+', 1)[0].lower()
  1003. if not vcs.get_backend(vc_type):
  1004. error_message = 'For --editable=%s only ' % editable_req + \
  1005. ', '.join([backend.name + '+URL' for backend in vcs.backends]) + \
  1006. ' is currently supported'
  1007. raise InstallationError(error_message)
  1008. try:
  1009. options = _build_editable_options(editable_req)
  1010. except Exception as exc:
  1011. raise InstallationError(
  1012. '--editable=%s error in editable options:%s' % (editable_req, exc)
  1013. )
  1014. if not options or 'egg' not in options:
  1015. req = _build_req_from_url(editable_req)
  1016. if not req:
  1017. raise InstallationError(
  1018. '--editable=%s is not the right format; it must have '
  1019. '#egg=Package' % editable_req
  1020. )
  1021. else:
  1022. req = options['egg']
  1023. package = _strip_postfix(req)
  1024. return package, url, None, options