def _verify_one(req: InstallRequirement, wheel_path: str) -> None: canonical_name = canonicalize_name(req.name or "") w = Wheel(os.path.basename(wheel_path)) if canonicalize_name(w.name) != canonical_name: raise InvalidWheelFilename( "Wheel has unexpected file name: expected {!r}, " "got {!r}".format(canonical_name, w.name), ) dist = get_wheel_distribution(FilesystemWheel(wheel_path), canonical_name) dist_verstr = str(dist.version) if canonicalize_version(dist_verstr) != canonicalize_version(w.version): raise InvalidWheelFilename( "Wheel has unexpected file name: expected {!r}, " "got {!r}".format(dist_verstr, w.version), ) metadata_version_value = dist.metadata_version if metadata_version_value is None: raise UnsupportedWheel("Missing Metadata-Version") try: metadata_version = Version(metadata_version_value) except InvalidVersion: msg = f"Invalid Metadata-Version: {metadata_version_value}" raise UnsupportedWheel(msg) if metadata_version >= Version("1.2") and not isinstance(dist.version, Version): raise UnsupportedWheel( "Metadata 1.2 mandates PEP 440 version, " "but {!r} is not".format(dist_verstr) )
def is_update(requirement, version): """ Check if version is newer than version(s) specified by requirement's specifiers. :param requirement: :type: pip.req.req_install.InstallRequirement :param version: :type: pip._vendor.packaging.version.Version :rtype: bool """ for spec in requirement.specifier: spec_version = Version(spec.version) if spec.operator == '==': if version <= spec_version: return False elif spec.operator == '!=': if version == spec_version: return False elif spec.operator == '>': if version <= spec_version: return False elif spec.operator == '>=': if version < spec_version: return False elif spec.operator == '<': if version < spec_version: return False elif spec.operator == '<=': if version <= spec_version: return False else: raise ValueError('Unknown operator: %s' % spec.operator) return True
def get_pip_version_for_python_executable(python_executable: str) -> Version: """ Returns pip version for the given python executable. """ str_version = run_python_snippet(python_executable, "import pip;print(pip.__version__)") return Version(str_version)
def __init__(self, py_version_info): # type: (Optional[Tuple[int, ...]]) -> None if py_version_info is not None: version_info = normalize_version_info(py_version_info) else: version_info = sys.version_info[:3] self._version = Version(".".join(str(c) for c in version_info))
def test_get(self): """pip 6.0+ - see issue #55""" dist = Mock( parsed_version=Version('1.2.3'), version='2.4.2', PKG_INFO='PKG-INFO', ) metadata = [ 'Metadata-Version: 1.1', 'Name: awslimitchecker', 'Version: 0.1.0', 'Summary: A script and python module to check your AWS service ', 'Home-page: https://github.com/jantman/awslimitchecker', 'Author: Jason Antman', 'Author-email: [email protected]', 'License: UNKNOWN', 'Description: awslimitchecker', '========================', '.. image:: https://pypip.in/v/awslimitchecker/badge.png', ':target: https://crate.io/packages/awslimitchecker', ':alt: PyPi package version', 'Status', '------', 'Keywords: AWS EC2 Amazon boto boto3 limits cloud', 'Platform: UNKNOWN', 'Classifier: Environment :: Console', ] def se_metadata(foo): for line in metadata: yield line dist.get_metadata_lines.side_effect = se_metadata res = self.cls._dist_version_url(dist) assert res == ('2.4.2', 'https://github.com/jantman/awslimitchecker')
def test_dist_from_wheel_url(session: PipSession) -> None: """Test if the acquired distribution contain correct information.""" dist = dist_from_wheel_url("mypy", MYPY_0_782_WHL, session) assert dist.canonical_name == "mypy" assert dist.version == Version("0.782") extras = list(dist.iter_provided_extras()) assert extras == ["dmypy"] assert {str(d) for d in dist.iter_dependencies(extras)} == MYPY_0_782_REQS
def __init__( self, link, # type: Link template, # type: InstallRequirement factory, # type: Factory name=None, # type: Optional[str] version=None, # type: Optional[_BaseVersion] ): # type: (...) -> None source_link = link cache_entry = factory.get_wheel_cache_entry(link, name) if cache_entry is not None: logger.debug("Using cached wheel link: %s", cache_entry.link) link = cache_entry.link ireq = make_install_req_from_link(link, template) assert ireq.link == link if ireq.link.is_wheel and not ireq.link.is_file: wheel = Wheel(ireq.link.filename) wheel_name = canonicalize_name(wheel.name) assert name == wheel_name, ( f"{name!r} != {wheel_name!r} for wheel") # Version may not be present for PEP 508 direct URLs if version is not None: wheel_version = Version(wheel.version) assert version == wheel_version, ( "{!r} != {!r} for wheel {}".format(version, wheel_version, name)) if (cache_entry is not None and cache_entry.persistent and template.link is template.original_link): ireq.original_link_is_in_wheel_cache = True super().__init__( link=link, source_link=source_link, ireq=ireq, factory=factory, name=name, version=version, )
def __init__(self, name, version, metadata=None, deps=None): self.name = Package.sanitize_name(name) self.metadata = metadata if type(version) == str: self.version = Version(version) else: self.version = version self.deps = deps or [] self.incoming = [] self.incoming_mismatched = []
def _check_pip_version(raise_on_incompatible=False): # type: (bool) -> bool """Check pip version running.""" if Version(pip_version) not in _SUPPORTED_PIP: msg = "pip in version {!r} not tested, tested versions: {!r}".format(pip_version, _SUPPORTED_PIP_STR) if raise_on_incompatible: raise CompatibilityError(msg) _LOGGER.warning(msg) return False return True
def version_satisfies_spec(spec, version): if not spec: return True if not version: return False if not isinstance(spec, SpecifierSet): spec = SpecifierSet(spec) if not isinstance(version, Version): version = Version(version) return spec.contains(version)
def _get_metadata_version(dist): # type: (Distribution) -> Optional[Version] for line in dist.get_metadata_lines(dist.PKG_INFO): if line.lower().startswith("metadata-version:"): value = line.split(":", 1)[-1].strip() try: return Version(value) except InvalidVersion: msg = "Invalid Metadata-Version: {}".format(value) raise UnsupportedWheel(msg) raise UnsupportedWheel("Missing Metadata-Version")
def get_missing_pip_packages(platform, dependency_set): """ Check for missing python modules """ failed_deps = [] for w3af_req in platform.PIP_PACKAGES[dependency_set]: for dist in pkg_resources.working_set: if w3af_req.package_name.lower() == dist.project_name.lower(): w3af_req_version = str(Version(w3af_req.package_version)) dist_version = str(Version(dist.version)) if w3af_req_version == dist_version: # It's installed and the version matches! break else: failed_deps.append(w3af_req) return failed_deps
def build_dependency_version_string(mixed): if isinstance(mixed, Dependency): version = mixed.version else: version = mixed if not version: return "" try: return ">=%s" % Version(version) except InvalidVersion: try: return str(SpecifierSet(version)) except InvalidSpecifier: raise ValueError("'%s' must be either PEP 0440 version or a version specifier set")
def test_core_logic( installed_version: str, remote_version: str, stored_version: Optional[str], installed_by_pip: bool, should_show_prompt: bool, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch, ) -> None: # GIVEN monkeypatch.setattr(self_outdated_check, "was_installed_by_pip", lambda _: installed_by_pip) mock_state = Mock() mock_state.get.return_value = stored_version fake_time = datetime.datetime(2000, 1, 1, 0, 0, 0) version_that_should_be_checked = stored_version or remote_version # WHEN with caplog.at_level(logging.DEBUG): return_value = self_outdated_check._self_version_check_logic( state=mock_state, current_time=fake_time, local_version=Version(installed_version), get_remote_version=lambda: remote_version, ) # THEN mock_state.get.assert_called_once_with(fake_time) assert caplog.messages == [ f"Remote version of pip: {version_that_should_be_checked}", f"Local version of pip: {installed_version}", f"Was pip installed by pip? {installed_by_pip}", ] if stored_version: mock_state.set.assert_not_called() else: mock_state.set.assert_called_once_with(version_that_should_be_checked, fake_time) if not should_show_prompt: assert return_value is None return # the remaining assertions are for the other case. assert return_value is not None assert return_value.old == installed_version assert return_value.new == remote_version
def __init__( self, link: Link, template: InstallRequirement, factory: "Factory", name: Optional[NormalizedName] = None, version: Optional[CandidateVersion] = None, ) -> None: source_link = link cache_entry = factory.get_wheel_cache_entry(link, name) if cache_entry is not None: logger.debug("Using cached wheel link: %s", cache_entry.link) link = cache_entry.link ireq = make_install_req_from_link(link, template) assert ireq.link == link if ireq.link.is_wheel and not ireq.link.is_file: wheel = Wheel(ireq.link.filename) wheel_name = canonicalize_name(wheel.name) assert name == wheel_name, f"{name!r} != {wheel_name!r} for wheel" # Version may not be present for PEP 508 direct URLs if version is not None: wheel_version = Version(wheel.version) assert version == wheel_version, "{!r} != {!r} for wheel {}".format( version, wheel_version, name ) if cache_entry is not None: if cache_entry.persistent and template.link is template.original_link: ireq.original_link_is_in_wheel_cache = True if cache_entry.origin is not None: ireq.download_info = cache_entry.origin else: # Legacy cache entry that does not have origin.json. # download_info may miss the archive_info.hash field. ireq.download_info = direct_url_from_link( source_link, link_is_in_wheel_cache=cache_entry.persistent ) super().__init__( link=link, source_link=source_link, ireq=ireq, factory=factory, name=name, version=version, )
def test_invalid_pip_version_in_python_executable( get_pip_version_for_python_executable, runner): with open("requirements.txt", "w") as req_in: req_in.write("small-fake-a==1.10.0") custom_executable = os.path.abspath("custom_executable") with open(custom_executable, "w") as exec_file: exec_file.write("") os.chmod(custom_executable, 0o700) get_pip_version_for_python_executable.return_value = Version("19.1") out = runner.invoke(cli, ["--python-executable", custom_executable]) assert out.exit_code == 2, out message = ( "Target python executable '{}' has pip version 19.1 installed. " "Version" # ">=20.3 is expected.\n" part is omitted ) assert out.stderr.startswith(message.format(custom_executable))
def current_version(req): """Get the current version from an InstallRequirement instance. Returns a tuple (ver, eq_ver, gt_ver, gte_ver, lt_ver, lte_ver, not_ver). The versions in the returned tuple will be either a pip.req.req_install.Version instance or None. :param req: Instance of pip.req.req_install.InstallRequirement. """ eq_ver = None gt_ver = None gte_ver = None lt_ver = None lte_ver = None not_ver = None for spec in req.req.specifier: operator, version = spec._spec try: ver = Version(version) # TODO: use parse to support LegacyVersion except InvalidVersion: continue if operator == '==': eq_ver = ver elif operator == '>': if not gt_ver or ver > gt_ver: gt_ver = ver elif operator == '>=': if not gte_ver or ver > gte_ver: gte_ver = ver elif operator == '<': if not lt_ver or ver < lt_ver: lt_ver = ver elif operator == '<=': if not lte_ver or ver < lte_ver: lte_ver = ver elif operator == '!=': not_ver = ver ver = eq_ver or gt_ver or gte_ver or lt_ver or lte_ver or not_ver return ver, eq_ver, gt_ver, gte_ver, lt_ver, lte_ver, not_ver
def _verify_one(req, wheel_path): # type: (InstallRequirement, str) -> None canonical_name = canonicalize_name(req.name) w = Wheel(os.path.basename(wheel_path)) if canonicalize_name(w.name) != canonical_name: raise InvalidWheelFilename( "Wheel has unexpected file name: expected {!r}, " "got {!r}".format(canonical_name, w.name), ) with zipfile.ZipFile(wheel_path, allowZip64=True) as zf: dist = pkg_resources_distribution_for_wheel( zf, canonical_name, wheel_path, ) if canonicalize_version(dist.version) != canonicalize_version(w.version): raise InvalidWheelFilename( "Wheel has unexpected file name: expected {!r}, " "got {!r}".format(dist.version, w.version), ) if _get_metadata_version(dist) >= Version("1.2") and not isinstance( dist.parsed_version, Version): raise UnsupportedWheel("Metadata 1.2 mandates PEP 440 version, " "but {!r} is not".format(dist.version))
def dependency_check(dependency_set=CORE, exit_on_failure=True): """ This function verifies that the dependencies that are needed by the framework core are met. :return: True if the process should exit """ disable_warnings() platform = get_current_platform() # # Check for missing python modules # failed_deps = [] pip_distributions = pip.get_installed_distributions(local_only=False) for w3af_req in platform.PIP_PACKAGES[dependency_set]: for dist in pip_distributions: if w3af_req.package_name.lower() == dist.project_name.lower(): w3af_req_version = str(Version(w3af_req.package_version)) dist_version = str(dist.version) if w3af_req_version == dist_version: # It's installed and the version matches! break else: failed_deps.append(w3af_req) # # Check for missing operating system packages # missing_os_packages = [] for os_package in platform.SYSTEM_PACKAGES[dependency_set]: if not platform.os_package_is_installed(os_package): missing_os_packages.append(os_package) os_packages = list(set(missing_os_packages)) # All installed? if not failed_deps and not os_packages: # False means: do not exit() enable_warnings() return False generate_requirements_txt(failed_deps) script_path = generate_helper_script(platform.PKG_MANAGER_CMD, os_packages, platform.PIP_CMD, failed_deps) # # Report the missing system packages # msg = ('w3af\'s requirements are not met, one or more third-party' ' libraries need to be installed.\n\n') if os_packages: missing_pkgs = ' '.join(os_packages) msg += ('On %s systems please install the following operating' ' system packages before running the pip installer:\n' ' %s %s\n') print(msg % (platform.SYSTEM_NAME, platform.PKG_MANAGER_CMD, missing_pkgs)) # # Report all missing python modules # if failed_deps: # pylint: disable=E1101 msg = ('Your python installation needs the following modules' ' to run w3af:\n') msg += ' ' + ' '.join([fdep.module_name for fdep in failed_deps]) print(msg) print('\n') # pylint: enable=E1101 # # Report missing pip packages # not_git_pkgs = [fdep for fdep in failed_deps if not fdep.is_git] git_pkgs = [fdep.git_src for fdep in failed_deps if fdep.is_git] msg = ('After installing any missing operating system packages, use' ' pip to install the remaining modules:\n') if not_git_pkgs: cmd = generate_pip_install_non_git(platform.PIP_CMD, not_git_pkgs) msg += ' %s\n' % cmd if git_pkgs: for missing_git_pkg in git_pkgs: msg += ' %s\n' % generate_pip_install_git( platform.PIP_CMD, missing_git_pkg) print(msg) msg = 'A script with these commands has been created for you at %s' print(msg % script_path) enable_warnings() platform.after_hook() if exit_on_failure: sys.exit(1) else: return True
def test_version_no_match(self): plugin_module = mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = ">=1.2.5" self.assertRaises(IncompatiblePluginException, _check_plugin_version, plugin_module, "test plugin")
def test_version_multiple_specifier_match(self): plugin_module = mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = ">=1.2.0,<=1.2.4" _check_plugin_version(plugin_module, "test plugin")
def test_version_compatible_match(self): plugin_module = mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = "~=1.2" _check_plugin_version(plugin_module, "test plugin")
def test_version_exact_match(self): plugin_module = mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = "===1.2.3" _check_plugin_version(plugin_module, "test plugin")
def is_poetry_version_valid(poetry_version: str, latest_tag: str) -> bool: """Validate if Poetry version is greater than the latest tag.""" return Version(poetry_version) > Version(latest_tag)
cache_entry = factory.get_wheel_cache_entry(link, name) if cache_entry is not None: logger.debug("Using cached wheel link: %s", cache_entry.link) link = cache_entry.link ireq = make_install_req_from_link(link, template) <<<<<<< HEAD assert ireq.link == link if ireq.link.is_wheel and not ireq.link.is_file: wheel = Wheel(ireq.link.filename) wheel_name = canonicalize_name(wheel.name) assert name == wheel_name, ( "{!r} != {!r} for wheel".format(name, wheel_name) ) # Version may not be present for PEP 508 direct URLs if version is not None: wheel_version = Version(wheel.version) assert version == wheel_version, ( "{!r} != {!r} for wheel {}".format( version, wheel_version, name ) ) ======= >>>>>>> 74c061954d5e927be4caafbd793e96a50563c265 if (cache_entry is not None and cache_entry.persistent and template.link is template.original_link): ireq.original_link_is_in_wheel_cache = True super(LinkCandidate, self).__init__( link=link,
def test_get_pip_version_for_python_executable(): result = get_pip_version_for_python_executable(sys.executable) assert Version(pip.__version__) == result
from pip._vendor.packaging.specifiers import SpecifierSet from pip._vendor.packaging.version import Version from pybuilder import __version__ as pyb_version from pybuilder.errors import ( MissingPluginException, IncompatiblePluginException, UnspecifiedPluginNameException, ) from pybuilder.utils import execute_command, read_file PYPI_PLUGIN_PROTOCOL = "pypi:" VCS_PLUGIN_PROTOCOL = "vcs:" if pyb_version == "${dist_version}": # This is the case of PyB bootstrap PYB_VERSION = Version('0.0.1.dev0') else: PYB_VERSION = Version(pyb_version) class PluginLoader(object): def __init__(self, logger): self.logger = logger def load_plugin(self, project, name, version=None, plugin_module_name=None): pass