use of com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput in project hub-detect by blackducksoftware.
the class ClangExtractorTest method testMultipleCommandsDependenciesPackages.
@Test
public void testMultipleCommandsDependenciesPackages() throws ExecutableRunnerException {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
final CompileCommand compileCommandWrapperHelloWorld = createCompileCommand("src/test/resources/clang/source/hello_world.cpp", "gcc hello_world.cpp", null);
final CompileCommand compileCommandWrapperGoodbyeWorld = createCompileCommand("src/test/resources/clang/source/goodbye_world.cpp", "gcc goodbye_world.cpp", null);
final Set<String> dependencyFilePathsHelloWorld = createDependencyFilePaths(new File("src/test/resources/clang/source/myinclude.h"), new File("/usr/include/nonexistentfile1.h"), new File("/usr/include/nonexistentfile2.h"));
final Set<String> dependencyFilePathsGoodbyeWorld = createDependencyFilePaths(new File("/usr/include/nonexistentfile4.h"), new File("/usr/include/nonexistentfile3.h"));
final ExecutableRunner executableRunner = Mockito.mock(ExecutableRunner.class);
final DirectoryManager directoryManager = Mockito.mock(DirectoryManager.class);
final DependenciesListFileManager dependenciesListFileManager = Mockito.mock(DependenciesListFileManager.class);
Mockito.when(dependenciesListFileManager.generateDependencyFilePaths(outputDir, compileCommandWrapperHelloWorld, true)).thenReturn(dependencyFilePathsHelloWorld);
Mockito.when(dependenciesListFileManager.generateDependencyFilePaths(outputDir, compileCommandWrapperGoodbyeWorld, true)).thenReturn(dependencyFilePathsGoodbyeWorld);
Mockito.when(executableRunner.executeFromDirQuietly(Mockito.any(File.class), Mockito.anyString(), Mockito.anyList())).thenReturn(new ExecutableOutput(0, "", ""));
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final CodeLocationAssembler codeLocationAssembler = new CodeLocationAssembler(externalIdFactory);
final ClangExtractor extractor = new ClangExtractor(null, executableRunner, gson, new DetectFileFinder(), directoryManager, dependenciesListFileManager, codeLocationAssembler);
final ClangLinuxPackageManager pkgMgr = Mockito.mock(ClangLinuxPackageManager.class);
final File givenDir = new File("src/test/resources/clang/source/build");
final int depth = 1;
final ExtractionId extractionId = new ExtractionId(DetectorType.CLANG, EXTRACTION_ID);
final File jsonCompilationDatabaseFile = new File("src/test/resources/clang/source/build/compile_commands.json");
Mockito.when(directoryManager.getExtractionOutputDirectory(Mockito.any(ExtractionId.class))).thenReturn(outputDir);
final List<PackageDetails> packages = new ArrayList<>();
packages.add(new PackageDetails("testPackageName1", "testPackageVersion1", "testPackageArch1"));
packages.add(new PackageDetails("testPackageName2", "testPackageVersion2", "testPackageArch2"));
Mockito.when(pkgMgr.getDefaultForge()).thenReturn(Forge.CENTOS);
Mockito.when(pkgMgr.getPackages(Mockito.any(File.class), Mockito.any(ExecutableRunner.class), Mockito.any(Set.class), Mockito.any(DependencyFileDetails.class))).thenReturn(packages);
Mockito.when(pkgMgr.getForges()).thenReturn(Arrays.asList(Forge.CENTOS, Forge.FEDORA, Forge.REDHAT));
final Extraction extraction = extractor.extract(pkgMgr, givenDir, depth, extractionId, jsonCompilationDatabaseFile);
checkGeneratedDependenciesComplex(extraction);
}
use of com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput in project hub-detect by blackducksoftware.
the class ClangExtractorTest method testJsonWithArgumentsNotCommand.
@Test
public void testJsonWithArgumentsNotCommand() throws ExecutableRunnerException {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
final String[] argsHello = { "gcc", "hello_world.cpp" };
final CompileCommand compileCommandWrapperHelloWorld = createCompileCommand("src/test/resources/clang/source/hello_world.cpp", null, argsHello);
final String[] argsGoodbye = { "gcc", "goodbye_world.cpp" };
final CompileCommand compileCommandWrapperGoodbyeWorld = createCompileCommand("src/test/resources/clang/source/goodbye_world.cpp", null, argsGoodbye);
final Set<String> dependencyFilePathsHelloWorld = createDependencyFilePaths(new File("src/test/resources/clang/source/myinclude.h"), new File("/usr/include/nonexistentfile1.h"), new File("/usr/include/nonexistentfile2.h"));
final Set<String> dependencyFilePathsGoodbyeWorld = createDependencyFilePaths(new File("/usr/include/nonexistentfile4.h"), new File("/usr/include/nonexistentfile3.h"));
;
final ExecutableRunner executableRunner = Mockito.mock(ExecutableRunner.class);
final DirectoryManager directoryManager = Mockito.mock(DirectoryManager.class);
final DependenciesListFileManager dependenciesListFileManager = Mockito.mock(DependenciesListFileManager.class);
Mockito.when(dependenciesListFileManager.generateDependencyFilePaths(outputDir, compileCommandWrapperHelloWorld, true)).thenReturn(dependencyFilePathsHelloWorld);
Mockito.when(dependenciesListFileManager.generateDependencyFilePaths(outputDir, compileCommandWrapperGoodbyeWorld, true)).thenReturn(dependencyFilePathsGoodbyeWorld);
Mockito.when(executableRunner.executeFromDirQuietly(Mockito.any(File.class), Mockito.anyString(), Mockito.anyList())).thenReturn(new ExecutableOutput(0, "", ""));
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final CodeLocationAssembler codeLocationAssembler = new CodeLocationAssembler(externalIdFactory);
final ClangExtractor extractor = new ClangExtractor(null, executableRunner, gson, new DetectFileFinder(), directoryManager, dependenciesListFileManager, codeLocationAssembler);
final ClangLinuxPackageManager pkgMgr = Mockito.mock(ClangLinuxPackageManager.class);
final File givenDir = new File("src/test/resources/clang/source/build");
final int depth = 1;
final ExtractionId extractionId = new ExtractionId(DetectorType.CLANG, EXTRACTION_ID);
final File jsonCompilationDatabaseFile = new File("src/test/resources/clang/source/build/compile_commands_usesArguments.json");
Mockito.when(directoryManager.getExtractionOutputDirectory(Mockito.any(ExtractionId.class))).thenReturn(outputDir);
final List<PackageDetails> packages = new ArrayList<>();
packages.add(new PackageDetails("testPackageName1", "testPackageVersion1", "testPackageArch1"));
packages.add(new PackageDetails("testPackageName2", "testPackageVersion2", "testPackageArch2"));
Mockito.when(pkgMgr.getDefaultForge()).thenReturn(Forge.CENTOS);
Mockito.when(pkgMgr.getPackages(Mockito.any(File.class), Mockito.any(ExecutableRunner.class), Mockito.any(Set.class), Mockito.any(DependencyFileDetails.class))).thenReturn(packages);
Mockito.when(pkgMgr.getForges()).thenReturn(Arrays.asList(Forge.CENTOS, Forge.FEDORA, Forge.REDHAT));
final Extraction extraction = extractor.extract(pkgMgr, givenDir, depth, extractionId, jsonCompilationDatabaseFile);
checkGeneratedDependenciesComplex(extraction);
}
use of com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput in project hub-detect by blackducksoftware.
the class ClangExtractorTest method testSimple.
@Test
public void testSimple() throws ExecutableRunnerException {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
final CompileCommand compileCommandWrapper = createCompileCommand("src/test/resources/clang/source/hello_world.cpp", "gcc hello_world.cpp", null);
final Set<String> dependencyFilePaths = createDependencyFilePaths(new File("/usr/include/nonexistentfile1.h"), new File("src/test/resources/clang/source/myinclude.h"));
final ExecutableRunner executableRunner = Mockito.mock(ExecutableRunner.class);
final DirectoryManager directoryManager = Mockito.mock(DirectoryManager.class);
final DependenciesListFileManager dependenciesListFileManager = Mockito.mock(DependenciesListFileManager.class);
Mockito.when(dependenciesListFileManager.generateDependencyFilePaths(outputDir, compileCommandWrapper, true)).thenReturn(dependencyFilePaths);
Mockito.when(executableRunner.executeFromDirQuietly(Mockito.any(File.class), Mockito.anyString(), Mockito.anyList())).thenReturn(new ExecutableOutput(0, "", ""));
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final CodeLocationAssembler codeLocationAssembler = new CodeLocationAssembler(externalIdFactory);
final ClangExtractor extractor = new ClangExtractor(null, executableRunner, gson, new DetectFileFinder(), directoryManager, dependenciesListFileManager, codeLocationAssembler);
final ClangLinuxPackageManager pkgMgr = Mockito.mock(ClangLinuxPackageManager.class);
final File givenDir = new File("src/test/resources/clang/source/build");
final int depth = 1;
final ExtractionId extractionId = new ExtractionId(DetectorType.CLANG, EXTRACTION_ID);
final File jsonCompilationDatabaseFile = new File("src/test/resources/clang/source/build/compile_commands.json");
Mockito.when(directoryManager.getExtractionOutputDirectory(Mockito.any(ExtractionId.class))).thenReturn(outputDir);
final List<PackageDetails> packages = new ArrayList<>();
packages.add(new PackageDetails("testPackageName", "testPackageVersion", "testPackageArch"));
Mockito.when(pkgMgr.getDefaultForge()).thenReturn(Forge.UBUNTU);
Mockito.when(pkgMgr.getPackages(Mockito.any(File.class), Mockito.any(ExecutableRunner.class), Mockito.any(Set.class), Mockito.any(DependencyFileDetails.class))).thenReturn(packages);
Mockito.when(pkgMgr.getForges()).thenReturn(Arrays.asList(Forge.UBUNTU, Forge.DEBIAN));
final Extraction extraction = extractor.extract(pkgMgr, givenDir, depth, extractionId, jsonCompilationDatabaseFile);
checkGeneratedDependenciesSimple(extraction);
}
use of com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput 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();
}
}
use of com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput 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();
}
}
Aggregations