def test_fmt_ctl_matches(): fmt = FormatControl(set(), set()) assert fmt_ctl_formats(fmt, "fred") == frozenset(["source", "binary"]) fmt = FormatControl(set(["fred"]), set()) assert fmt_ctl_formats(fmt, "fred") == frozenset(["source"]) fmt = FormatControl(set(["fred"]), set([":all:"])) assert fmt_ctl_formats(fmt, "fred") == frozenset(["source"]) fmt = FormatControl(set(), set(["fred"])) assert fmt_ctl_formats(fmt, "fred") == frozenset(["binary"]) fmt = FormatControl(set([":all:"]), set(["fred"])) assert fmt_ctl_formats(fmt, "fred") == frozenset(["binary"])
def _make_requirement_set(session, tempdir, download_dir, wheel_download_dir): """ construction a RequirementSet """ format_control = FormatControl(set(), set()) wheel_cache = WheelCache(os.path.join(tempdir, ".cache"), format_control) requirement_set = RequirementSet( build_dir=os.path.join(tempdir, 'build'), src_dir=os.path.join(tempdir, 'src'), ignore_installed=True, download_dir=download_dir, wheel_download_dir=wheel_download_dir, wheel_cache=wheel_cache, session=session, ) try: requirement_set.add_requirement( EditInstallRequirement.from_editable("."), ) except Exception as e: sys.exit(e) if os.path.exists("requirements.txt"): with open("requirements.txt") as f: for line in f: requirement_set.add_requirement( InstallRequirement.from_line(line), ) return requirement_set
def writer(): return OutputWriter(src_files=["src_file", "src_file2"], dst_file="dst_file", dry_run=True, emit_header=True, emit_index=True, annotate=True, generate_hashes=False, default_index_url=None, index_urls=[], trusted_hosts=[], format_control=FormatControl(set(), set()))
def lookup_candidates(pkg, version): finder = PackageFinder(find_links=[], format_control=FormatControl(set([":all:"]), set()), index_urls=[_DEFAULT_INDEX], session=PipSession()) for candidate in finder.find_all_candidates(pkg): if str(candidate.version) == version: return candidate.location return None
class FakePackageFinder: format_control = FormatControl(set(), set()) def find_requirement(self, ireq, upgrade): return Link( str(ireq) ) # this URL will be passed to FakePipSession.get - we ignore extras for now def add_dependency_links(self, links): pass
def only_binary(): return Option( "--only-binary", dest="format_control", action="callback", callback=_handle_only_binary, type="str", default=FormatControl(set(), set()), help="Do not use source packages. Can be supplied multiple times, and " "each time adds to the existing value. Accepts either :all: to " "disable all source packages, :none: to empty the set, or one or " "more package names with commas between them. Packages without " "binary distributions will fail to install when this option is " "used on them.")
def no_binary(): return Option( "--no-binary", dest="format_control", action="callback", callback=_handle_no_binary, type="str", default=FormatControl(set(), set()), help="Do not use binary packages. Can be supplied multiple times, and " "each time adds to the existing value. Accepts either :all: to " "disable all binary packages, :none: to empty the set, or one or " "more package names with commas between them. Note that some " "packages are tricky to compile and may fail to install when " "this option is used on them.")
def find_packages_latest_versions(self, options): index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.info('Ignoring indexes: %s', ','.join(index_urls)) index_urls = [] dependency_links = [] for dist in get_installed_distributions(local_only=options.local, user_only=options.user): if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) with self._build_session(options) as session: finder = self._build_package_finder(options, index_urls, session) finder.add_dependency_links(dependency_links) installed_packages = get_installed_distributions( local_only=options.local, user_only=options.user, include_editables=False, ) format_control = FormatControl(set(), set()) wheel_cache = WheelCache(options.cache_dir, format_control) for dist in installed_packages: req = InstallRequirement.from_line( dist.key, None, isolated=options.isolated_mode, wheel_cache=wheel_cache) typ = 'unknown' try: link = finder.find_requirement(req, True) # If link is None, means installed version is most # up-to-date if link is None: continue except DistributionNotFound: continue else: canonical_name = pkg_resources.safe_name(req.name).lower() formats = fmt_ctl_formats(format_control, canonical_name) search = Search(req.name, canonical_name, formats) remote_version = finder._link_package_versions( link, search).version if link.is_wheel: typ = 'wheel' else: typ = 'sdist' yield dist, remote_version, typ
def _install_build_reqs(self, reqs, prefix): # Local import to avoid circular import (wheel <-> req_install) from pip.req.req_install import InstallRequirement from pip.index import FormatControl # Ignore the --no-binary option when installing the build system, so # we don't recurse trying to build a self-hosting build system. finder = copy.copy(self.finder) finder.format_control = FormatControl(set(), set()) urls = [finder.find_requirement(InstallRequirement.from_line(r), upgrade=False).url for r in reqs] args = [sys.executable, '-m', 'pip', 'install', '--ignore-installed', '--prefix', prefix] + list(urls) with open_spinner("Installing build dependencies") as spinner: call_subprocess(args, show_stdout=False, spinner=spinner)
def run(self, options, args): options.ignore_installed = True if options.python_version: python_versions = [options.python_version] else: python_versions = None dist_restriction_set = any([ options.python_version, options.platform, options.abi, options.implementation, ]) binary_only = FormatControl(set(), set([':all:'])) if dist_restriction_set and options.format_control != binary_only: raise CommandError( "--only-binary=:all: must be set and --no-binary must not " "be set (or must be set to :none:) when restricting platform " "and interpreter constraints using --python-version, " "--platform, --abi, or --implementation." ) options.src_dir = os.path.abspath(options.src_dir) options.download_dir = normalize_path(options.download_dir) ensure_dir(options.download_dir) with self._build_session(options) as session: finder = self._build_package_finder( options=options, session=session, platform=options.platform, python_versions=python_versions, abi=options.abi, implementation=options.implementation, ) build_delete = (not (options.no_clean or options.build_dir)) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with BuildDirectory(options.build_dir, delete=build_delete) as build_dir: requirement_set = RequirementSet( build_dir=build_dir, src_dir=options.src_dir, download_dir=options.download_dir, ignore_installed=True, ignore_dependencies=options.ignore_dependencies, session=session, isolated=options.isolated_mode, require_hashes=options.require_hashes ) self.populate_requirement_set( requirement_set, args, options, finder, session, self.name, None ) if not requirement_set.has_requirements: return requirement_set.prepare_files(finder) downloaded = ' '.join([ req.name for req in requirement_set.successfully_downloaded ]) if downloaded: logger.info( 'Successfully downloaded %s', downloaded ) # Clean up if not options.no_clean: requirement_set.cleanup_files() return requirement_set
def run(self, options, args): options.ignore_installed = True # editable doesn't really make sense for `pip download`, but the bowels # of the RequirementSet code require that property. options.editables = [] if options.python_version: python_versions = [options.python_version] else: python_versions = None dist_restriction_set = any([ options.python_version, options.platform, options.abi, options.implementation, ]) binary_only = FormatControl(set(), {':all:'}) if dist_restriction_set and options.format_control != binary_only: raise CommandError( "--only-binary=:all: must be set and --no-binary must not " "be set (or must be set to :none:) when restricting platform " "and interpreter constraints using --python-version, " "--platform, --abi, or --implementation." ) options.src_dir = os.path.abspath(options.src_dir) options.download_dir = normalize_path(options.download_dir) ensure_dir(options.download_dir) with self._build_session(options) as session: finder = self._build_package_finder( options=options, session=session, platform=options.platform, python_versions=python_versions, abi=options.abi, implementation=options.implementation, ) build_delete = (not (options.no_clean or options.build_dir)) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with TempDirectory( options.build_dir, delete=build_delete, kind="download" ) as directory: requirement_set = RequirementSet( build_dir=directory.path, src_dir=options.src_dir, download_dir=options.download_dir, require_hashes=options.require_hashes, progress_bar=options.progress_bar, ) self.populate_requirement_set( requirement_set, args, options, finder, session, self.name, None ) resolver = Resolver( preparer=RequirementPreparer(), finder=finder, session=session, use_user_site=False, upgrade_strategy="to-satisfy-only", force_reinstall=False, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=False, ignore_installed=True, isolated=options.isolated_mode, ) resolver.resolve(requirement_set) downloaded = ' '.join([ req.name for req in requirement_set.successfully_downloaded ]) if downloaded: logger.info( 'Successfully downloaded %s', downloaded ) # Clean up if not options.no_clean: requirement_set.cleanup_files() return requirement_set