Search in sources :

Example 36 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method testExtractionException.

@Test
public void testExtractionException() throws DetectableException, ExecutableFailedException {
    Extraction extraction = createExceptionExtraction();
    DetectableResult extractionResult = new PassedDetectableResult();
    String projectBomTool = DetectorType.GO_MOD.name();
    DetectorToolResult result = executeToolTest(extraction, extractionResult, projectBomTool);
    assertFalse(result.getApplicableDetectorTypes().isEmpty());
    assertTrue(result.getBomToolCodeLocations().isEmpty());
    assertFalse(result.getBomToolProjectNameVersion().isPresent());
    assertTrue(result.getCodeLocationMap().isEmpty());
    assertTrue(result.getFailedDetectorTypes().isEmpty());
    assertTrue(result.getRootDetectorEvaluationTree().isPresent());
}
Also used : DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Test(org.junit.jupiter.api.Test)

Example 37 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class NpmCliExtractor method extract.

public Extraction extract(File directory, ExecutableTarget npmExe, @Nullable String npmArguments, File packageJsonFile) {
    toolVersionLogger.log(directory, npmExe);
    PackageJson packageJson;
    try {
        packageJson = parsePackageJson(packageJsonFile);
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
    List<String> exeArgs = new ArrayList<>();
    exeArgs.add("ls");
    exeArgs.add("-json");
    Optional.ofNullable(npmArguments).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> exeArgs.addAll(Arrays.asList(additionalArguments)));
    ExecutableOutput npmLsOutput;
    try {
        npmLsOutput = executableRunner.execute(ExecutableUtils.createFromTarget(directory, npmExe, exeArgs));
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
    String standardOutput = npmLsOutput.getStandardOutput();
    String errorOutput = npmLsOutput.getErrorOutput();
    if (StringUtils.isNotBlank(errorOutput)) {
        logger.error("Error when running npm ls -json command");
        logger.error(errorOutput);
        return new Extraction.Builder().failure("Npm wrote to stderr while running npm ls.").build();
    } else if (StringUtils.isNotBlank(standardOutput)) {
        logger.debug("Parsing npm ls file.");
        logger.debug(standardOutput);
        NpmPackagerResult result = npmCliParser.generateCodeLocation(standardOutput, packageJson);
        String projectName = result.getProjectName() != null ? result.getProjectName() : packageJson.name;
        String projectVersion = result.getProjectVersion() != null ? result.getProjectVersion() : packageJson.version;
        return new Extraction.Builder().success(result.getCodeLocation()).projectName(projectName).projectVersion(projectVersion).build();
    } else {
        logger.error("Nothing returned from npm ls -json command");
        return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Gson(com.google.gson.Gson) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) IOException(java.io.IOException)

Example 38 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class GoVendorExtractor method extract.

public Extraction extract(File vendorJsonFile) {
    try {
        GoVendorJsonParser vendorJsonParser = new GoVendorJsonParser(externalIdFactory);
        String vendorJsonContents = FileUtils.readFileToString(vendorJsonFile, StandardCharsets.UTF_8);
        logger.debug(vendorJsonContents);
        DependencyGraph dependencyGraph = vendorJsonParser.parseVendorJson(gson, vendorJsonContents);
        CodeLocation codeLocation = new CodeLocation(dependencyGraph);
        return new Extraction.Builder().success(codeLocation).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GoVendorJsonParser(com.synopsys.integration.detectable.detectables.go.vendor.parse.GoVendorJsonParser) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Extraction(com.synopsys.integration.detectable.extraction.Extraction)

Example 39 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class MavenCliExtractor method extract.

// TODO: Limit 'extractors' to 'execute' and 'read', delegate all other work.
public Extraction extract(File directory, ExecutableTarget mavenExe, MavenCliExtractorOptions mavenCliExtractorOptions) throws ExecutableFailedException {
    toolVersionLogger.log(directory, mavenExe);
    List<String> commandArguments = commandParser.parseCommandString(mavenCliExtractorOptions.getMavenBuildCommand().orElse("")).stream().filter(arg -> !arg.equals("dependency:tree")).collect(Collectors.toList());
    commandArguments.add("dependency:tree");
    // Force maven to use a single thread to ensure the tree output is in the correct order.
    commandArguments.add("-T1");
    ExecutableOutput mvnExecutableResult = executableRunner.executeSuccessfully(ExecutableUtils.createFromTarget(directory, mavenExe, commandArguments));
    List<String> mavenOutput = mvnExecutableResult.getStandardOutputAsList();
    List<String> excludedScopes = mavenCliExtractorOptions.getMavenExcludedScopes();
    List<String> includedScopes = mavenCliExtractorOptions.getMavenIncludedScopes();
    List<String> excludedModules = mavenCliExtractorOptions.getMavenExcludedModules();
    List<String> includedModules = mavenCliExtractorOptions.getMavenIncludedModules();
    List<MavenParseResult> mavenResults = mavenCodeLocationPackager.extractCodeLocations(directory.toString(), mavenOutput, excludedScopes, includedScopes, excludedModules, includedModules);
    List<CodeLocation> codeLocations = Bds.of(mavenResults).map(MavenParseResult::getCodeLocation).toList();
    Optional<MavenParseResult> firstWithName = Bds.of(mavenResults).firstFiltered(it -> StringUtils.isNotBlank(it.getProjectName()));
    Extraction.Builder builder = new Extraction.Builder().success(codeLocations);
    if (firstWithName.isPresent()) {
        builder.projectName(firstWithName.get().getProjectName());
        builder.projectVersion(firstWithName.get().getProjectVersion());
    }
    return builder.build();
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) CommandParser(com.synopsys.integration.common.util.parse.CommandParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) List(java.util.List) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Bds(com.synopsys.integration.common.util.Bds) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Extraction(com.synopsys.integration.detectable.extraction.Extraction)

Example 40 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class GoVndrExtractor method extract.

public Extraction extract(File vndrConfig) {
    try {
        VndrParser vndrParser = new VndrParser(externalIdFactory);
        List<String> vendorConfContents = Files.readAllLines(vndrConfig.toPath(), StandardCharsets.UTF_8);
        logger.debug(String.join("\n", vendorConfContents));
        DependencyGraph dependencyGraph = vndrParser.parseVendorConf(vendorConfContents);
        CodeLocation codeLocation = new CodeLocation(dependencyGraph);
        return new Extraction.Builder().success(codeLocation).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) VndrParser(com.synopsys.integration.detectable.detectables.go.vendr.parse.VndrParser) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Extraction(com.synopsys.integration.detectable.extraction.Extraction)

Aggregations

Extraction (com.synopsys.integration.detectable.extraction.Extraction)63 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)38 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)23 IOException (java.io.IOException)19 Test (org.junit.jupiter.api.Test)18 File (java.io.File)17 ArrayList (java.util.ArrayList)14 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)13 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)12 NameVersion (com.synopsys.integration.util.NameVersion)12 Executable (com.synopsys.integration.executable.Executable)11 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)9 List (java.util.List)9 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)8 HashMap (java.util.HashMap)8 Optional (java.util.Optional)8 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)7 Collectors (java.util.stream.Collectors)7 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)6 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6