Skip to content

Commit a6397c2

Browse files
committed
RequirementsInfoExtractor.py: Extract Project Dependency Info
Extract Project Dependency Info from requirements.txt. Closes #154
1 parent 02369a9 commit a6397c2

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from requirements import parse
2+
3+
from coala_quickstart.info_extraction.InfoExtractor import InfoExtractor
4+
from coala_quickstart.info_extraction.Information import (
5+
ProjectDependencyInfo, VersionInfo)
6+
7+
8+
class RequirementsInfoExtractor(InfoExtractor):
9+
supported_file_globs = ("requirements.txt",)
10+
11+
supported_information_kinds = (
12+
ProjectDependencyInfo, VersionInfo)
13+
14+
def parse_file(self, fname, file_content):
15+
parsed_file = []
16+
with open(fname, 'r') as f:
17+
parsed_file = parse(f)
18+
return parsed_file
19+
20+
def find_information(self, fname, parsed_file):
21+
results = []
22+
for dependency in parsed_file:
23+
results.append(
24+
ProjectDependencyInfo(
25+
fname,
26+
dependency.name,
27+
version=VersionInfo(fname,
28+
''.join(str(i) for i in
29+
dependency.specs[0]))
30+
)
31+
)
32+
33+
return results
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import unittest
3+
4+
from coala_quickstart.info_extractors.RequirementsInfoExtractor import (
5+
RequirementsInfoExtractor)
6+
from coala_quickstart.info_extraction.Information import (
7+
ProjectDependencyInfo, VersionInfo)
8+
from tests.TestUtilities import generate_files
9+
10+
11+
test_file = """
12+
# This is the file to test requirements extractor.
13+
# It should not parse this line.
14+
Babel<=2.3.4
15+
Flask==0.11.1 # Everything after # must be ignored.
16+
Django>=1.5 # This is a comment.
17+
Jinja~2.9.6
18+
# Neither this.
19+
"""
20+
21+
22+
class RequirementsInfoExtractorTest(unittest.TestCase):
23+
def setUp(self):
24+
self.current_dir = os.getcwd()
25+
26+
def test_extracted_information(self):
27+
28+
with generate_files(
29+
['requirements'],
30+
[test_file],
31+
self.current_dir) as gen_file:
32+
33+
self.uut = RequirementsInfoExtractor(
34+
['requirements'],
35+
self.current_dir)
36+
extracted_info = self.uut.extract_information()
37+
extracted_info = extracted_info[
38+
os.path.normcase('requirements')
39+
]
40+
41+
information_types = extracted_info.keys()
42+
self.assertIn("ProjectDependencyInfo", information_types)
43+
dep_info = extracted_info['ProjectDependencyInfo']
44+
self.assertEqual(len(dep_info), 4)
45+
46+
requirements_list = [('Babel', '<=2.3.4'),
47+
('Flask', '==0.11.1'),
48+
('Django', '>=1.5'),
49+
('Jinja', '~2.9.6'), ]
50+
51+
deps = [(dep.value, dep.version.value) for dep in dep_info]
52+
self.assertNotIn(('ignore_this', '<=2.4'), deps)
53+
54+
for requirement in requirements_list:
55+
self.assertIn(requirement, deps)

0 commit comments

Comments
 (0)