mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-23 17:41:28 +01:00
Normalized Python Package Name
This commit is contained in:
@@ -110,6 +110,11 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
*/
|
*/
|
||||||
private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();
|
private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dependency Ecosystem
|
||||||
|
*/
|
||||||
|
static final String DEPENDENCY_ECOSYSTEM = "Python.Pkg";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the name of the Python Package Analyzer.
|
* Returns the name of the Python Package Analyzer.
|
||||||
*
|
*
|
||||||
@@ -173,6 +178,7 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
@Override
|
@Override
|
||||||
protected void analyzeDependency(Dependency dependency, Engine engine)
|
protected void analyzeDependency(Dependency dependency, Engine engine)
|
||||||
throws AnalysisException {
|
throws AnalysisException {
|
||||||
|
dependency.setDependencyEcosystem(DEPENDENCY_ECOSYSTEM);
|
||||||
final File file = dependency.getActualFile();
|
final File file = dependency.getActualFile();
|
||||||
final File parent = file.getParentFile();
|
final File parent = file.getParentFile();
|
||||||
final String parentName = parent.getName();
|
final String parentName = parent.getName();
|
||||||
@@ -180,7 +186,7 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
//by definition, the containing folder of __init__.py is considered the package, even the file is empty:
|
//by definition, the containing folder of __init__.py is considered the package, even the file is empty:
|
||||||
//"The __init__.py files are required to make Python treat the directories as containing packages"
|
//"The __init__.py files are required to make Python treat the directories as containing packages"
|
||||||
//see section "6.4 Packages" from https://docs.python.org/2/tutorial/modules.html;
|
//see section "6.4 Packages" from https://docs.python.org/2/tutorial/modules.html;
|
||||||
dependency.setDisplayFileName(parentName + "/__init__.py");
|
dependency.setName(parentName);
|
||||||
dependency.getProductEvidence().addEvidence(file.getName(),
|
dependency.getProductEvidence().addEvidence(file.getName(),
|
||||||
"PackageName", parentName, Confidence.HIGHEST);
|
"PackageName", parentName, Confidence.HIGHEST);
|
||||||
|
|
||||||
@@ -217,9 +223,9 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
boolean found = false;
|
boolean found = false;
|
||||||
if (!contents.isEmpty()) {
|
if (!contents.isEmpty()) {
|
||||||
final String source = file.getName();
|
final String source = file.getName();
|
||||||
found = gatherEvidence(VERSION_PATTERN, contents, source,
|
found = gatherVersionEvidence(VERSION_PATTERN, contents, source,
|
||||||
dependency.getVersionEvidence(), "SourceVersion",
|
dependency.getVersionEvidence(), "SourceVersion",
|
||||||
Confidence.MEDIUM);
|
Confidence.MEDIUM,dependency);
|
||||||
found |= addSummaryInfo(dependency, SUMMARY_PATTERN, 4, contents,
|
found |= addSummaryInfo(dependency, SUMMARY_PATTERN, 4, contents,
|
||||||
source, "summary");
|
source, "summary");
|
||||||
if (INIT_PY_FILTER.accept(file)) {
|
if (INIT_PY_FILTER.accept(file)) {
|
||||||
@@ -311,6 +317,30 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gather package version evidence from a Python source file using the given string
|
||||||
|
* assignment regex pattern.
|
||||||
|
*
|
||||||
|
* @param pattern to scan contents with
|
||||||
|
* @param contents of Python source file
|
||||||
|
* @param source for storing evidence
|
||||||
|
* @param evidence to store evidence in
|
||||||
|
* @param name of evidence
|
||||||
|
* @param confidence in evidence
|
||||||
|
* @return whether evidence was found
|
||||||
|
*/
|
||||||
|
private boolean gatherVersionEvidence(Pattern pattern, String contents,
|
||||||
|
String source, EvidenceCollection evidence, String name,
|
||||||
|
Confidence confidence,Dependency d) {
|
||||||
|
final Matcher matcher = pattern.matcher(contents);
|
||||||
|
final boolean found = matcher.find();
|
||||||
|
if (found) {
|
||||||
|
evidence.addEvidence(source, name, matcher.group(4), confidence);
|
||||||
|
d.setVersion(matcher.group(4));
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAnalyzerEnabledSettingKey() {
|
protected String getAnalyzerEnabledSettingKey() {
|
||||||
return Settings.KEYS.ANALYZER_PYTHON_PACKAGE_ENABLED;
|
return Settings.KEYS.ANALYZER_PYTHON_PACKAGE_ENABLED;
|
||||||
|
|||||||
@@ -98,6 +98,10 @@ public class PythonPackageAnalyzerTest extends BaseTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertTrue("Version 0.0.1 not found in EggTest dependency.", found);
|
assertTrue("Version 0.0.1 not found in EggTest dependency.", found);
|
||||||
|
assertEquals("0.0.1",result.getVersion());
|
||||||
|
assertEquals("eggtest",result.getName());
|
||||||
|
assertEquals("eggtest:0.0.1",result.getDisplayFileName());
|
||||||
|
assertEquals(PythonPackageAnalyzer.DEPENDENCY_ECOSYSTEM,result.getDependencyEcosystem());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user