Skip to content

Commit 8c26dd0

Browse files
committed
RequirementsInfoExtractor.py: Extract Project Info
Extract Project Dependency Info from requirements.txt. Closes #154
1 parent 584888c commit 8c26dd0

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"""FIXME: VersionInfo is a string, that stores only the
28+
first version."""
29+
version=VersionInfo(fname,
30+
''.join(''.join(dependency.specs[0])))
31+
)
32+
)
33+
34+
return results

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coala_bears~=0.12.0.dev20170722110839
44
coala_utils~=0.6.6
55
gemfileparser~=0.6.2
66
pyjsparser~=2.4.5
7+
requirements-parser~=0.1.0
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
Django<1.4
18+
Jinja~2.9.6
19+
# Neither this.
20+
"""
21+
22+
23+
class RequirementsInfoExtractorTest(unittest.TestCase):
24+
def setUp(self):
25+
self.current_dir = os.getcwd()
26+
27+
def test_extracted_information(self):
28+
29+
with generate_files(
30+
['requirements'],
31+
[test_file],
32+
self.current_dir) as gen_file:
33+
34+
self.uut = RequirementsInfoExtractor(
35+
['requirements'],
36+
self.current_dir)
37+
extracted_info = self.uut.extract_information()
38+
extracted_info = extracted_info[
39+
os.path.normcase('requirements')
40+
]
41+
42+
information_types = extracted_info.keys()
43+
self.assertIn("ProjectDependencyInfo", information_types)
44+
dep_info = extracted_info['ProjectDependencyInfo']
45+
self.assertEqual(len(dep_info), 4)
46+
47+
requirements_list = [('Babel', '<=2.3.4'),
48+
('Flask', '==0.11.1'),
49+
('Django', '>=1.5'),
50+
('Jinja', '~2.9.6'), ]
51+
52+
deps = [(dep.value, dep.version.value) for dep in dep_info]
53+
self.assertNotIn(('ignore_this', '<=2.4'), deps)
54+
55+
for requirement in requirements_list:
56+
self.assertIn(requirement, deps)

0 commit comments

Comments
 (0)