com.metaeffekt.artifact.analysis.scancode.ScanCodeProcessClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metaeffekt.artifact.analysis.scancode;
import com.metaeffekt.artifact.analysis.utils.FileUtils;
import com.metaeffekt.artifact.utils.ConfigurationDiscovery;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.UUID;
public class ScanCodeProcessClient implements ScanCodeClient {
private static final Logger LOG = LoggerFactory.getLogger(ScanCodeProcessClient.class);
private final String numberOfThreads;
public ScanCodeProcessClient(String numberOfThreads) {
this.numberOfThreads = numberOfThreads;
}
@Override
public ScanRequestResponse scan(String scanPath, String outputFile) throws IOException {
execute("-l", "-c", "-n", getScanCodeThreads(), "--only-findings", "--json-pp", outputFile, scanPath);
if (!FileUtils.exists(outputFile)) {
throw new IOException(String.format("Output file %s does not exits.", outputFile));
}
return new ScanRequestResponse(scanPath, outputFile, UUID.randomUUID());
}
private String getScanCodeThreads() {
return numberOfThreads;
}
private void execute(String... parameter) {
ExecTask execTask = new ExecTask();
Project project = new Project();
execTask.setProject(project);
Commandline command = new Commandline();
String pathToScancodeToolkit = discoverScanCode();
command.setExecutable(pathToScancodeToolkit);
Environment.Variable var = new Environment.Variable();
var.setKey("LC_ALL");
var.setValue("en_US.UTF-8");
execTask.addEnv(var);
LOG.info("executing: {}", pathToScancodeToolkit);
// FIXME: check whether this is still required
// required to circumvent issue on MacOS
// https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
var = new Environment.Variable();
var.setKey("OBJC_DISABLE_INITIALIZE_FORK_SAFETY");
var.setValue("YES");
execTask.addEnv(var);
command.addArguments(parameter);
execTask.setCommand(command);
execTask.execute();
}
public String discoverScanCode() {
final String pathToScancodeToolkit = System.getProperty("scancode.toolkit.path");
if (pathToScancodeToolkit != null && new File(pathToScancodeToolkit).exists()) {
return pathToScancodeToolkit;
}
final Properties p = ConfigurationDiscovery.discoverLocalProperties();
final String scancodePath = p.getProperty("scancode.toolkit.path");
if (scancodePath == null || !new File(scancodePath).exists()) {
LOG.warn("Cannot resolve scancode. Configured path: scancode.toolkit.path={}", scancodePath);
}
return scancodePath;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy