Search in sources :

Example 21 with Extraction

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

the class CargoExtractor method extract.

public Extraction extract(File cargoLockFile, @Nullable File cargoTomlFile) throws IOException, DetectableException, MissingExternalIdException {
    CargoLockData cargoLockData = new Toml().read(cargoLockFile).to(CargoLockData.class);
    List<CargoLockPackage> packages = cargoLockData.getPackages().orElse(new ArrayList<>()).stream().map(cargoLockPackageDataTransformer::transform).collect(Collectors.toList());
    DependencyGraph graph = cargoLockPackageTransformer.transformToGraph(packages);
    Optional<NameVersion> projectNameVersion = Optional.empty();
    if (cargoTomlFile != null) {
        String cargoTomlContents = FileUtils.readFileToString(cargoTomlFile, StandardCharsets.UTF_8);
        projectNameVersion = cargoTomlParser.parseNameVersionFromCargoToml(cargoTomlContents);
    }
    // TODO: Consider for 8.0.0 providing an external ID.
    CodeLocation codeLocation = new CodeLocation(graph);
    return new Extraction.Builder().success(codeLocation).nameVersionIfPresent(projectNameVersion).build();
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) CargoLockData(com.synopsys.integration.detectable.detectables.cargo.data.CargoLockData) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Toml(com.moandjiezana.toml.Toml) CargoLockPackage(com.synopsys.integration.detectable.detectables.cargo.model.CargoLockPackage)

Example 22 with Extraction

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

the class PodlockExtractor method extract.

public Extraction extract(File podlock) {
    String podLockText;
    try {
        logger.trace(String.format("Reading from the pod lock file %s", podlock.getAbsolutePath()));
        podLockText = FileUtils.readFileToString(podlock, StandardCharsets.UTF_8);
        logger.trace("Finished reading from the pod lock file.");
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
    DependencyGraph dependencyGraph;
    try {
        logger.trace("Attempting to create the dependency graph from the pod lock file.");
        dependencyGraph = podlockParser.extractDependencyGraph(podLockText);
        logger.trace("Finished creating the dependency graph from the pod lock file.");
    } catch (IOException | MissingExternalIdException e) {
        return new Extraction.Builder().exception(e).build();
    }
    CodeLocation codeLocation = new CodeLocation(dependencyGraph);
    return new Extraction.Builder().success(codeLocation).build();
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException)

Example 23 with Extraction

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

the class SbtDotExtractor method extract.

public Extraction extract(File directory, ExecutableTarget sbt, String sbtCommandAdditionalArguments) {
    try {
        List<String> sbtArgs = sbtCommandArgumentGenerator.generateSbtCmdArgs(sbtCommandAdditionalArguments, "dependencyDot");
        Executable dotExecutable = ExecutableUtils.createFromTarget(directory, sbt, sbtArgs);
        ExecutableOutput dotOutput = executableRunner.executeSuccessfully(dotExecutable);
        List<File> dotGraphs = sbtDotOutputParser.parseGeneratedGraphFiles(dotOutput.getStandardOutputAsList());
        Extraction.Builder extraction = new Extraction.Builder();
        for (File dotGraph : dotGraphs) {
            GraphParser graphParser = new GraphParser(FileUtils.openInputStream(dotGraph));
            Set<String> rootIDs = sbtRootNodeFinder.determineRootIDs(graphParser);
            // typically found in project-folder/target/<>.dot so .parent.parent == project folder
            File projectFolder = dotGraph.getParentFile().getParentFile();
            if (rootIDs.size() == 1) {
                String projectId = rootIDs.stream().findFirst().get();
                DependencyGraph graph = sbtGraphParserTransformer.transformDotToGraph(graphParser, projectId);
                Dependency projectDependency = graphNodeParser.nodeToDependency(projectId);
                extraction.codeLocations(new CodeLocation(graph, projectDependency.getExternalId(), projectFolder));
                if (projectFolder.equals(directory)) {
                    extraction.projectName(projectDependency.getName());
                    extraction.projectVersion(projectDependency.getVersion());
                }
            } else {
                logger.warn("Unable to determine which node was the project in an SBT graph: " + dotGraph.toString());
                logger.warn("This may mean you have extraneous dependencies and should consider removing them. The dependencies are: " + String.join(",", rootIDs));
                DependencyGraph graph = sbtGraphParserTransformer.transformDotToGraph(graphParser, rootIDs);
                extraction.codeLocations(new CodeLocation(graph, projectFolder));
            }
        }
        return extraction.success().build();
    } catch (IOException | DetectableException | ExecutableFailedException e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GraphParser(com.paypal.digraph.parser.GraphParser) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) IOException(java.io.IOException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 24 with Extraction

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

the class ProjectInspectorExtractor method extract.

public Extraction extract(ProjectInspectorOptions projectInspectorOptions, List<String> extra, File targetDirectory, File outputDirectory, ExecutableTarget inspector) throws ExecutableFailedException {
    File outputFile = new File(outputDirectory, "inspection.json");
    List<String> arguments = new LinkedList<>();
    arguments.add("inspect");
    arguments.add("--dir");
    arguments.add(targetDirectory.toString());
    arguments.add("--output-file");
    arguments.add(outputFile.toString());
    arguments.addAll(extra);
    Optional.ofNullable(projectInspectorOptions.getAdditionalArguments()).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> arguments.addAll(Arrays.asList(additionalArguments)));
    executableRunner.executeSuccessfully(ExecutableUtils.createFromTarget(targetDirectory, inspector, arguments));
    try {
        String outputContents = FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8);
        List<CodeLocation> codeLocations = projectInspectorParser.parse(outputContents);
        return new Extraction.Builder().success(codeLocations).build();
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Arrays(java.util.Arrays) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) 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) LinkedList(java.util.LinkedList) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 25 with Extraction

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

the class SbtResolutionCacheExtractor method extract.

public Extraction extract(File directory, SbtResolutionCacheOptions sbtResolutionCacheOptions) {
    // TODO: Extractor should not use DetectableOptions
    try {
        // TODO: Handle null better.
        List<String> included = sbtResolutionCacheOptions.getIncludedConfigurations();
        List<String> excluded = sbtResolutionCacheOptions.getExcludedConfigurations();
        int depth = sbtResolutionCacheOptions.getReportDepth();
        SbtProject project = extractProject(directory, sbtResolutionCacheOptions.isFollowSymLinks(), depth, included, excluded);
        List<CodeLocation> codeLocations = new ArrayList<>();
        String projectName = null;
        String projectVersion = null;
        for (SbtDependencyModule module : project.getModules()) {
            CodeLocation codeLocation;
            if (project.getProjectExternalId() != null) {
                codeLocation = new CodeLocation(module.getGraph(), project.getProjectExternalId());
            } else {
                codeLocation = new CodeLocation(module.getGraph());
            }
            if (projectName == null) {
                projectName = project.getProjectName();
                projectVersion = project.getProjectVersion();
            }
            codeLocations.add(codeLocation);
        }
        if (codeLocations.size() > 0) {
            return new Extraction.Builder().success(codeLocations).projectName(projectName).projectVersion(projectVersion).build();
        } else {
            logger.error("Unable to find any dependency information.");
            return new Extraction.Builder().failure("Unable to find any dependency information.").build();
        }
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) SbtProject(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

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