Search in sources :

Example 16 with Extraction

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

the class YarnLockExtractor method extract.

public Extraction extract(File projectDir, File yarnLockFile, File rootPackageJsonFile) {
    try {
        NullSafePackageJson rootPackageJson = packageJsonFiles.read(rootPackageJsonFile);
        String projectName = rootPackageJson.getName().orElse("null");
        logger.debug("Extracting Yarn project {} in {}", projectName, projectDir.getAbsolutePath());
        YarnLock yarnLock = readYarnLock(yarnLockFile);
        YarnWorkspaces workspaceData = collectWorkspaceData(projectDir);
        ExcludedIncludedWildcardFilter workspacesFilter = deriveExcludedIncludedWildcardFilter();
        YarnResult yarnResult = yarnPackager.generateCodeLocation(rootPackageJson, workspaceData, yarnLock, new ArrayList<>(), workspacesFilter);
        Optional<Exception> yarnException = yarnResult.getException();
        if (yarnException.isPresent()) {
            throw yarnException.get();
        }
        return new Extraction.Builder().projectName(yarnResult.getProjectName()).projectVersion(yarnResult.getProjectVersionName()).success(yarnResult.getCodeLocations()).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) ExcludedIncludedWildcardFilter(com.synopsys.integration.util.ExcludedIncludedWildcardFilter) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) YarnWorkspaces(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException)

Example 17 with Extraction

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

the class PnpmLockExtractorTest method testNoFailureOnNullPackageJson.

@Test
public void testNoFailureOnNullPackageJson() {
    PackageJsonFiles packageJsonFiles = new PackageJsonFiles(new PackageJsonReader(new Gson()));
    EnumListFilter<PnpmDependencyType> dependencyTypeFilter = EnumListFilter.fromExcluded(PnpmDependencyType.APP);
    PnpmLockExtractor extractor = new PnpmLockExtractor(new PnpmLockYamlParser(new PnpmYamlTransformer(dependencyTypeFilter)), packageJsonFiles);
    File pnpmLockYaml = FunctionalTestFiles.asFile("/pnpm/pnpm-lock.yaml");
    Extraction extraction = extractor.extract(pnpmLockYaml, null, new PnpmLinkedPackageResolver(new File(""), packageJsonFiles));
    Assertions.assertTrue(extraction.isSuccess());
}
Also used : PnpmLinkedPackageResolver(com.synopsys.integration.detectable.detectables.pnpm.lockfile.process.PnpmLinkedPackageResolver) PackageJsonReader(com.synopsys.integration.detectable.detectables.yarn.packagejson.PackageJsonReader) PnpmYamlTransformer(com.synopsys.integration.detectable.detectables.pnpm.lockfile.process.PnpmYamlTransformer) PnpmLockYamlParser(com.synopsys.integration.detectable.detectables.pnpm.lockfile.process.PnpmLockYamlParser) Gson(com.google.gson.Gson) Extraction(com.synopsys.integration.detectable.extraction.Extraction) PackageJsonFiles(com.synopsys.integration.detectable.detectables.yarn.packagejson.PackageJsonFiles) File(java.io.File) PnpmDependencyType(com.synopsys.integration.detectable.detectables.pnpm.lockfile.model.PnpmDependencyType) PnpmLockExtractor(com.synopsys.integration.detectable.detectables.pnpm.lockfile.PnpmLockExtractor) Test(org.junit.jupiter.api.Test)

Example 18 with Extraction

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

the class CodeLocationConverter method toDetectCodeLocation.

public Map<CodeLocation, DetectCodeLocation> toDetectCodeLocation(File detectSourcePath, Extraction extraction, File overridePath, String overrideName) {
    Map<CodeLocation, DetectCodeLocation> detectCodeLocations = new HashMap<>();
    for (CodeLocation codeLocation : extraction.getCodeLocations()) {
        File sourcePath = codeLocation.getSourcePath().orElse(overridePath);
        ExternalId externalId;
        if (!codeLocation.getExternalId().isPresent()) {
            logger.debug("The detector was unable to determine an external id for this code location, so an external id will be created using the file path.");
            String relativePath = FileNameUtils.relativize(detectSourcePath.getAbsolutePath(), sourcePath.getAbsolutePath());
            if (StringUtils.isNotBlank(relativePath)) {
                externalId = externalIdFactory.createPathExternalId(DETECT_FORGE, relativePath);
            } else {
                // Relativize from the parent.
                externalId = externalIdFactory.createPathExternalId(DETECT_FORGE, FileNameUtils.relativizeParent(detectSourcePath.getAbsolutePath(), sourcePath.getAbsolutePath()));
            }
            logger.debug("The external id that was created is: {}", Arrays.asList(externalId.getExternalIdPieces()));
        } else {
            externalId = codeLocation.getExternalId().get();
        }
        Optional<String> dockerImageName = extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA);
        DetectCodeLocation detectCodeLocation = dockerImageName.map(s -> DetectCodeLocation.forDocker(codeLocation.getDependencyGraph(), sourcePath, externalId, s)).orElseGet(() -> DetectCodeLocation.forCreator(codeLocation.getDependencyGraph(), sourcePath, externalId, overrideName));
        detectCodeLocations.put(codeLocation, detectCodeLocation);
    }
    return detectCodeLocations;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Forge(com.synopsys.integration.bdio.model.Forge) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) HashMap(java.util.HashMap) FileNameUtils(com.synopsys.integration.detect.workflow.codelocation.FileNameUtils) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Map(java.util.Map) Optional(java.util.Optional) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) DockerExtractor(com.synopsys.integration.detectable.detectables.docker.DockerExtractor) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) HashMap(java.util.HashMap) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) File(java.io.File)

Example 19 with Extraction

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

the class DetectableTool method extract.

public DetectableToolResult extract() {
    // TODO: Move docker/bazel out of detectable and drop this notion of a detctable tool. Will simplify this logic and make this unneccessary.
    DetectableResult extractable;
    try {
        extractable = detectable.extractable();
    } catch (DetectableException e) {
        extractable = new ExceptionDetectableResult(e);
    }
    if (!extractable.getPassed()) {
        logger.error(String.format("Was not extractable: %s", extractable.toDescription()));
        statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", Arrays.asList(extractable.toDescription())));
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
        exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription());
        return DetectableToolResult.failed(extractable);
    }
    logger.debug("Extractable passed.");
    ExtractionEnvironment extractionEnvironment = extractionEnvironmentProvider.createExtractionEnvironment(name);
    Extraction extraction;
    try {
        extraction = detectable.extract(extractionEnvironment);
    } catch (ExecutableFailedException | ExecutableRunnerException | JsonSyntaxException | IOException | CycleDetectedException | DetectableException | MissingExternalIdException | ParserConfigurationException | SAXException e) {
        extraction = new Extraction.Builder().exception(e).build();
    }
    if (!extraction.isSuccess()) {
        logger.error("Extraction was not success.");
        List<String> errorMessages = collectErrorMessages(extraction);
        statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", errorMessages));
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
        exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extraction.getDescription()));
        return DetectableToolResult.failed();
    } else {
        logger.debug("Extraction success.");
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.SUCCESS));
    }
    Map<CodeLocation, DetectCodeLocation> detectCodeLocationMap = codeLocationConverter.toDetectCodeLocation(sourcePath, extraction, sourcePath, name);
    List<DetectCodeLocation> detectCodeLocations = new ArrayList<>(detectCodeLocationMap.values());
    DockerTargetData dockerTargetData = DockerTargetData.fromExtraction(extraction);
    DetectToolProjectInfo projectInfo = null;
    if (StringUtils.isNotBlank(extraction.getProjectName()) || StringUtils.isNotBlank(extraction.getProjectVersion())) {
        NameVersion nameVersion = new NameVersion(extraction.getProjectName(), extraction.getProjectVersion());
        projectInfo = new DetectToolProjectInfo(detectTool, nameVersion);
    }
    logger.debug("Tool finished.");
    return DetectableToolResult.success(detectCodeLocations, projectInfo, dockerTargetData);
}
Also used : ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) NameVersion(com.synopsys.integration.util.NameVersion) CycleDetectedException(com.synopsys.integration.detectable.util.CycleDetectedException) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) DetectIssue(com.synopsys.integration.detect.workflow.status.DetectIssue) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Extraction(com.synopsys.integration.detectable.extraction.Extraction) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DockerTargetData(com.synopsys.integration.detect.lifecycle.run.data.DockerTargetData) Status(com.synopsys.integration.detect.workflow.status.Status) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) JsonSyntaxException(com.google.gson.JsonSyntaxException) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) DetectToolProjectInfo(com.synopsys.integration.detect.workflow.project.DetectToolProjectInfo) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 20 with Extraction

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

the class ClangExtractor method extract.

public Extraction extract(ClangPackageManager currentPackageManager, ClangPackageManagerRunner packageManagerRunner, File sourceDirectory, File outputDirectory, File jsonCompilationDatabaseFile, boolean cleanup) {
    try {
        logger.debug(String.format("Analyzing %s", jsonCompilationDatabaseFile.getAbsolutePath()));
        logger.debug(String.format("extract() called; compileCommandsJsonFilePath: %s", jsonCompilationDatabaseFile.getAbsolutePath()));
        List<CompileCommand> compileCommands = compileCommandDatabaseParser.parseCompileCommandDatabase(jsonCompilationDatabaseFile);
        Set<File> dependencyFileDetails = dependencyFileDetailGenerator.fromCompileCommands(compileCommands, outputDirectory, cleanup);
        PackageDetailsResult results = packageManagerRunner.getAllPackages(currentPackageManager, sourceDirectory, executableRunner, dependencyFileDetails);
        logger.trace("Found : " + results.getFoundPackages() + " packages.");
        logger.trace("Found : " + results.getUnRecognizedDependencyFiles() + " non-package files.");
        List<Forge> packageForges = forgeChooser.determineForges(currentPackageManager);
        CodeLocation codeLocation = clangPackageDetailsTransformer.toCodeLocation(packageForges, results.getFoundPackages());
        logFileCollection("Unrecognized dependency files (all)", results.getUnRecognizedDependencyFiles());
        List<File> unrecognizedIncludeFiles = results.getUnRecognizedDependencyFiles().stream().filter(file -> !isFileUnderDir(sourceDirectory, file)).collect(Collectors.toList());
        logFileCollection(String.format("Unrecognized dependency files that are outside the compile_commands.json directory (%s) and will be collected", sourceDirectory), unrecognizedIncludeFiles);
        return new Extraction.Builder().unrecognizedPaths(unrecognizedIncludeFiles).success(codeLocation).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Logger(org.slf4j.Logger) Forge(com.synopsys.integration.bdio.model.Forge) PackageDetailsResult(com.synopsys.integration.detectable.detectables.clang.packagemanager.PackageDetailsResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) ClangPackageManagerRunner(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerRunner) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ClangPackageDetailsTransformer(com.synopsys.integration.detectable.detectables.clang.dependencyfile.ClangPackageDetailsTransformer) File(java.io.File) ArrayList(java.util.ArrayList) CompileCommandDatabaseParser(com.synopsys.integration.detectable.detectables.clang.compilecommand.CompileCommandDatabaseParser) CompileCommand(com.synopsys.integration.detectable.detectables.clang.compilecommand.CompileCommand) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ClangPackageManager(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManager) DependencyFileDetailGenerator(com.synopsys.integration.detectable.detectables.clang.dependencyfile.DependencyFileDetailGenerator) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Forge(com.synopsys.integration.bdio.model.Forge) IOException(java.io.IOException) CompileCommand(com.synopsys.integration.detectable.detectables.clang.compilecommand.CompileCommand) Extraction(com.synopsys.integration.detectable.extraction.Extraction) PackageDetailsResult(com.synopsys.integration.detectable.detectables.clang.packagemanager.PackageDetailsResult) File(java.io.File)

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