Search in sources :

Example 1 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class PipInspectorTreeParser method parseDependencyFromLine.

private Dependency parseDependencyFromLine(final String line, final String sourcePath) {
    final String[] segments = line.split(SEPARATOR);
    String name = segments[0].trim();
    String version = segments[1].trim();
    ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.PYPI, name, version);
    if (name.equals(UNKNOWN_PROJECT_NAME) || version.equals(UNKNOWN_PROJECT_VERSION)) {
        externalId = externalIdFactory.createPathExternalId(Forge.PYPI, sourcePath);
    }
    name = name.equals(UNKNOWN_PROJECT_NAME) ? "" : name;
    version = version.equals(UNKNOWN_PROJECT_VERSION) ? "" : version;
    return new Dependency(name, version, externalId);
}
Also used : ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 2 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class PipenvGraphParser method getDependencyFromLine.

Optional<Dependency> getDependencyFromLine(final Map<String, String[]> pipFreezeMap, final String line) {
    Dependency dependency = null;
    String name = null;
    String version = null;
    final String trimmedLine = line.trim();
    if (line.contains(DEPENDENCY_NAME_PREFIX) && line.contains(DEPENDENCY_NAME_SUFFIX) && line.contains(DEPENDENCY_VERSION_PREFIX) && line.contains(DEPENDENCY_VERSION_SUFFIX)) {
        name = trimmedLine.substring(trimmedLine.indexOf(DEPENDENCY_NAME_PREFIX) + DEPENDENCY_NAME_PREFIX.length(), trimmedLine.indexOf(DEPENDENCY_NAME_SUFFIX));
        version = trimmedLine.substring(trimmedLine.indexOf(DEPENDENCY_VERSION_PREFIX) + DEPENDENCY_VERSION_PREFIX.length(), trimmedLine.indexOf(DEPENDENCY_VERSION_SUFFIX));
    } else if (trimmedLine.contains(TOP_LEVEL_SEPARATOR)) {
        final String[] splitLine = trimmedLine.split(TOP_LEVEL_SEPARATOR);
        name = splitLine[0];
        version = splitLine[1];
    }
    if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(version)) {
        final String[] pipFreezeResult = pipFreezeMap.get(name.toLowerCase());
        if (pipFreezeResult != null) {
            name = pipFreezeResult[0].trim();
            version = pipFreezeResult[1].trim();
        }
        final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.PYPI, name, version);
        dependency = new Dependency(name, version, externalId);
    }
    return Optional.ofNullable(dependency);
}
Also used : ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 3 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class PipenvGraphParser method parse.

public PipParseResult parse(final String projectName, final String projectVersionName, final List<String> pipFreezeOutput, final List<String> pipenvGraphOutput, final String sourcePath) {
    final MutableMapDependencyGraph dependencyGraph = new MutableMapDependencyGraph();
    final Stack<Dependency> dependencyStack = new Stack<>();
    final Map<String, String[]> pipFreezeMap = pipFreezeOutput.stream().map(line -> line.split(TOP_LEVEL_SEPARATOR)).filter(splitLine -> splitLine.length == 2).collect(Collectors.toMap(splitLine -> splitLine[0].trim().toLowerCase(), splitLine -> splitLine));
    int lastLevel = -1;
    for (final String line : pipenvGraphOutput) {
        final int currentLevel = getLevel(line);
        final Optional<Dependency> parsedDependency = getDependencyFromLine(pipFreezeMap, line);
        if (!parsedDependency.isPresent()) {
            continue;
        }
        final Dependency dependency = parsedDependency.get();
        if (currentLevel == lastLevel) {
            dependencyStack.pop();
        } else {
            for (; lastLevel >= currentLevel; lastLevel--) {
                dependencyStack.pop();
            }
        }
        if (dependencyStack.size() > 0) {
            dependencyGraph.addChildWithParent(dependency, dependencyStack.peek());
        } else {
            dependencyGraph.addChildrenToRoot(dependency);
        }
        lastLevel = currentLevel;
        dependencyStack.push(dependency);
    }
    if (!dependencyGraph.getRootDependencyExternalIds().isEmpty()) {
        final ExternalId projectExternalId = externalIdFactory.createNameVersionExternalId(Forge.PYPI, projectName, projectVersionName);
        final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.PIP, sourcePath, projectExternalId, dependencyGraph).build();
        return new PipParseResult(projectName, projectVersionName, codeLocation);
    } else {
        return null;
    }
}
Also used : Logger(org.slf4j.Logger) Forge(com.synopsys.integration.bdio.model.Forge) LoggerFactory(org.slf4j.LoggerFactory) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Stack(java.util.Stack) List(java.util.List) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) Map(java.util.Map) DetectCodeLocationType(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocationType) Optional(java.util.Optional) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) Stack(java.util.Stack) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)

Example 4 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class Rebar3TreeParser method createDependencyFromLine.

protected Dependency createDependencyFromLine(final String line) {
    final String nameVersionLine = reduceLineToNameVersion(line);
    final String name = nameVersionLine.substring(0, nameVersionLine.lastIndexOf(HORIZONTAL_SEPARATOR_CHARACTER));
    final String version = nameVersionLine.substring(nameVersionLine.lastIndexOf(HORIZONTAL_SEPARATOR_CHARACTER) + 1);
    final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.HEX, name, version);
    return new Dependency(name, version, externalId);
}
Also used : ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 5 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class PipInspectorTreeParser method parse.

public Optional<PipParseResult> parse(final List<String> pipInspectorOutputAsList, final String sourcePath) {
    PipParseResult parseResult = null;
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();
    final DependencyHistory history = new DependencyHistory();
    Dependency project = null;
    for (final String line : pipInspectorOutputAsList) {
        final String trimmedLine = StringUtils.trimToEmpty(line);
        if (StringUtils.isEmpty(trimmedLine) || !trimmedLine.contains(SEPARATOR) || trimmedLine.startsWith(UNKNOWN_REQUIREMENTS_PREFIX) || trimmedLine.startsWith(UNPARSEABLE_REQUIREMENTS_PREFIX) || trimmedLine.startsWith(UNKNOWN_PACKAGE_PREFIX)) {
            parseErrorsFromLine(trimmedLine);
            continue;
        }
        final Dependency currentDependency = parseDependencyFromLine(trimmedLine, sourcePath);
        final int lineLevel = getLineLevel(trimmedLine);
        try {
            history.clearDependenciesDeeperThan(lineLevel);
        } catch (final IllegalStateException e) {
            logger.warn(String.format("Problem parsing line '%s': %s", line, e.getMessage()));
        }
        if (project == null) {
            project = currentDependency;
        } else if (project.equals(history.getLastDependency())) {
            graph.addChildToRoot(currentDependency);
        } else if (history.isEmpty()) {
            graph.addChildToRoot(currentDependency);
        } else {
            graph.addChildWithParents(currentDependency, history.getLastDependency());
        }
        history.add(currentDependency);
    }
    if (project != null) {
        final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.PIP, sourcePath, project.externalId, graph).build();
        parseResult = new PipParseResult(project.name, project.version, codeLocation);
    }
    return Optional.ofNullable(parseResult);
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) DependencyHistory(com.blackducksoftware.integration.hub.detect.util.DependencyHistory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Aggregations

Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)46 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)23 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)12 MutableMapDependencyGraph (com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)11 Test (org.junit.Test)11 MutableDependencyGraph (com.synopsys.integration.bdio.graph.MutableDependencyGraph)10 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)9 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)5 File (java.io.File)5 DependencyHistory (com.blackducksoftware.integration.hub.detect.util.DependencyHistory)4 ArrayList (java.util.ArrayList)3 Extraction (com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction)2 Gson (com.google.gson.Gson)2 Forge (com.synopsys.integration.bdio.model.Forge)2 NpmDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency)1 PackageLockDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 DetectCodeLocationType (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocationType)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1