Search in sources :

Example 16 with Executable

use of com.blackducksoftware.integration.hub.detect.util.executable.Executable 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 Executable

use of com.blackducksoftware.integration.hub.detect.util.executable.Executable 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 18 with Executable

use of com.blackducksoftware.integration.hub.detect.util.executable.Executable 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)

Example 19 with Executable

use of com.blackducksoftware.integration.hub.detect.util.executable.Executable in project hub-detect by blackducksoftware.

the class PolarisTool method runPolaris.

public void runPolaris(final IntLogger logger, File projectDirectory) throws DetectUserFriendlyException {
    logger.info("Checking if Polaris can run.");
    PolarisEnvironmentCheck polarisEnvironmentCheck = new PolarisEnvironmentCheck();
    if (!polarisEnvironmentCheck.canRun(directoryManager.getUserHome())) {
        logger.info("Polaris determined it should not run.");
        logger.debug("Checked the following user directory: " + directoryManager.getUserHome().getAbsolutePath());
        return;
    }
    logger.info("Polaris determined it should attempt to run.");
    IntHttpClient restConnection = connectionManager.createUnauthenticatedRestConnection(PolarisDownloadUtility.DEFAULT_POLARIS_SERVER_URL);
    CleanupZipExpander cleanupZipExpander = new CleanupZipExpander(logger);
    File toolsDirectory = directoryManager.getPermanentDirectory();
    PolarisDownloadUtility polarisDownloadUtility = new PolarisDownloadUtility(logger, restConnection, cleanupZipExpander, PolarisDownloadUtility.DEFAULT_POLARIS_SERVER_URL, toolsDirectory);
    Optional<String> swipCliPath = polarisDownloadUtility.retrievePolarisCliExecutablePath();
    if (swipCliPath.isPresent()) {
        Map<String, String> environmentVariables = new HashMap<>();
        environmentVariables.put("COVERITY_UNSUPPORTED", "1");
        environmentVariables.put("SWIP_USER_INPUT_TIMEOUT_MINUTES", "1");
        logger.info("Found polaris cli: " + swipCliPath.get());
        List<String> arguments = new ArrayList<>();
        arguments.add("analyze");
        arguments.add("-w");
        Executable swipExecutable = new Executable(projectDirectory, environmentVariables, swipCliPath.get(), arguments);
        try {
            ExecutableOutput output = executableRunner.execute(swipExecutable);
            if (output.getReturnCode() == 0) {
                eventSystem.publishEvent(Event.StatusSummary, new Status("POLARIS", StatusType.SUCCESS));
            } else {
                logger.error("Polaris returned a non-zero exit code.");
                eventSystem.publishEvent(Event.StatusSummary, new Status("POLARIS", StatusType.FAILURE));
            }
        } catch (ExecutableRunnerException e) {
            eventSystem.publishEvent(Event.StatusSummary, new Status("POLARIS", StatusType.FAILURE));
            logger.error("Couldn't run the executable: " + e.getMessage());
        }
    } else {
        logger.error("Check the logs - the Polaris CLI could not be found.");
    }
}
Also used : Status(com.blackducksoftware.integration.hub.detect.workflow.status.Status) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunnerException) CleanupZipExpander(com.synopsys.integration.util.CleanupZipExpander) ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) PolarisDownloadUtility(com.synopsys.integration.polaris.common.PolarisDownloadUtility) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable) File(java.io.File)

Example 20 with Executable

use of com.blackducksoftware.integration.hub.detect.util.executable.Executable in project hub-detect by blackducksoftware.

the class YarnLockExtractor method extract.

public Extraction extract(final File directory, final File yarnlock, final String yarnExe) {
    try {
        final List<String> yarnLockText = Files.readAllLines(yarnlock.toPath(), StandardCharsets.UTF_8);
        final List<String> exeArgs = Stream.of("list", "--emoji", "false").collect(Collectors.toCollection(ArrayList::new));
        if (detectConfiguration.getBooleanProperty(DetectProperty.DETECT_YARN_PROD_ONLY, PropertyAuthority.None)) {
            exeArgs.add("--prod");
        }
        final Executable yarnListExe = new Executable(directory, yarnExe, exeArgs);
        final ExecutableOutput executableOutput = executableRunner.execute(yarnListExe);
        if (executableOutput.getReturnCode() != 0) {
            final Extraction.Builder builder = new Extraction.Builder().failure(String.format("Executing command '%s' returned a non-zero exit code %s", String.join(" ", exeArgs), executableOutput.getReturnCode()));
            return builder.build();
        }
        final DependencyGraph dependencyGraph = yarnListParser.parseYarnList(yarnLockText, executableOutput.getStandardOutputAsList());
        final ExternalId externalId = externalIdFactory.createPathExternalId(Forge.NPM, directory.getCanonicalPath());
        final DetectCodeLocation detectCodeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.YARN, directory.getCanonicalPath(), externalId, dependencyGraph).build();
        return new Extraction.Builder().success(detectCodeLocation).build();
    } catch (final Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : ExecutableOutput(com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Extraction(com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Executable(com.blackducksoftware.integration.hub.detect.util.executable.Executable)

Aggregations

Executable (com.blackducksoftware.integration.hub.detect.util.executable.Executable)22 ArrayList (java.util.ArrayList)13 File (java.io.File)11 ExecutableOutput (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput)10 Extraction (com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction)8 ExecutableRunnerException (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunnerException)6 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)5 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)2 DetectProperty (com.blackducksoftware.integration.hub.detect.configuration.DetectProperty)2 PropertyAuthority (com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority)2 ExecutableRunner (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)2 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 StringUtils (org.apache.commons.lang3.StringUtils)2 DetectFileFinder (com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder)1