|
| 1 | +package com.blackduck.integration.detectable.detectables.uv.lockfile; |
| 2 | + |
| 3 | +import com.blackduck.integration.common.util.finder.FileFinder; |
| 4 | +import com.blackduck.integration.detectable.Detectable; |
| 5 | +import com.blackduck.integration.detectable.DetectableEnvironment; |
| 6 | +import com.blackduck.integration.detectable.detectable.DetectableAccuracyType; |
| 7 | +import com.blackduck.integration.detectable.detectable.Requirements; |
| 8 | +import com.blackduck.integration.detectable.detectable.annotation.DetectableInfo; |
| 9 | +import com.blackduck.integration.detectable.detectable.exception.DetectableException; |
| 10 | +import com.blackduck.integration.detectable.detectable.result.DetectableResult; |
| 11 | +import com.blackduck.integration.detectable.detectable.result.FailedDetectableResult; |
| 12 | +import com.blackduck.integration.detectable.detectable.result.PassedDetectableResult; |
| 13 | +import com.blackduck.integration.detectable.detectable.result.UVLockfileNotFoundDetectableResult; |
| 14 | +import com.blackduck.integration.detectable.detectables.uv.UVDetectorOptions; |
| 15 | +import com.blackduck.integration.detectable.detectables.uv.parse.UVTomlParser; |
| 16 | +import com.blackduck.integration.detectable.extraction.Extraction; |
| 17 | +import com.blackduck.integration.detectable.extraction.ExtractionEnvironment; |
| 18 | +import com.blackduck.integration.executable.ExecutableRunnerException; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +@DetectableInfo(name = "UV Lockfile", language = "Python", forge = "PyPi", accuracy = DetectableAccuracyType.HIGH, requirementsMarkdown = "File: pyproject.toml file. Lock File: uv.lock or requirements.txt file.") |
| 26 | +public class UVLockFileDetectable extends Detectable { |
| 27 | + |
| 28 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 29 | + private static final String PYPROJECT_TOML = "pyproject.toml"; |
| 30 | + private static final String UV_LOCK_FILE = "uv.lock"; |
| 31 | + private static final String REQUIREMENTS_TXT = "requirements.txt"; |
| 32 | + private final FileFinder fileFinder; |
| 33 | + private final UVDetectorOptions uvDetectorOptions; |
| 34 | + private File uvLockFile; |
| 35 | + private File requirementsTxtFile; |
| 36 | + private File uvTomlFile; |
| 37 | + private UVTomlParser uvTomlParser; |
| 38 | + private final UVLockfileExtractor uvLockfileExtractor; |
| 39 | + |
| 40 | + public UVLockFileDetectable(DetectableEnvironment environment, FileFinder fileFinder, UVDetectorOptions uvDetectorOptions, UVLockfileExtractor uvLockfileExtractor) { |
| 41 | + super(environment); |
| 42 | + this.fileFinder = fileFinder; |
| 43 | + this.uvDetectorOptions = uvDetectorOptions; |
| 44 | + this.uvLockfileExtractor = uvLockfileExtractor; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public DetectableResult applicable() { |
| 49 | + Requirements requirements = new Requirements(fileFinder, environment); |
| 50 | + uvTomlFile = requirements.file(PYPROJECT_TOML); |
| 51 | + |
| 52 | + uvLockFile = fileFinder.findFile(environment.getDirectory(), UV_LOCK_FILE); |
| 53 | + requirementsTxtFile = fileFinder.findFile(environment.getDirectory(), REQUIREMENTS_TXT); |
| 54 | + |
| 55 | + // check [tool.uv] managed setting and if set to false, skip this detector |
| 56 | + if(uvTomlFile != null) { |
| 57 | + uvTomlParser = new UVTomlParser(uvTomlFile); |
| 58 | + if(!uvTomlParser.parseManagedKey()) { |
| 59 | + return new FailedDetectableResult(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return requirements.result(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public DetectableResult extractable() throws DetectableException { |
| 68 | + if(uvLockFile == null && requirementsTxtFile == null && uvTomlFile != null) { |
| 69 | + return new UVLockfileNotFoundDetectableResult(environment.getDirectory().getAbsolutePath()); |
| 70 | + } |
| 71 | + |
| 72 | + return new PassedDetectableResult(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Extraction extract(ExtractionEnvironment extractionEnvironment) throws ExecutableRunnerException, IOException { |
| 77 | + return uvLockfileExtractor.extract(uvDetectorOptions, uvTomlParser, uvLockFile, requirementsTxtFile); |
| 78 | + } |
| 79 | +} |
0 commit comments