Search in sources :

Example 11 with MutableMapDependencyGraph

use of com.synopsys.integration.bdio.graph.MutableMapDependencyGraph in project hub-detect by blackducksoftware.

the class GradleReportParser method parseDependencies.

public Optional<DetectCodeLocation> parseDependencies(final File codeLocationFile) {
    DetectCodeLocation codeLocation = null;
    String projectSourcePath = "";
    String projectGroup = "";
    String projectName = "";
    String projectVersionName = "";
    boolean processingMetaData = false;
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();
    final DependencyHistory history = new DependencyHistory();
    try (FileInputStream dependenciesInputStream = new FileInputStream(codeLocationFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(dependenciesInputStream, StandardCharsets.UTF_8))) {
        while (reader.ready()) {
            final String line = reader.readLine();
            /**
             * The meta data section will be at the end of the file after all of the "gradle dependencies" output
             */
            if (line.startsWith(DETECT_META_DATA_HEADER)) {
                processingMetaData = true;
                continue;
            }
            if (line.startsWith(DETECT_META_DATA_FOOTER)) {
                processingMetaData = false;
                continue;
            }
            if (processingMetaData) {
                if (line.startsWith(PROJECT_PATH_PREFIX)) {
                    projectSourcePath = line.substring(PROJECT_PATH_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_GROUP_PREFIX)) {
                    projectGroup = line.substring(PROJECT_GROUP_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_NAME_PREFIX)) {
                    projectName = line.substring(PROJECT_NAME_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_VERSION_PREFIX)) {
                    projectVersionName = line.substring(PROJECT_VERSION_PREFIX.length()).trim();
                }
                continue;
            }
            if (StringUtils.isBlank(line)) {
                history.clear();
                gradleReportConfigurationParser = new GradleReportConfigurationParser();
                continue;
            }
            final Dependency dependency = gradleReportConfigurationParser.parseDependency(externalIdFactory, line);
            if (dependency == null) {
                continue;
            }
            final int lineTreeLevel = gradleReportConfigurationParser.getTreeLevel();
            try {
                history.clearDependenciesDeeperThan(lineTreeLevel);
            } catch (final IllegalStateException e) {
                logger.warn(String.format("Problem parsing line '%s': %s", line, e.getMessage()));
            }
            if (history.isEmpty()) {
                graph.addChildToRoot(dependency);
            } else {
                graph.addChildWithParents(dependency, history.getLastDependency());
            }
            history.add(dependency);
        }
        final ExternalId id = externalIdFactory.createMavenExternalId(projectGroup, projectName, projectVersionName);
        codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.GRADLE, projectSourcePath, id, graph).build();
    } catch (final IOException e) {
        codeLocation = null;
    }
    return Optional.ofNullable(codeLocation);
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) InputStreamReader(java.io.InputStreamReader) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) DependencyHistory(com.blackducksoftware.integration.hub.detect.util.DependencyHistory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) BufferedReader(java.io.BufferedReader)

Example 12 with MutableMapDependencyGraph

use of com.synopsys.integration.bdio.graph.MutableMapDependencyGraph in project hub-detect by blackducksoftware.

the class NugetDependencyNodeBuilder method createDependencyGraph.

public DependencyGraph createDependencyGraph(final List<NugetPackageId> packageDependencies) {
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();
    if (packageSets != null) {
        for (final NugetPackageSet packageSet : packageSets) {
            if (packageSet.dependencies != null) {
                for (final NugetPackageId id : packageSet.dependencies) {
                    if (packageSet.packageId != null) {
                        graph.addParentWithChild(convertPackageId(packageSet.packageId), convertPackageId(id));
                    }
                }
            }
        }
    }
    packageDependencies.forEach(it -> {
        graph.addChildToRoot(convertPackageId(it));
    });
    return graph;
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) NugetPackageSet(com.blackducksoftware.integration.hub.detect.detector.nuget.model.NugetPackageSet) NugetPackageId(com.blackducksoftware.integration.hub.detect.detector.nuget.model.NugetPackageId)

Example 13 with MutableMapDependencyGraph

use of com.synopsys.integration.bdio.graph.MutableMapDependencyGraph in project hub-detect by blackducksoftware.

the class Rebar3TreeParser method parseRebarTreeOutput.

public RebarParseResult parseRebarTreeOutput(final List<String> dependencyTreeOutput, final String sourcePath) {
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();
    final DependencyHistory history = new DependencyHistory();
    Dependency project = null;
    for (final String line : dependencyTreeOutput) {
        if (!line.contains(HORIZONTAL_SEPARATOR_CHARACTER)) {
            continue;
        }
        final Dependency currentDependency = createDependencyFromLine(line);
        final int lineLevel = getDependencyLevelFromLine(line);
        try {
            history.clearDependenciesDeeperThan(lineLevel);
        } catch (final IllegalStateException e) {
            logger.warn(String.format("Problem parsing line '%s': %s", line, e.getMessage()));
        }
        if (history.isEmpty() && isProject(line)) {
            project = currentDependency;
        } else if (history.getLastDependency().equals(project)) {
            graph.addChildToRoot(currentDependency);
        } else if (history.isEmpty()) {
            graph.addChildToRoot(currentDependency);
        } else {
            graph.addChildWithParents(currentDependency, history.getLastDependency());
        }
        history.add(currentDependency);
    }
    if (project == null) {
        final ExternalId projectExternalId = externalIdFactory.createPathExternalId(Forge.HEX, sourcePath);
        project = new Dependency("", "", projectExternalId);
    }
    final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.HEX, project.name, project.version);
    final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.HEX, sourcePath, externalId, graph).build();
    return new RebarParseResult(project.name, project.version, codeLocation);
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) DependencyHistory(com.blackducksoftware.integration.hub.detect.util.DependencyHistory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 14 with MutableMapDependencyGraph

use of com.synopsys.integration.bdio.graph.MutableMapDependencyGraph in project hub-detect by blackducksoftware.

the class MavenCodeLocationPackager method extractCodeLocations.

// mavenTextOutput should be the full output of mvn dependency:tree (no scope applied); scope filtering is now done by this method
public List<MavenParseResult> extractCodeLocations(final String sourcePath, final String mavenOutputText, final String targetScope, final String excludedModules, final String includedModules) {
    final ExcludedIncludedFilter filter = new ExcludedIncludedFilter(excludedModules, includedModules);
    codeLocations = new ArrayList<>();
    currentMavenProject = null;
    dependencyParentStack = new Stack<>();
    parsingProjectSection = false;
    currentGraph = new MutableMapDependencyGraph();
    level = 0;
    for (final String currentLine : mavenOutputText.split(System.lineSeparator())) {
        String line = currentLine.trim();
        if (!isLineRelevant(line)) {
            continue;
        }
        line = trimLogLevel(line);
        if (StringUtils.isBlank(line)) {
            continue;
        }
        if (isProjectSection(line)) {
            parsingProjectSection = true;
            continue;
        }
        if (!parsingProjectSection) {
            continue;
        }
        if (isDependencyTreeUpdates(line)) {
            continue;
        }
        if (parsingProjectSection && currentMavenProject == null) {
            // this is the first line of a new code location, the following lines will be the tree of dependencies for this code location
            currentGraph = new MutableMapDependencyGraph();
            final MavenParseResult mavenProject = createMavenParseResult(sourcePath, line, currentGraph);
            if (null != mavenProject && filter.shouldInclude(mavenProject.projectName)) {
                logger.trace(String.format("Project: %s", mavenProject.projectName));
                this.currentMavenProject = mavenProject;
                codeLocations.add(mavenProject);
            } else {
                logger.trace("Project: unknown");
                currentMavenProject = null;
                dependencyParentStack.clear();
                parsingProjectSection = false;
                level = 0;
            }
            continue;
        }
        final boolean finished = line.contains("--------");
        if (finished) {
            currentMavenProject = null;
            dependencyParentStack.clear();
            parsingProjectSection = false;
            level = 0;
            continue;
        }
        final int previousLevel = level;
        final String cleanedLine = calculateCurrentLevelAndCleanLine(line);
        final ScopedDependency dependency = textToDependency(cleanedLine);
        if (null == dependency) {
            continue;
        }
        if (currentMavenProject != null) {
            if (level == 1) {
                // a direct dependency, clear the stack and add this as a potential parent for the next line
                if (dependency.isInScope(targetScope)) {
                    logger.trace(String.format("Level 1 component %s:%s:%s:%s is in scope; adding it to hierarchy root", dependency.externalId.group, dependency.externalId.name, dependency.externalId.version, dependency.scope));
                    currentGraph.addChildToRoot(dependency);
                    inOutOfScopeTree = false;
                } else {
                    logger.trace(String.format("Level 1 component %s:%s:%s:%s is a top-level out-of-scope component; entering non-scoped tree", dependency.externalId.group, dependency.externalId.name, dependency.externalId.version, dependency.scope));
                    inOutOfScopeTree = true;
                }
                dependencyParentStack.clear();
                dependencyParentStack.push(dependency);
            } else {
                // level should be greater than 1
                if (level == previousLevel) {
                    // a sibling of the previous dependency
                    dependencyParentStack.pop();
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                } else if (level > previousLevel) {
                    // a child of the previous dependency
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                } else {
                    // a child of a dependency further back than 1 line
                    for (int i = previousLevel; i >= level; i--) {
                        dependencyParentStack.pop();
                    }
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                }
            }
        }
    }
    addOrphansToGraph(currentGraph, orphans);
    return codeLocations;
}
Also used : ExcludedIncludedFilter(com.synopsys.integration.util.ExcludedIncludedFilter) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)

Example 15 with MutableMapDependencyGraph

use of com.synopsys.integration.bdio.graph.MutableMapDependencyGraph in project hub-detect by blackducksoftware.

the class GoDepExtractor method extract.

public Extraction extract(final File directory, final File goExe, final String goDepInspector) {
    try {
        DependencyGraph graph = depPackager.makeDependencyGraph(directory.toString(), goDepInspector);
        if (graph == null) {
            graph = new MutableMapDependencyGraph();
        }
        final ExternalId externalId = externalIdFactory.createPathExternalId(Forge.GOLANG, directory.toString());
        final DetectCodeLocation detectCodeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.GO_DEP, directory.toString(), externalId, graph).build();
        return new Extraction.Builder().success(detectCodeLocation).build();
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction)

Aggregations

MutableMapDependencyGraph (com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)21 MutableDependencyGraph (com.synopsys.integration.bdio.graph.MutableDependencyGraph)17 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)12 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)11 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)8 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)5 DependencyHistory (com.blackducksoftware.integration.hub.detect.util.DependencyHistory)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 DependencyGraphCombiner (com.synopsys.integration.bdio.graph.DependencyGraphCombiner)2 Forge (com.synopsys.integration.bdio.model.Forge)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 NameVersion (com.synopsys.integration.util.NameVersion)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2