Search in sources :

Example 1 with NameVersionCodeLocation

use of com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation in project synopsys-detect by blackducksoftware.

the class PipInspectorTreeParserTest method validTest.

@Test
public void validTest() {
    List<String> pipInspectorOutput = Arrays.asList("projectName==projectVersionName", "   with-dashes==1.0.0", "   Uppercase==2.0.0", "      child==3.0.0", "   test==4.0.0");
    Optional<NameVersionCodeLocation> validParse = parser.parse(pipInspectorOutput, "");
    Assertions.assertTrue(validParse.isPresent());
    Assertions.assertEquals("projectName", validParse.get().getProjectName());
    Assertions.assertEquals("projectVersionName", validParse.get().getProjectVersion());
    NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.PYPI, validParse.get().getCodeLocation().getDependencyGraph());
    graphAssert.hasRootDependency("with-dashes", "1.0.0");
    graphAssert.hasRootDependency("Uppercase", "2.0.0");
    graphAssert.hasRootDependency("test", "4.0.0");
    graphAssert.hasParentChildRelationship("Uppercase", "2.0.0", "child", "3.0.0");
    graphAssert.hasRootSize(3);
}
Also used : NameVersionGraphAssert(com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert) NameVersionCodeLocation(com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Example 2 with NameVersionCodeLocation

use of com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation in project synopsys-detect by blackducksoftware.

the class PipInspectorExtractor method extract.

public Extraction extract(File directory, ExecutableTarget pythonExe, ExecutableTarget pipExe, File pipInspector, File setupFile, List<Path> requirementFilePaths, String providedProjectName) {
    toolVersionLogger.log(directory, pythonExe);
    toolVersionLogger.log(directory, pipExe);
    Extraction extractionResult;
    try {
        String projectName = getProjectName(directory, pythonExe, setupFile, providedProjectName);
        List<CodeLocation> codeLocations = new ArrayList<>();
        String projectVersion = null;
        List<Path> requirementsPaths = new ArrayList<>();
        if (requirementFilePaths.isEmpty()) {
            requirementsPaths.add(null);
        } else {
            requirementsPaths.addAll(requirementFilePaths);
        }
        for (Path requirementFilePath : requirementsPaths) {
            List<String> inspectorOutput = runInspector(directory, pythonExe, pipInspector, projectName, requirementFilePath);
            Optional<NameVersionCodeLocation> result = pipInspectorTreeParser.parse(inspectorOutput, directory.toString());
            if (result.isPresent()) {
                codeLocations.add(result.get().getCodeLocation());
                String potentialProjectVersion = result.get().getProjectVersion();
                if (projectVersion == null && StringUtils.isNotBlank(potentialProjectVersion)) {
                    projectVersion = potentialProjectVersion;
                }
            }
        }
        if (codeLocations.isEmpty()) {
            extractionResult = new Extraction.Builder().failure("The Pip Inspector tree parse failed to produce output.").build();
        } else {
            extractionResult = new Extraction.Builder().success(codeLocations).projectName(projectName).projectVersion(projectVersion).build();
        }
    } catch (Exception e) {
        extractionResult = new Extraction.Builder().exception(e).build();
    }
    return extractionResult;
}
Also used : Path(java.nio.file.Path) NameVersionCodeLocation(com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Extraction(com.synopsys.integration.detectable.extraction.Extraction) NameVersionCodeLocation(com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation)

Example 3 with NameVersionCodeLocation

use of com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation in project synopsys-detect by blackducksoftware.

the class PipInspectorTreeParser method parse.

public Optional<NameVersionCodeLocation> parse(List<String> pipInspectorOutputAsList, String sourcePath) {
    NameVersionCodeLocation parseResult = null;
    DependencyGraph graph = new BasicDependencyGraph();
    DependencyHistory history = new DependencyHistory();
    Dependency project = null;
    int unResolvedPackageCount = 0;
    for (String line : pipInspectorOutputAsList) {
        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)) {
            boolean wasUnresolved = parseErrorsFromLine(trimmedLine);
            if (wasUnresolved) {
                unResolvedPackageCount++;
            }
            continue;
        }
        Dependency currentDependency = parseDependencyFromLine(trimmedLine, sourcePath);
        adjustForIndentLevel(history, line);
        project = addDependencyToGraph(graph, history, project, currentDependency);
        history.add(currentDependency);
    }
    adviseIfUnresolvedPackages(unResolvedPackageCount);
    if (project != null) {
        CodeLocation codeLocation = new CodeLocation(graph, project.getExternalId());
        parseResult = new NameVersionCodeLocation(project.getName(), project.getVersion(), codeLocation);
    }
    return Optional.ofNullable(parseResult);
}
Also used : NameVersionCodeLocation(com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) DependencyHistory(com.synopsys.integration.detectable.detectable.util.DependencyHistory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) NameVersionCodeLocation(com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation)

Aggregations

NameVersionCodeLocation (com.synopsys.integration.detectable.detectables.pip.inspector.model.NameVersionCodeLocation)3 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 BasicDependencyGraph (com.synopsys.integration.bdio.graph.BasicDependencyGraph)1 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)1 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)1 DependencyHistory (com.synopsys.integration.detectable.detectable.util.DependencyHistory)1 Extraction (com.synopsys.integration.detectable.extraction.Extraction)1 NameVersionGraphAssert (com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert)1 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1