All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metaeffekt.artifact.resolver.pypi.PyPIArtifactReference Maven / Gradle / Ivy

There is a newer version: 0.134.0
Show newest version
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metaeffekt.artifact.resolver.pypi;

import com.github.packageurl.PackageURL;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.metaeffekt.artifact.resolver.generic.FileLocation;
import org.metaeffekt.core.inventory.processor.model.Artifact;
import org.metaeffekt.core.inventory.processor.model.Constants;

public class PyPIArtifactReference {

    // FIXME: currently does not support architecture/platform selection
    //  SomeProject==5.4; sys_platform == 'win32'
    //  See also https://peps.python.org/pep-0508/#examples

    @Getter
    private String name;

    @Getter
    private String version;

    public PyPIArtifactReference(String name, String version) {
        this.name = name;
        this.version = version;
    }

    public PyPIArtifactReference(PackageURL packageURL) {
        if (packageURL.getType().equalsIgnoreCase(PackageURL.StandardTypes.PYPI)) {
            name = packageURL.getName();
            version = packageURL.getVersion();
        } else {
            name = null;
            version = null;
        }
    }

    public PyPIArtifactReference(Artifact artifact) {
        if (!"python-package".equals(artifact.get(Constants.KEY_TYPE))) return;

        final String id = artifact.getId();
        if (id != null) {
            final int slashEndIndex = id.lastIndexOf("/");
            version = artifact.getVersion();
            if (slashEndIndex != -1) {
                name = id.substring(slashEndIndex + 1).replace("-" + version, "");
            } else {
                name = id.replace("-" + version, "");
            }
        } else {
            name = null;
            version = null;
        }
    }

    public String derivePipRequirementSpec() {
        return String.format("%s==%s", name, version);
    }

    public boolean isValid() {
        return StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(version);
    }

    public String deriveMarkerFileName(String modifier) {
        return String.format("%s-%s-%s.marker", name, version, modifier);
    }

    public FileLocation deriveFileLocation() {
        final FileLocation fileLocation = new FileLocation();
        fileLocation.setEcosystem("pypi");
        final String qualifier = getName() + "-" + getVersion();
        fileLocation.setFilename(qualifier);
        fileLocation.setResourceQualifier(qualifier);
        fileLocation.setDispatchName(getName());
        return fileLocation;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy