Esempio n. 1
0
def _use_pom_resolution(context: DependencyContext, dependency: Dependency, classified_name: str, base_name: str) \
        -> Optional[DependencyPathSet]:
    """
    A function that resolves Java dependencies using POM files (the old way).

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param classified_name: the classified base name for the main asset.
    :param base_name: the base name for file assets.
    :return: the appropriate dependency path set or ``None``.
    """
    jar_file = context.to_local_path(dependency, f'{classified_name}.jar')

    if not jar_file:
        return None

    # Ok, we're good to go.
    # First, let's see if there's a POM file that can tell us about dependencies.
    pom_file = context.to_local_path(dependency, f'{base_name}.pom')

    if pom_file and not dependency.ignore_transients:
        read_pom_for_dependencies(pom_file, context, dependency)

    # Now, let's create and load up our result:
    result = DependencyPathSet(dependency, jar_file)

    _try_to_add_secondary_path(context, dependency, 'sources', f'{base_name}-sources.jar', result)
    _try_to_add_secondary_path(context, dependency, 'javadoc', f'{base_name}-javadoc.jar', result)

    return result
Esempio n. 2
0
def _get_remote_version_info(context: DependencyContext, dependency: Dependency) -> \
        Optional[Tuple[Version, List[Version]]]:
    """
    A function that determines the available and latest version numbers for the given
    remote dependency.  This is achieved by retrieving the ``maven-metadata.xml`` file
    for the dependency from Maven.

    :param context: the current context to use in pulling down remote files.
    :param dependency: the dependency to get the version information for.
    :return: Either ``None``, if version information could not be determined or a tuple
    containing the latest version in the 1st position and the list of available versions
    in the 2nd.
    """
    metadata_path = context.to_local_path(dependency, 'maven-metadata.xml')

    if metadata_path:
        data = ElementTree.parse(metadata_path).getroot()
        versioning = data.find('versioning')
        latest = versioning.find('latest')
        versions = versioning.find('versions')
        versions = [
            Version(version.text) for version in versions.iter('version')
        ]

        return Version(latest.text), versions

    return None
Esempio n. 3
0
    def test_to_local_file_bad_passed_signatures(self, tmpdir):
        directory = Path(str(tmpdir))
        path = directory / 'file.txt'
        mock_fetch = MagicMock(return_value=path)
        dep = _make_dep()
        context = DependencyContext([], Language({}, 'lang'),
                                    Configuration({}, [], None))
        context._fetch_file = mock_fetch

        path.write_text("file content.\n")

        with pytest.raises(ValueError) as info:
            context.to_local_path(dep, 'file.txt', {'sha512': 'bad-signature'})

        assert info.value.args[
            0] == 'Dependency path:\n\nCould not verify the signature of the file file.txt.'
        assert mock_fetch.mock_calls == [call(dep, 'file.txt')]
Esempio n. 4
0
    def test_to_local_file_no_path(self):
        mock_fetch = MagicMock(return_value=None)
        dep = _make_dep()
        context = DependencyContext([], Language({}, 'lang'),
                                    Configuration({}, [], None))

        context._fetch_file = mock_fetch

        assert context.to_local_path(dep, 'file.txt') is None

        mock_fetch.assert_called_once_with(dep, 'file.txt')
Esempio n. 5
0
    def test_to_local_file_empty_signatures(self):
        path = 'file.txt'
        mock_fetch = MagicMock(return_value=path)
        dep = _make_dep()
        context = DependencyContext([], Language({}, 'lang'),
                                    Configuration({}, [], None))

        context._fetch_file = mock_fetch

        assert context.to_local_path(dep, 'file.txt', {}) is path

        mock_fetch.assert_called_once_with(dep, 'file.txt')
Esempio n. 6
0
    def test_to_local_file_good_passed_signatures(self, tmpdir):
        directory = Path(str(tmpdir))
        path = directory / 'file.txt'
        mock_fetch = MagicMock(return_value=path)
        dep = _make_dep()
        context = DependencyContext([], Language({}, 'lang'),
                                    Configuration({}, [], None))
        context._fetch_file = mock_fetch

        path.write_text("file content.\n")

        signatures = sign_path(path)

        assert context.to_local_path(dep, 'file.txt', signatures) is path
        assert mock_fetch.mock_calls == [call(dep, 'file.txt')]
Esempio n. 7
0
def resolve(context: DependencyContext, dependency: Dependency) -> Optional[DependencyPathSet]:
    """
    A function to resolve dependencies in Java-land.

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :return: the appropriate dependency path set or ``None``.
    """
    remote_resolver, classified_name, base_name = build_names(dependency)

    context.set_remote_resolver(remote_resolver)

    module_path = context.to_local_path(dependency, f'{base_name}.module')

    return _use_module_resolution(context, dependency, module_path) if module_path else \
        _use_pom_resolution(context, dependency, classified_name, base_name)
Esempio n. 8
0
def _try_to_add_secondary_path(context: DependencyContext, dependency: Dependency, key: str, name: str,
                               path_set: DependencyPathSet, signatures: Optional[Dict[str, str]] = None):
    """
    A function that attempts to load a secondary path (sources or javadoc) and, if
    successful, adds them to the given path set.

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param key: the key by which the secondary path will be known.
    :param name: the name of the secondary path.
    :param path_set: the path set to add a successfully isolated path to.
    :param signatures: the set of signatures to verify against (if any).
    """
    path = context.to_local_path(dependency, name, signatures)

    if path:
        path_set.add_secondary_path(key, path)
Esempio n. 9
0
    def test_to_local_file_good_file_signatures(self, tmpdir):
        directory = Path(str(tmpdir))
        path = directory / 'file.txt'
        file_names = ['file.txt']
        file_names.extend([f'file.txt.{sn}' for sn in supported_signatures])
        mock_fetch = MagicMock(
            side_effect=[directory / name for name in file_names])
        dep = _make_dep()
        context = DependencyContext([], Language({}, 'lang'),
                                    Configuration({}, [], None))
        context._fetch_file = mock_fetch
        file_names = ['file.txt']
        file_names.extend([f'file.txt.{sn}' for sn in supported_signatures])

        path.write_text("file content.\n")

        sign_path_to_files(path)

        assert context.to_local_path(dep, 'file.txt') == path
        assert mock_fetch.mock_calls == [
            call(dep, 'file.txt'),
            call(dep, f'file.txt.{supported_signatures[0]}')
        ]
Esempio n. 10
0
def _use_module_resolution(context: DependencyContext, dependency: Dependency, module_path: Path) \
        -> Optional[DependencyPathSet]:
    """
    A function that resolves Java dependencies using module files (the new way).

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param module_path: the local path to our module file.
    :return: the appropriate dependency path set or ``None``.
    """
    module_data = ModuleData.from_path(module_path)
    variant = module_data.get_variant(API_ELEMENTS)

    # If there's not an API variant, then we really can't do anything.
    if not variant:
        return None

    jar_variant_file = variant.files[0]
    jar_file = context.to_local_path(dependency, jar_variant_file.name, jar_variant_file.signatures)

    # If we couldn't get the jar file, report same.
    if not jar_file:
        return None

    # Ok, let's process any dependencies that may be involved.
    if not dependency.ignore_transients:
        for transient_dependency in variant.dependencies:
            context.add_dependency(transient_dependency.as_dependency(dependency))

    # Now, let's create and load up our result:
    result = DependencyPathSet(dependency, jar_file)

    _try_for_variant(context, dependency, module_data, SOURCE_ELEMENTS, result)
    _try_for_variant(context, dependency, module_data, JAVADOC_ELEMENTS, result)

    return result