44import os
55import re
66import sys
7- import tempfile
8- from contextlib import suppress
97from io import StringIO
108from pathlib import Path
119from typing import List , Optional , Pattern
@@ -145,7 +143,8 @@ def check_dependencies(ctx):
145143 importlib .invalidate_caches ()
146144
147145 # reload original, unpatched safety
148- from safety .formatter import report
146+ from safety .formatter import SafetyFormatter
147+ from safety .safety import calculate_remediations
149148 from safety .safety import check as safety_check
150149 from safety .util import read_requirements
151150
@@ -159,10 +158,19 @@ def check_dependencies(ctx):
159158 # check using safety as a library
160159 def safety (): # noqa: WPS430
161160 packages = list (read_requirements (StringIO (requirements )))
162- vulns = safety_check (packages = packages , ignore_ids = "" , key = "" , db_mirror = "" , cached = False , proxy = {})
163- output_report = report (vulns = vulns , full = True , checked_packages = len (packages ))
161+ vulns , db_full = safety_check (packages = packages , ignore_vulns = "" )
162+ remediations = calculate_remediations (vulns , db_full )
163+ output_report = SafetyFormatter ("text" ).render_vulnerabilities (
164+ announcements = [],
165+ vulnerabilities = vulns ,
166+ remediations = remediations ,
167+ full = True ,
168+ packages = packages ,
169+ )
164170 if vulns :
165171 print (output_report )
172+ return False
173+ return True
166174
167175 ctx .run (safety , title = "Checking dependencies" )
168176
@@ -188,49 +196,7 @@ def check_types(ctx): # noqa: WPS231
188196 Arguments:
189197 ctx: The context instance (passed automatically).
190198 """
191- # NOTE: the following code works around this issue:
192- # https://github.com/python/mypy/issues/10633
193-
194- # compute packages directory path
195- py = f"{ sys .version_info .major } .{ sys .version_info .minor } "
196- pkgs_dir = Path ("__pypackages__" , py , "lib" ).resolve ()
197-
198- # build the list of available packages
199- packages = {}
200- for package in pkgs_dir .glob ("*" ):
201- if package .suffix not in {".dist-info" , ".pth" } and package .name != "__pycache__" :
202- packages [package .name ] = package
203-
204- # handle .pth files
205- for pth in pkgs_dir .glob ("*.pth" ):
206- with suppress (OSError ):
207- for package in Path (pth .read_text ().splitlines ()[0 ]).glob ("*" ): # noqa: WPS440
208- if package .suffix != ".dist-info" :
209- packages [package .name ] = package
210-
211- # create a temporary directory to assign to MYPYPATH
212- with tempfile .TemporaryDirectory () as tmpdir :
213-
214- # symlink the stubs
215- ignore = set ()
216- for stubs in (path for name , path in packages .items () if name .endswith ("-stubs" )): # noqa: WPS335
217- Path (tmpdir , stubs .name ).symlink_to (stubs , target_is_directory = True )
218- # try to symlink the corresponding package
219- # see https://www.python.org/dev/peps/pep-0561/#stub-only-packages
220- pkg_name = stubs .name .replace ("-stubs" , "" )
221- if pkg_name in packages :
222- ignore .add (pkg_name )
223- Path (tmpdir , pkg_name ).symlink_to (packages [pkg_name ], target_is_directory = True )
224-
225- # create temporary mypy config to ignore stubbed packages
226- newconfig = Path ("config" , "mypy.ini" ).read_text ()
227- newconfig += "\n " + "\n \n " .join (f"[mypy-{ pkg } .*]\n ignore_errors=true" for pkg in ignore )
228- tmpconfig = Path (tmpdir , "mypy.ini" )
229- tmpconfig .write_text (newconfig )
230-
231- # set MYPYPATH and run mypy
232- os .environ ["MYPYPATH" ] = tmpdir
233- ctx .run (f"mypy --config-file { tmpconfig } { PY_SRC } " , title = "Type-checking" , pty = PTY )
199+ ctx .run (f"mypy --config-file config/mypy.ini { PY_SRC } " , title = "Type-checking" , pty = PTY )
234200
235201
236202@duty (silent = True )
0 commit comments