From 3f450e8a3194fedc325b2e2d72113de48bc1da37 Mon Sep 17 00:00:00 2001 From: Anshumaan Parashar Date: Sat, 29 Jul 2017 13:46:07 +0530 Subject: [PATCH] RequirementsInfoExtractor.py: Extract Project Info Extract Project Dependency Info from requirements.txt. Closes https://github.com/coala/coala-quickstart/issues/154 --- .moban.yaml | 2 +- .../RequirementsInfoExtractor.py | 34 +++++++++++ requirements.txt | 1 + .../RequirementsInfoExtractorTest.py | 57 +++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 coala_quickstart/info_extractors/RequirementsInfoExtractor.py create mode 100644 tests/info_extractors/RequirementsInfoExtractorTest.py diff --git a/.moban.yaml b/.moban.yaml index 01912a9..59a657e 100644 --- a/.moban.yaml +++ b/.moban.yaml @@ -32,7 +32,7 @@ dependencies: - coala_utils~=0.6.6 - gemfileparser~=0.6.2 - pyjsparser~=2.4.5 - + - requirements-parser~=0.1.0 configuration: template_dir: diff --git a/coala_quickstart/info_extractors/RequirementsInfoExtractor.py b/coala_quickstart/info_extractors/RequirementsInfoExtractor.py new file mode 100644 index 0000000..4264249 --- /dev/null +++ b/coala_quickstart/info_extractors/RequirementsInfoExtractor.py @@ -0,0 +1,34 @@ +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, + # FIXME: VersionInfo is a string, that stores only the + # first version. + version=VersionInfo(fname, + ''.join(''.join(dependency.specs[0]))) + ) + ) + + return results diff --git a/requirements.txt b/requirements.txt index 7327ffa..80acb0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ coala_bears~=0.12.0.dev20170722110839 coala_utils~=0.6.6 gemfileparser~=0.6.2 pyjsparser~=2.4.5 +requirements-parser~=0.1.0 diff --git a/tests/info_extractors/RequirementsInfoExtractorTest.py b/tests/info_extractors/RequirementsInfoExtractorTest.py new file mode 100644 index 0000000..22b24ca --- /dev/null +++ b/tests/info_extractors/RequirementsInfoExtractorTest.py @@ -0,0 +1,57 @@ +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. +Django<1.4 +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)