-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RequirementsInfoExtractor.py: Extract Project Dependency Info
Extract Project Dependency Info from requirements.txt. Closes #154
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
coala_quickstart/info_extractors/RequirementsInfoExtractor.py
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,33 @@ | ||
from requirements import parse | ||
|
||
from coala_quickstart.info_extraction.InfoExtractor import InfoExtractor | ||
from coala_quickstart.info_extraction.Information import ( | ||
ProjectDependencyInfo, VersionInfo) | ||
|
||
|
||
class RequirementsInfoExtractor(InfoExtractor): | ||
supported_file_globs = ("requirements.txt",) | ||
|
||
supported_information_kinds = ( | ||
ProjectDependencyInfo, VersionInfo) | ||
|
||
def parse_file(self, fname, file_content): | ||
parsed_file = [] | ||
with open(fname, 'r') as f: | ||
parsed_file = parse(f) | ||
return parsed_file | ||
|
||
def find_information(self, fname, parsed_file): | ||
results = [] | ||
for dependency in parsed_file: | ||
results.append( | ||
ProjectDependencyInfo( | ||
fname, | ||
dependency.name, | ||
version=VersionInfo(fname, | ||
''.join(str(i) for i in | ||
dependency.specs[0])) | ||
) | ||
) | ||
|
||
return results |
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,55 @@ | ||
import os | ||
import unittest | ||
|
||
from coala_quickstart.info_extractors.RequirementsInfoExtractor import ( | ||
RequirementsInfoExtractor) | ||
from coala_quickstart.info_extraction.Information import ( | ||
ProjectDependencyInfo, VersionInfo) | ||
from tests.TestUtilities import generate_files | ||
|
||
|
||
test_file = """ | ||
# This is the file to test requirements extractor. | ||
# It should not parse this line. | ||
Babel<=2.3.4 | ||
Flask==0.11.1 # Everything after # must be ignored. | ||
Django>=1.5 # This is a comment. | ||
Jinja~2.9.6 | ||
# Neither this. | ||
""" | ||
|
||
|
||
class RequirementsInfoExtractorTest(unittest.TestCase): | ||
def setUp(self): | ||
self.current_dir = os.getcwd() | ||
|
||
def test_extracted_information(self): | ||
|
||
with generate_files( | ||
['requirements'], | ||
[test_file], | ||
self.current_dir) as gen_file: | ||
|
||
self.uut = RequirementsInfoExtractor( | ||
['requirements'], | ||
self.current_dir) | ||
extracted_info = self.uut.extract_information() | ||
extracted_info = extracted_info[ | ||
os.path.normcase('requirements') | ||
] | ||
|
||
information_types = extracted_info.keys() | ||
self.assertIn("ProjectDependencyInfo", information_types) | ||
dep_info = extracted_info['ProjectDependencyInfo'] | ||
self.assertEqual(len(dep_info), 4) | ||
|
||
requirements_list = [('Babel', '<=2.3.4'), | ||
('Flask', '==0.11.1'), | ||
('Django', '>=1.5'), | ||
('Jinja', '~2.9.6'), ] | ||
|
||
deps = [(dep.value, dep.version.value) for dep in dep_info] | ||
self.assertNotIn(('ignore_this', '<=2.4'), deps) | ||
|
||
for requirement in requirements_list: | ||
self.assertIn(requirement, deps) |