Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f450e8

Browse files
Wert1996jayvdb
authored andcommittedAug 17, 2018
RequirementsInfoExtractor.py: Extract Project Info
Extract Project Dependency Info from requirements.txt. Closes #154
1 parent 5645a4c commit 3f450e8

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed
 

‎.moban.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies:
3232
- coala_utils~=0.6.6
3333
- gemfileparser~=0.6.2
3434
- pyjsparser~=2.4.5
35-
35+
- requirements-parser~=0.1.0
3636

3737
configuration:
3838
template_dir:
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

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

0 commit comments

Comments
 (0)
Please sign in to comment.