Beispiel #1
0
    def test_tostring(self):
        a = Artifact("foo:bar")
        assert str(a) == "foo:bar"

        a = Artifact("foo:bar:1")
        assert str(a) == "foo:bar:jar:1"

        a = Artifact("foo:bar:pom:1")
        assert str(a) == "foo:bar:pom:1"

        a = Artifact("foo:bar:pom:sources:1")
        assert str(a) == "foo:bar:pom:sources:1"
Beispiel #2
0
def fetch_artifact_files(_dest_root_dir: str, artifact: Artifact, parent: Artifact = None):
    print("fetching artifacts : {}".format(artifact))

    _dest_dir = os.path.join(_dest_root_dir, artifact.dir_format())

    os.makedirs(_dest_dir, exist_ok=True)

    # group_id/artifact_id/version/artifact_id-version.pom
    # .pom, .{packaging}, -sources.jar

    base_url = artifact.dir_format()

    _dest_file = os.path.join(_dest_dir, artifact.pom_file())

    fetch_file(base_url + "/" + artifact.pom_file(), _dest_file)

    pom_artifact = pom.parse(_dest_file)

    if pom_artifact.packaging != Packaging.pom:
        # artifact file (e.g. jar)
        artifact_file = pom_artifact.filename()
        _dest_file = os.path.join(_dest_dir, artifact_file)

        fetch_file(base_url + "/" + artifact_file, _dest_file)

        # sources file (e.g. *-sources.jar)
        artifact_file = pom_artifact.sources_file()
        _dest_file = os.path.join(_dest_dir, artifact_file)

        fetch_file(base_url + "/" + artifact_file, _dest_file)

    for dependency in pom_artifact.dependencies:
        print("fetching dependency: {}".format(dependency))

        if parent is not None and parent.equals(dependency):
            print("circular dependency detected {} to {}".format(parent, dependency))
            continue

        try:
            fetch_artifact_files(_dest_root_dir, dependency, parent=pom_artifact)
        except RecursionError as err:
            print(err)
            pass
Beispiel #3
0
    def test_path(self):
        test_pairs = (
            ("foo.bar:baz", "foo/bar/baz"),
            ("foo.bar:baz:1", "foo/bar/baz/1/baz-1.jar"),
            ("foo.bar:baz:pkg:1", "foo/bar/baz/1/baz-1.pkg"),
            ("foo.bar:baz:pkg:sources:1", "foo/bar/baz/1/baz-1-sources.pkg"),
            ("foo.bar:baz:pkg:javadoc:1", "foo/bar/baz/1/baz-1-javadoc.pkg"),
            ("foo.bar:baz:[1,2)", "foo/bar/baz"),
        )

        for input, expected in test_pairs:
            artifact = Artifact(input)
            assert artifact.path == expected
Beispiel #4
0
def parse_group_index(_file: str) -> List[Artifact]:
    print("parsing group: {}".format(_file))

    with open(_file) as f:
        # force utf-8, otherwise fromstring throws the following error
        # Unicode strings with encoding declaration are not supported
        xml_string = f.read().encode("utf-8")

    xml_obj = objectify.fromstring(xml_string)

    group_id = xml_obj.tag

    artifacts = []
    for artifact in xml_obj.getchildren():
        for version in artifact.attrib["versions"].split(","):
            artifacts.append(Artifact(group_id, artifact.tag, version))

    return artifacts
Beispiel #5
0
    def test_comparison(self):
        test_pairs = (
            # compare group id
            (Artifact("f:a:1"), Artifact("g:a:1")),
            # compare artifact id
            (Artifact("g:a:1"), Artifact("g:b:1")),
            # compare type
            (Artifact("g:a:1"), Artifact("g:a:pom:1")),
            (Artifact("g:a:jar:1"), Artifact("g:a:pom:1")),
            (Artifact("g:a:pom:1"), Artifact("g:a:war:1")),
            # compare classifier
            (Artifact("g:a:jar:c:1"), Artifact("g:a:1")),
            (Artifact("g:a:jar:a:1"), Artifact("g:a:jar:c:1")),
            # compare version
            (Artifact("g:a:1"), Artifact("g:a:2")),
            # mask version
            (Artifact("f:a:2"), Artifact("g:a:1")),
            (Artifact("g:a:2"), Artifact("g:b:1")),
            (Artifact("g:a:2"), Artifact("g:a:pom:1")),
            (Artifact("g:a:jar:2"), Artifact("g:a:pom:1")),
            (Artifact("g:a:pom:2"), Artifact("g:a:war:1")),
            (Artifact("g:a:jar:c:2"), Artifact("g:a:1")),
            (Artifact("g:a:jar:a:2"), Artifact("g:a:jar:c:1")),
        )

        for pair in test_pairs:
            self._assertArtifactOrder(*pair)

        # verify identity
        a = Artifact("foo:bar:1")
        assert not a < a
        assert a <= a
        assert a >= a
        assert a == a
        assert not a != a

        # compare to non-artifact
        assert a > "aardvark"
        assert a != "aardvark"
        assert "aardvark" < a
        assert "aardvark" != a
        assert a > 10
        assert a != 10
        assert 10 < a
        assert 10 != a
Beispiel #6
0
    def test_dependency_version_range(self):
        client = self._mock_client(COM_TEST_PROJECT3, COM_TEST_PROJECT2)
        client.find_artifacts.return_value = [
            Artifact("com.test:project2:2.0.0"),
            Artifact("com.test:project2:1.0.0"),
        ]
        pom = Pom("com.test:project3:1.0.0", client)

        deps = list(pom.dependencies["compile"])
        assert len(pom.dependencies) == 1
        assert len(deps) == 1
        assert deps[0] == (("com.test", "project2", "[1.0,2.0)"), True)

        client = self._mock_client(COM_TEST_PROJECT4)
        client.find_artifacts.return_value = [
            Artifact("com.test:project2:2.0.0-SNAPSHOT"),
            Artifact("com.test:project2:1.0.0"),
        ]
        pom = Pom("com.test:project4:1.0.0", client)

        deps = list(pom.dependencies["compile"])
        assert len(pom.dependencies) == 1
        assert len(deps) == 1
        assert deps[0] == (("com.test", "project2", "release"), True)

        client = self._mock_client(
            COM_TEST_PROJECT4.replace("version>release", "version>latest"))
        client.find_artifacts.return_value = [
            Artifact("com.test:project2:2.0.0-SNAPSHOT"),
            Artifact("com.test:project2:1.0.0"),
        ]
        pom = Pom("com.test:project4:1.0.0", client)

        deps = list(pom.dependencies["compile"])
        assert len(pom.dependencies) == 1
        assert len(deps) == 1
        assert deps[0] == (("com.test", "project2", "latest"), True)

        client = self._mock_client(
            COM_TEST_PROJECT4.replace("version>release",
                                      "version>latest.release"))
        client.find_artifacts.return_value = [
            Artifact("com.test:project2:2.0.0-SNAPSHOT"),
            Artifact("com.test:project2:1.0.0"),
        ]
        pom = Pom("com.test:project4:1.0.0", client)

        deps = list(pom.dependencies["compile"])
        assert len(pom.dependencies) == 1
        assert len(deps) == 1
        assert deps[0] == (("com.test", "project2", "latest.release"), True)

        client = self._mock_client(
            COM_TEST_PROJECT4.replace("version>release",
                                      "version>latest.integration"))
        client.find_artifacts.return_value = [
            Artifact("com.test:project2:2.0.0-SNAPSHOT"),
            Artifact("com.test:project2:1.0.0"),
        ]
        pom = Pom("com.test:project4:1.0.0", client)

        deps = list(pom.dependencies["compile"])
        assert len(pom.dependencies) == 1
        assert len(deps) == 1
        assert deps[0] == (("com.test", "project2", "latest.integration"),
                           True)
Beispiel #7
0
def parse(_file: str) -> Artifact:
    with open(_file) as f:
        # force utf-8, otherwise fromstring throws the following error
        # Unicode strings with encoding declaration are not supported
        xml_string = f.read().encode("utf-8")

    if not xml_string:
        print("pom file is empty: {}".format(_file))
        return Artifact()

    try:
        xml_obj = objectify.fromstring(xml_string)
    except Exception as err:
        print(err)
        return Artifact()

    # build artifact
    artifact = Artifact()

    try:
        artifact.group_id = xml_obj.groupId.text
    except AttributeError:
        artifact.group_id = xml_obj.parent.groupId.text

    artifact.artifact_id = xml_obj.artifactId.text

    try:
        artifact.version = xml_obj.version.text
    except AttributeError:
        artifact.version = xml_obj.parent.version.text

    artifact.version = artifact.version.translate(
        str.maketrans(dict.fromkeys("[]")))

    try:
        artifact.packaging = Packaging[xml_obj.packaging.text.lower()]
    except AttributeError:
        pass

    try:
        for dep in xml_obj.dependencies:
            dep = dep.dependency

            version = dep.version.text.translate(
                str.maketrans(dict.fromkeys("[]")))

            # we need to pull this from properties
            if "$" in version:
                version = version.translate(str.maketrans(
                    dict.fromkeys("${}")))
                version = xml_obj.properties[version].text

            d = Artifact(dep.groupId.text, dep.artifactId.text, version)
            d.scope = Scope[dep.scope.text.lower()]

            artifact.dependencies.append(d)
    except AttributeError:
        pass

    return artifact
Beispiel #8
0
    def test_find_artifacts(self, _LocalRepository):
        _repo1 = mock.Mock(spec=LocalRepository)
        _repo2 = mock.Mock(spec=LocalRepository)

        _LocalRepository.side_effect = [_repo1, _repo2]

        _repo1.get_versions.return_value = [Artifact("foo:bar:2.0"),
                                            Artifact("foo:bar:2.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]

        _repo2.get_versions.return_value = [Artifact("foo:bar:3.0"),
                                            Artifact("foo:bar:3.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]
        client = MavenClient("foobar", "foobaz")
        expected = [Artifact("foo:bar:3.0"),
                    Artifact("foo:bar:3.0-SNAPSHOT"),
                    Artifact("foo:bar:2.0"),
                    Artifact("foo:bar:2.0-SNAPSHOT"),
                    Artifact("foo:bar:1.0"),
                    Artifact("foo:bar:1.0-SNAPSHOT"),
                    ]
        actual = client.find_artifacts("foo:bar")
        assert expected == actual, "client.find_artifacts(%s)" % input
Beispiel #9
0
    def test_get_versions(self, _os):
        _os.listdir.return_value = ["1.0-SNAPSHOT",
                                    "2.0.0",
                                    "3.0-SNAPSHOT",
                                    "1.1",
                                    "1.0",
                                    ]
        repo = LocalRepository("/maven")

        for input, expected in (
                ("foo:bar", [Artifact("foo:bar:3.0-SNAPSHOT"),
                             Artifact("foo:bar:2.0.0"),
                             Artifact("foo:bar:1.1"),
                             Artifact("foo:bar:1.0"),
                             Artifact("foo:bar:1.0-SNAPSHOT"),
                             ]),
                ("foo:bar:1.0", [Artifact("foo:bar:1.0")]),
                ("foo:bar:[1.0]", [Artifact("foo:bar:1.0")]),
                ("foo:bar:[1.0,2.0)", [Artifact("foo:bar:1.1"),
                                       Artifact("foo:bar:1.0"),
                                       ]),
                ("foo:bar:[2.0,3.0)", [Artifact("foo:bar:3.0-SNAPSHOT"),
                                       Artifact("foo:bar:2.0.0"),
                                       ]),
                ):
            actual = repo.get_versions(input)
            assert expected == actual, \
                "LocalRepository.get_versions(%s)" % input
Beispiel #10
0
    def test_get_versions(self, _request):
        res = mock.MagicMock(spec=Struct)
        res.__enter__.return_value = StringIO(SIMPLE_METADATA)
        _request.return_value = res

        repo = HttpRepository("http://foo.com/repo")
        for input, expected in (
                ("foo:bar", [Artifact("foo:bar:3.0-SNAPSHOT"),
                             Artifact("foo:bar:2.0.0"),
                             Artifact("foo:bar:1.1"),
                             Artifact("foo:bar:1.0"),
                             Artifact("foo:bar:1.0-SNAPSHOT"),
                             ]),
                ("foo:bar:1.0", [Artifact("foo:bar:1.0")]),
                ("foo:bar:[1.0]", [Artifact("foo:bar:1.0")]),
                ("foo:bar:[1.0,2.0)", [Artifact("foo:bar:1.1"),
                                       Artifact("foo:bar:1.0"),
                                       ]),
                ("foo:bar:[2.0,3.0)", [Artifact("foo:bar:3.0-SNAPSHOT"),
                                       Artifact("foo:bar:2.0.0"),
                                       ]),
                ):
            actual = repo.get_versions(input)
            assert expected == actual, "HttpRepository.get_versions(%s)" % input
            # reset res contents
            res.__enter__.return_value.seek(0)