Skip to content

Instantly share code, notes, and snippets.

@bkhanale
Created July 14, 2019 03:22
Show Gist options
  • Save bkhanale/790f9f6e48ba43cd314142b724c869d5 to your computer and use it in GitHub Desktop.
Save bkhanale/790f9f6e48ba43cd314142b724c869d5 to your computer and use it in GitHub Desktop.
import os
from coalib.bears.GlobalBear import GlobalBear
from coalib.results.Result import Result, RESULT_SEVERITY
from sarge import capture_both
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(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 {} -n --no-annotate --no-header '
'--no-index --allow-unsafe'.format(req_files[0]))
if out.stderr.text and not out.stdout.text:
yield Result(self,
message=out.stderr.text.split('\n')[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