-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch is a rewrite of the remediation implementation. It makes several changes: - Remove the reliance on `inspect`, and instead get top level deps by parsing the manifest file. This should be significantly quicker than a pip install, making the fix process much faster for larger projects - Change the parser to attempt to better preserve package casing, version comparators, and extras (comments, markers, etc) - Update tests and add new tests. In a couple of cases, this actually involves fixing the test fixture, as the fixture itself was making changes such as package name casing, adding extra new lines to the manifest etc.
- Loading branch information
1 parent
2622af2
commit 61cfb58
Showing
7 changed files
with
303 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
type VersionComparator = '<' | '<=' | '!=' | '==' | '>=' | '>' | '~='; | ||
|
||
interface Requirement { | ||
originalText: string; | ||
line: number; | ||
name?: string; | ||
versionComparator?: VersionComparator; | ||
version?: string; | ||
extras?: string; | ||
} | ||
|
||
/** | ||
* Converts a requirements file into an array of parsed requirements, with data | ||
* such as name, version, etc. | ||
* @param requirementsFile A requirements.txt file as a string | ||
*/ | ||
export function parseRequirementsFile(requirementsFile: string): Requirement[] { | ||
return requirementsFile.split('\n').map((requirementText, line) => { | ||
const requirement: Requirement = { originalText: requirementText, line }; | ||
const trimmedText = requirementText.trim(); | ||
|
||
// Quick returns for cases we cannot remediate | ||
// - Empty line i.e. '' | ||
// - 'editable' packages i.e. '-e git://git.myproject.org/MyProject.git#egg=MyProject' | ||
// - Comments i.e. # This is a comment | ||
// - Local files i.e. file:../../lib/project#egg=MyProject | ||
if ( | ||
requirementText === '' || | ||
trimmedText.startsWith('-e') || | ||
trimmedText.startsWith('#') || | ||
trimmedText.startsWith('file:') | ||
) { | ||
return requirement; | ||
} | ||
|
||
// Regex to match against a Python package specifier. Any invalid lines (or | ||
// lines we can't handle) should have been returned this point. | ||
const regex = /([A-Z0-9]*)(===|==|>=|<=|>|<|~=)(\d\.?\d?\.?\d?)(.*)/i; | ||
const result = regex.exec(requirementText); | ||
|
||
if (result !== null) { | ||
requirement.name = result[1]; | ||
requirement.versionComparator = result[2] as VersionComparator; | ||
requirement.version = result[3]; | ||
requirement.extras = result[4]; | ||
} | ||
|
||
return requirement; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
Jinja2==2.7.2 | ||
django==2.0.1 | ||
Django==2.0.1 | ||
python-etcd==0.4.5 | ||
Django-Select2==6.0.1 # this version installs with lowercase so it catches a previous bug in pip_resolve.py | ||
irc==16.2 # this has a cyclic dependecy (interanl jaraco.text <==> jaraco.collections) | ||
testtools==\ | ||
2.3.0 # this has a cycle (fixtures ==> testtols); | ||
./packages/prometheus_client-0.6.0 | ||
transitive>=1.1.1 # not directly required, pinned by Snyk to avoid a vulnerability | ||
transitive>=1.1.1 # not directly required, pinned by Snyk to avoid a vulnerability |
Oops, something went wrong.