Search in sources :

Example 11 with ExecutableTarget

use of com.synopsys.integration.detectable.ExecutableTarget 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 12 with ExecutableTarget

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

the class NugetInspectorExtractor method extract.

public Extraction extract(List<File> targets, File outputDirectory, ExecutableTarget inspector, NugetInspectorOptions nugetInspectorOptions) {
    try {
        List<NugetTargetResult> results = new ArrayList<>();
        for (int i = 0; i < targets.size(); i++) {
            File targetDirectory = new File(outputDirectory, "inspection-" + i);
            results.add(executeTarget(inspector, targets.get(i), targetDirectory, nugetInspectorOptions));
        }
        List<CodeLocation> codeLocations = results.stream().flatMap(it -> it.codeLocations.stream()).collect(Collectors.toList());
        Map<File, CodeLocation> codeLocationsBySource = new HashMap<>();
        codeLocations.forEach(codeLocation -> {
            File sourcePathFile = codeLocation.getSourcePath().orElse(null);
            if (codeLocationsBySource.containsKey(sourcePathFile)) {
                logger.debug("Combined code location for: " + sourcePathFile);
                CodeLocation destination = codeLocationsBySource.get(sourcePathFile);
                // TODO: I don't like this casting, perhaps this doesn't have to happen here in 8.0.0. JM-04/2022
                destination.getDependencyGraph().copyGraphToRoot((BasicDependencyGraph) codeLocation.getDependencyGraph());
            } else {
                codeLocationsBySource.put(sourcePathFile, codeLocation);
            }
        });
        Optional<NameVersion> nameVersion = results.stream().filter(it -> it.nameVersion != null).map(it -> it.nameVersion).findFirst();
        List<CodeLocation> uniqueCodeLocations = new ArrayList<>(codeLocationsBySource.values());
        return new Extraction.Builder().success(uniqueCodeLocations).nameVersionIfPresent(nameVersion).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) Logger(org.slf4j.Logger) NugetInspectorParser(com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executable(com.synopsys.integration.executable.Executable) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) IOException(java.io.IOException) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Extraction(com.synopsys.integration.detectable.extraction.Extraction) File(java.io.File)

Example 13 with ExecutableTarget

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

the class BitbakeExtractor method extract.

public Extraction extract(File sourceDirectory, ExecutableTarget bashExecutable, File buildEnvironmentFile) throws ExecutableFailedException, IOException {
    toolVersionLogger.log(() -> bitbakeCommandRunner.runBitbakeVersion(sourceDirectory, bashExecutable, buildEnvironmentFile));
    File buildDirectory = determineBuildDirectory(sourceDirectory, bashExecutable, buildEnvironmentFile);
    BitbakeEnvironment bitbakeEnvironment = executeBitbakeForEnvironment(sourceDirectory, bashExecutable, buildEnvironmentFile);
    ShowRecipesResults showRecipesResults = executeBitbakeForRecipeLayerCatalog(sourceDirectory, bashExecutable, buildEnvironmentFile);
    List<CodeLocation> codeLocations = packageNames.stream().map(targetPackage -> generateCodeLocationForTargetPackage(targetPackage, sourceDirectory, bashExecutable, buildEnvironmentFile, buildDirectory, showRecipesResults, bitbakeEnvironment)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    if (codeLocations.isEmpty()) {
        return Extraction.failure("No Code Locations were generated during extraction");
    } else {
        return Extraction.success(codeLocations);
    }
}
Also used : BitbakeEnvironment(com.synopsys.integration.detectable.detectables.bitbake.data.BitbakeEnvironment) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) IntegrationException(com.synopsys.integration.exception.IntegrationException) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BitbakeDependencyGraphTransformer(com.synopsys.integration.detectable.detectables.bitbake.transform.BitbakeDependencyGraphTransformer) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) PwdOutputParser(com.synopsys.integration.detectable.detectables.bitbake.parse.PwdOutputParser) Charset(java.nio.charset.Charset) Map(java.util.Map) ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) BitbakeGraph(com.synopsys.integration.detectable.detectables.bitbake.model.BitbakeGraph) Logger(org.slf4j.Logger) EnumListFilter(com.synopsys.integration.detectable.detectable.util.EnumListFilter) BitbakeGraphTransformer(com.synopsys.integration.detectable.detectables.bitbake.transform.BitbakeGraphTransformer) GraphParser(com.paypal.digraph.parser.GraphParser) BuildFileFinder(com.synopsys.integration.detectable.detectables.bitbake.collect.BuildFileFinder) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BitbakeEnvironmentParser(com.synopsys.integration.detectable.detectables.bitbake.parse.BitbakeEnvironmentParser) BitbakeEnvironment(com.synopsys.integration.detectable.detectables.bitbake.data.BitbakeEnvironment) BitbakeCommandRunner(com.synopsys.integration.detectable.detectables.bitbake.collect.BitbakeCommandRunner) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ShowRecipesResults(com.synopsys.integration.detectable.detectables.bitbake.data.ShowRecipesResults) Optional(java.util.Optional) BitbakeRecipesParser(com.synopsys.integration.detectable.detectables.bitbake.parse.BitbakeRecipesParser) LicenseManifestParser(com.synopsys.integration.detectable.detectables.bitbake.parse.LicenseManifestParser) InputStream(java.io.InputStream) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Optional(java.util.Optional) ShowRecipesResults(com.synopsys.integration.detectable.detectables.bitbake.data.ShowRecipesResults) File(java.io.File)

Example 14 with ExecutableTarget

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

the class LernaPackageDiscovererTest method discoverLernaPackages.

@Test
void discoverLernaPackages() throws ExecutableRunnerException {
    File workingDirectory = new File("workingDir");
    ExecutableTarget lernaExecutable = ExecutableTarget.forCommand("lerna");
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    Mockito.when(executableRunner.execute(Mockito.any(Executable.class))).thenReturn(new ExecutableOutput(0, String.join(System.lineSeparator(), "[", "  {", "    \"name\": \"@lerna/packageA\",", "    \"version\": \"1.2.3\",", "    \"private\": false,", "    \"location\": \"/source/packages/packageA\"", "  },", "  {", "    \"name\": \"@lerna/packageB\",", "    \"version\": \"3.2.1\",", "    \"private\": true,", "    \"location\": \"/source/packages/packageB\"", "  },", "  {", "    \"name\": \"@lerna/packageC\",", "    \"version\": \"1.1.1\",", "    \"private\": true,", "    \"location\": \"/source/packages/packageC\"", "  }", "]"), ""));
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    LernaPackageDiscoverer lernaPackageDiscoverer = new LernaPackageDiscoverer(executableRunner, gson, Collections.singletonList("@lerna/packageC"), new LinkedList<>());
    List<LernaPackage> lernaPackages = lernaPackageDiscoverer.discoverLernaPackages(workingDirectory, lernaExecutable);
    Assertions.assertEquals(2, lernaPackages.size(), "Expected to find two Lerna packages.");
    LernaPackage lernaPackageA = lernaPackages.get(0);
    Assertions.assertEquals("@lerna/packageA", lernaPackageA.getName());
    Assertions.assertEquals("1.2.3", lernaPackageA.getVersion());
    Assertions.assertFalse(lernaPackageA.isPrivate());
    Assertions.assertEquals("/source/packages/packageA", lernaPackageA.getLocation());
    LernaPackage lernaPackageB = lernaPackages.get(1);
    Assertions.assertEquals("@lerna/packageB", lernaPackageB.getName());
    Assertions.assertEquals("3.2.1", lernaPackageB.getVersion());
    Assertions.assertTrue(lernaPackageB.isPrivate());
    Assertions.assertEquals("/source/packages/packageB", lernaPackageB.getLocation());
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) GsonBuilder(com.google.gson.GsonBuilder) LernaPackageDiscoverer(com.synopsys.integration.detectable.detectables.lerna.LernaPackageDiscoverer) Gson(com.google.gson.Gson) LernaPackage(com.synopsys.integration.detectable.detectables.lerna.model.LernaPackage) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Test(org.junit.jupiter.api.Test)

Example 15 with ExecutableTarget

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

the class IntermediateStepExecuteBazelOnEachLineTest method testNoInput.

@Test
public void testNoInput() throws ExecutableRunnerException, IntegrationException, ExecutableFailedException {
    File workspaceDir = new File(".");
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    ExecutableTarget bazelExe = ExecutableTarget.forCommand("/usr/bin/bazel");
    ExecutableOutput bazelCmdExecutableOutput = Mockito.mock(ExecutableOutput.class);
    Mockito.when(bazelCmdExecutableOutput.getReturnCode()).thenReturn(0);
    Mockito.when(bazelCmdExecutableOutput.getStandardOutput()).thenReturn("@org_apache_commons_commons_io//jar:jar\n@com_google_guava_guava//jar:jar");
    Mockito.when(executableRunner.executeSuccessfully(Mockito.any(Executable.class))).thenReturn(bazelCmdExecutableOutput);
    BazelCommandExecutor bazelCommandExecutor = new BazelCommandExecutor(executableRunner, workspaceDir, bazelExe);
    BazelVariableSubstitutor bazelVariableSubstitutor = new BazelVariableSubstitutor("//:ProjectRunner", null);
    IntermediateStep executor = new IntermediateStepExecuteBazelOnEachLine(bazelCommandExecutor, bazelVariableSubstitutor, Arrays.asList("cquery", "filter(\\\"@.*:jar\\\", deps(${detect.bazel.target}))"), false);
    List<String> input = new ArrayList<>(0);
    List<String> output = executor.process(input);
    assertEquals(1, output.size());
    assertEquals("@org_apache_commons_commons_io//jar:jar\n@com_google_guava_guava//jar:jar", output.get(0));
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IntermediateStep(com.synopsys.integration.detectable.detectables.bazel.pipeline.step.IntermediateStep) ArrayList(java.util.ArrayList) BazelVariableSubstitutor(com.synopsys.integration.detectable.detectables.bazel.pipeline.step.BazelVariableSubstitutor) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) BazelCommandExecutor(com.synopsys.integration.detectable.detectables.bazel.pipeline.step.BazelCommandExecutor) IntermediateStepExecuteBazelOnEachLine(com.synopsys.integration.detectable.detectables.bazel.pipeline.step.IntermediateStepExecuteBazelOnEachLine) Test(org.junit.jupiter.api.Test)

Aggregations

ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)15 File (java.io.File)15 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)10 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)10 Extraction (com.synopsys.integration.detectable.extraction.Extraction)9 List (java.util.List)8 ExecutableUtils (com.synopsys.integration.detectable.ExecutableUtils)6 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)6 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)6 Executable (com.synopsys.integration.executable.Executable)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 StandardCharsets (java.nio.charset.StandardCharsets)5 ArrayList (java.util.ArrayList)5 FileUtils (org.apache.commons.io.FileUtils)5 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)4 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)4 HashMap (java.util.HashMap)4 Test (org.junit.jupiter.api.Test)4