Search in sources :

Example 16 with Extraction

use of com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction in project hub-detect by blackducksoftware.

the class GradleInspectorExtractor method extract.

public Extraction extract(final File directory, final String gradleExe, final String gradleInspector, final File outputDirectory) {
    try {
        String gradleCommand = detectConfiguration.getProperty(DetectProperty.DETECT_GRADLE_BUILD_COMMAND, PropertyAuthority.None);
        final List<String> arguments = new ArrayList<>();
        if (StringUtils.isNotBlank(gradleCommand)) {
            gradleCommand = gradleCommand.replaceAll("dependencies", "").trim();
            Arrays.stream(gradleCommand.split(" ")).filter(StringUtils::isNotBlank).forEach(arguments::add);
        }
        arguments.add("dependencies");
        arguments.add(String.format("--init-script=%s", gradleInspector));
        arguments.add(String.format("-DGRADLEEXTRACTIONDIR=%s", outputDirectory.getCanonicalPath()));
        arguments.add("--info");
        final Executable executable = new Executable(directory, gradleExe, arguments);
        final ExecutableOutput output = executableRunner.execute(executable);
        if (output.getReturnCode() == 0) {
            final File rootProjectMetadataFile = detectFileFinder.findFile(outputDirectory, "rootProjectMetadata.txt");
            final List<File> codeLocationFiles = detectFileFinder.findFiles(outputDirectory, "*_dependencyGraph.txt");
            final List<DetectCodeLocation> codeLocations = new ArrayList<>();
            String projectName = null;
            String projectVersion = null;
            if (codeLocationFiles != null) {
                codeLocationFiles.stream().map(codeLocationFile -> gradleReportParser.parseDependencies(codeLocationFile)).filter(Optional::isPresent).map(Optional::get).forEach(codeLocations::add);
                if (rootProjectMetadataFile != null) {
                    final Optional<NameVersion> projectNameVersion = gradleReportParser.parseRootProjectNameVersion(rootProjectMetadataFile);
                    if (projectNameVersion.isPresent()) {
                        projectName = projectNameVersion.get().getName();
                        projectVersion = projectNameVersion.get().getVersion();
                    }
                } else {
                    logger.warn("Gradle inspector did not create a meta data report so no project version information was found.");
                }
            }
            return new Extraction.Builder().success(codeLocations).projectName(projectName).projectVersion(projectVersion).build();
        } else {
            return new Extraction.Builder().failure("The gradle inspector returned a non-zero exit code: " + output.getReturnCode()).build();
        }
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExecutableRunner(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) List(java.util.List) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) PropertyAuthority(com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority) Optional(java.util.Optional) DetectFileFinder(com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) DetectProperty(com.blackducksoftware.integration.hub.detect.configuration.DetectProperty) Optional(java.util.Optional) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable) File(java.io.File)

Example 17 with Extraction

use of com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction in project hub-detect by blackducksoftware.

the class NugetInspectorExtractor method extract.

public Extraction extract(final File targetDirectory, File outputDirectory, NugetInspector inspector, final ExtractionId extractionId) {
    try {
        final List<String> options = new ArrayList<>(Arrays.asList("--target_path=" + targetDirectory.toString(), "--output_directory=" + outputDirectory.getCanonicalPath(), "--ignore_failure=" + detectConfiguration.getBooleanProperty(DetectProperty.DETECT_NUGET_IGNORE_FAILURE, PropertyAuthority.None)));
        final String nugetExcludedModules = detectConfiguration.getProperty(DetectProperty.DETECT_NUGET_EXCLUDED_MODULES, PropertyAuthority.None);
        if (StringUtils.isNotBlank(nugetExcludedModules)) {
            options.add("--excluded_modules=" + nugetExcludedModules);
        }
        final String nugetIncludedModules = detectConfiguration.getProperty(DetectProperty.DETECT_NUGET_INCLUDED_MODULES, PropertyAuthority.None);
        if (StringUtils.isNotBlank(nugetIncludedModules)) {
            options.add("--included_modules=" + nugetIncludedModules);
        }
        final String[] nugetPackagesRepo = detectConfiguration.getStringArrayProperty(DetectProperty.DETECT_NUGET_PACKAGES_REPO_URL, PropertyAuthority.None);
        if (nugetPackagesRepo.length > 0) {
            final String packagesRepos = Arrays.asList(nugetPackagesRepo).stream().collect(Collectors.joining(","));
            options.add("--packages_repo_url=" + packagesRepos);
        }
        final String nugetConfigPath = detectConfiguration.getProperty(DetectProperty.DETECT_NUGET_CONFIG_PATH, PropertyAuthority.None);
        if (StringUtils.isNotBlank(nugetConfigPath)) {
            options.add("--nuget_config_path=" + nugetConfigPath);
        }
        if (logger.isTraceEnabled()) {
            options.add("-v");
        }
        final ExecutableOutput executableOutput = inspector.execute(targetDirectory, options);
        if (executableOutput.getReturnCode() != 0) {
            return new Extraction.Builder().failure(String.format("Executing command '%s' returned a non-zero exit code %s", String.join(" ", options), executableOutput.getReturnCode())).build();
        }
        final List<File> dependencyNodeFiles = detectFileFinder.findFiles(outputDirectory, INSPECTOR_OUTPUT_PATTERN);
        final List<NugetParseResult> parseResults = new ArrayList<>();
        for (final File dependencyNodeFile : dependencyNodeFiles) {
            final NugetParseResult result = nugetInspectorPackager.createDetectCodeLocation(dependencyNodeFile);
            parseResults.add(result);
        }
        final List<DetectCodeLocation> codeLocations = parseResults.stream().flatMap(it -> it.codeLocations.stream()).collect(Collectors.toList());
        if (codeLocations.size() <= 0) {
            logger.warn("Unable to extract any dependencies from nuget");
        }
        final Map<String, DetectCodeLocation> codeLocationsBySource = new HashMap<>();
        final DependencyGraphCombiner combiner = new DependencyGraphCombiner();
        codeLocations.stream().forEach(codeLocation -> {
            final String sourcePathKey = codeLocation.getSourcePath().toLowerCase();
            if (codeLocationsBySource.containsKey(sourcePathKey)) {
                logger.info("Multiple project code locations were generated for: " + targetDirectory.toString());
                logger.info("This most likely means the same project exists in multiple solutions.");
                logger.info("The code location's dependencies will be combined, in the future they will exist seperately for each solution.");
                final DetectCodeLocation destination = codeLocationsBySource.get(sourcePathKey);
                combiner.addGraphAsChildrenToRoot((MutableDependencyGraph) destination.getDependencyGraph(), codeLocation.getDependencyGraph());
            } else {
                codeLocationsBySource.put(sourcePathKey, codeLocation);
            }
        });
        final List<DetectCodeLocation> uniqueCodeLocations = codeLocationsBySource.values().stream().collect(Collectors.toList());
        final Extraction.Builder builder = new Extraction.Builder().success(uniqueCodeLocations);
        final Optional<NugetParseResult> project = parseResults.stream().filter(it -> StringUtils.isNotBlank(it.projectName)).findFirst();
        if (project.isPresent()) {
            builder.projectName(project.get().projectName);
            builder.projectVersion(project.get().projectVersion);
        }
        return builder.build();
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) ArrayList(java.util.ArrayList) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner) List(java.util.List) ExtractionId(com.blackducksoftware.integration.hub.detect.detector.ExtractionId) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) PropertyAuthority(com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority) Map(java.util.Map) NugetInspector(com.blackducksoftware.integration.hub.detect.detector.nuget.inspector.NugetInspector) Optional(java.util.Optional) DetectFileFinder(com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) DetectProperty(com.blackducksoftware.integration.hub.detect.configuration.DetectProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) File(java.io.File) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner)

Example 18 with Extraction

use of com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction in project hub-detect by blackducksoftware.

the class ClangExtractor method extract.

public Extraction extract(final ClangLinuxPackageManager pkgMgr, final File givenDir, final int depth, final ExtractionId extractionId, final File jsonCompilationDatabaseFile) {
    try {
        logger.info(String.format("Analyzing %s", jsonCompilationDatabaseFile.getAbsolutePath()));
        final File rootDir = fileFinder.findContainingDir(givenDir, depth);
        final File outputDirectory = directoryManager.getExtractionOutputDirectory(extractionId);
        logger.debug(String.format("extract() called; compileCommandsJsonFilePath: %s", jsonCompilationDatabaseFile.getAbsolutePath()));
        final Set<File> unManagedDependencyFiles = ConcurrentHashMap.newKeySet(64);
        final List<CompileCommand> compileCommands = CompileCommandsJsonFile.parseJsonCompilationDatabaseFile(gson, jsonCompilationDatabaseFile);
        final List<Dependency> bdioComponents = compileCommands.parallelStream().flatMap(compileCommandToDependencyFilePathsConverter(outputDirectory)).collect(Collectors.toSet()).parallelStream().filter(StringUtils::isNotBlank).map(File::new).filter(fileIsNewPredicate()).flatMap(dependencyFileToLinuxPackagesConverter(rootDir, unManagedDependencyFiles, pkgMgr)).collect(Collectors.toSet()).parallelStream().flatMap(linuxPackageToBdioComponentsConverter(pkgMgr)).collect(Collectors.toList());
        final DetectCodeLocation detectCodeLocation = codeLocationAssembler.generateCodeLocation(pkgMgr.getDefaultForge(), rootDir, bdioComponents);
        logSummary(bdioComponents, unManagedDependencyFiles);
        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) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) File(java.io.File)

Example 19 with Extraction

use of com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction in project hub-detect by blackducksoftware.

the class RebarExtractor method extract.

public Extraction extract(final File directory, final File rebarExe) {
    try {
        final List<DetectCodeLocation> codeLocations = new ArrayList<>();
        final Map<String, String> envVars = new HashMap<>();
        envVars.put("REBAR_COLOR", "none");
        final List<String> arguments = new ArrayList<>();
        arguments.add("tree");
        final Executable rebar3TreeExe = new Executable(directory, envVars, rebarExe.toString(), arguments);
        final List<String> output = executableRunner.execute(rebar3TreeExe).getStandardOutputAsList();
        final RebarParseResult parseResult = rebarTreeParser.parseRebarTreeOutput(output, directory.toString());
        codeLocations.add(parseResult.getCodeLocation());
        return new Extraction.Builder().success(codeLocations).projectName(parseResult.getProjectName()).projectVersion(parseResult.getProjectVersion()).build();
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable)

Example 20 with Extraction

use of com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction in project hub-detect by blackducksoftware.

the class MavenCliExtractor method extract.

public Extraction extract(final File directory, final String mavenExe) {
    try {
        String mavenCommand = detectConfiguration.getProperty(DetectProperty.DETECT_MAVEN_BUILD_COMMAND, PropertyAuthority.None);
        if (StringUtils.isNotBlank(mavenCommand)) {
            mavenCommand = mavenCommand.replace("dependency:tree", "");
            if (StringUtils.isNotBlank(mavenCommand)) {
                mavenCommand = mavenCommand.trim();
            }
        }
        final List<String> arguments = new ArrayList<>();
        if (StringUtils.isNotBlank(mavenCommand)) {
            arguments.addAll(Arrays.asList(mavenCommand.split(" ")));
        }
        arguments.add("dependency:tree");
        final Executable mvnExecutable = new Executable(directory, mavenExe, arguments);
        final ExecutableOutput mvnOutput = executableRunner.execute(mvnExecutable);
        if (mvnOutput.getReturnCode() == 0) {
            final String mavenScope = detectConfiguration.getProperty(DetectProperty.DETECT_MAVEN_SCOPE, PropertyAuthority.None);
            final String excludedModules = detectConfiguration.getProperty(DetectProperty.DETECT_MAVEN_EXCLUDED_MODULES, PropertyAuthority.None);
            final String includedModules = detectConfiguration.getProperty(DetectProperty.DETECT_MAVEN_INCLUDED_MODULES, PropertyAuthority.None);
            final List<MavenParseResult> mavenResults = mavenCodeLocationPackager.extractCodeLocations(directory.toString(), mvnOutput.getStandardOutput(), mavenScope, excludedModules, includedModules);
            final List<DetectCodeLocation> codeLocations = mavenResults.stream().map(it -> it.codeLocation).collect(Collectors.toList());
            final Optional<MavenParseResult> firstWithName = mavenResults.stream().filter(it -> StringUtils.isNoneBlank(it.projectName)).findFirst();
            final Extraction.Builder builder = new Extraction.Builder().success(codeLocations);
            if (firstWithName.isPresent()) {
                builder.projectName(firstWithName.get().projectName);
                builder.projectVersion(firstWithName.get().projectVersion);
            }
            return builder.build();
        } else {
            final Extraction.Builder builder = new Extraction.Builder().failure(String.format("Executing command '%s' returned a non-zero exit code %s", String.join(" ", arguments), mvnOutput.getReturnCode()));
            return builder.build();
        }
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Arrays(java.util.Arrays) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) ExecutableRunner(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) PropertyAuthority(com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority) Optional(java.util.Optional) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) DetectProperty(com.blackducksoftware.integration.hub.detect.configuration.DetectProperty) ArrayList(java.util.ArrayList) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable)

Aggregations

Extraction (com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction)29 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)17 File (java.io.File)17 ExecutableOutput (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput)14 ArrayList (java.util.ArrayList)12 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)11 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)11 Executable (com.blackducksoftware.integration.hub.detect.util.executable.Executable)8 DetectFileFinder (com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder)7 ExecutableRunner (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner)6 ExtractionId (com.blackducksoftware.integration.hub.detect.detector.ExtractionId)5 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)5 IOException (java.io.IOException)4 DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)3 DetectProperty (com.blackducksoftware.integration.hub.detect.configuration.DetectProperty)3 PropertyAuthority (com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority)3 ExecutableRunnerException (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunnerException)3 DirectoryManager (com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3