Skip to content

Instantly share code, notes, and snippets.

@bkhanale
Created July 22, 2019 12:08
Show Gist options
  • Save bkhanale/c8db25dfe1a0198ffac4e5c5d1f4a717 to your computer and use it in GitHub Desktop.
Save bkhanale/c8db25dfe1a0198ffac4e5c5d1f4a717 to your computer and use it in GitHub Desktop.
import os.path
from coalib.bears.GlobalBear import GlobalBear
from coalib.results.Result import Result, RESULT_SEVERITY
from sarge import capture_both
from dependency_management.requirements.PipRequirement import PipRequirement
class RequirementsCheckBear(GlobalBear):
"""
The bear to check and find any conflicting dependencies.
"""
LANGUAGES = {
'Python Requirements',
'Python 2 Requirements',
'Python 3 Requirements',
}
REQUIREMENTS = {PipRequirement('pip-tools', '3.8.0')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'
def run(self, req_files: tuple):
"""
:param req_files:
Tuple of requirements files.
"""
data = ''
orig_file = ''
for req_file in req_files:
if not os.path.isfile(os.path.abspath(req_file)):
raise ValueError('The file \'{}\' doesn\'t exist.'
.format(req_file))
with open(req_file) as req:
cont = req.read()
if not orig_file:
orig_file = cont
else:
data += cont
with open(req_files[0], 'a+') as temp_file:
temp_file.write(data)
out = capture_both('pip-compile {} -r -n --no-annotate --no-header '
'--no-index --allow-unsafe'.format(req_files[0]))
if out.stderr.text and not out.stdout.text:
lines = out.stderr.text.splitlines()
yield Result(self,
message=[s for s in lines if 'Could not' in s][0],
severity=RESULT_SEVERITY.MAJOR,
)
with open(req_files[0], 'w+') as temp_file:
temp_file.write(orig_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment