@@ -20,6 +20,7 @@ def build_safety_options(
2020 policy_file = "" ,
2121 safety_project = None ,
2222 temp_requirements_name = "" ,
23+ quiet = False ,
2324):
2425 options = [
2526 "--audit-and-monitor" if audit_and_monitor else "--disable-audit-and-monitor" ,
@@ -29,7 +30,10 @@ def build_safety_options(
2930 # "minimal" maps to --short-report for concise output
3031 formats = {"full-report" : "--full-report" , "minimal" : "--short-report" }
3132
32- if output in formats :
33+ # When quiet mode is enabled, force JSON output so we can parse it
34+ if quiet :
35+ options .append ("--output=json" )
36+ elif output in formats :
3337 options .append (formats .get (output , "" ))
3438 elif output not in ["screen" , "default" ]:
3539 options .append (f"--output={ output } " )
@@ -47,7 +51,8 @@ def build_safety_options(
4751 if temp_requirements_name :
4852 temp_requirements_path = str (Path (temp_requirements_name ).absolute ())
4953 options .extend (["--file" , temp_requirements_path ])
50- console .print (f"[dim]Using requirements file: { temp_requirements_path } [/dim]" )
54+ if not quiet :
55+ console .print (f"[dim]Using requirements file: { temp_requirements_path } [/dim]" )
5156
5257 return options
5358
@@ -105,7 +110,7 @@ def get_requirements(project, use_installed, categories):
105110 return run_command (["pipenv" , "requirements" ], is_verbose = project .s .is_verbose ())
106111
107112
108- def create_temp_requirements (project , requirements ):
113+ def create_temp_requirements (project , requirements , quiet = False ):
109114 """Create a temporary requirements file that safety can access."""
110115 # Use the current directory which should be accessible
111116 temp_file_path = os .path .join (os .getcwd (), f"safety_requirements_{ os .getpid ()} .txt" )
@@ -116,7 +121,10 @@ def create_temp_requirements(project, requirements):
116121
117122 # Make sure the file exists and log its path
118123 if os .path .exists (temp_file_path ):
119- console .print (f"[dim]Created temporary requirements file: { temp_file_path } [/dim]" )
124+ if not quiet :
125+ console .print (
126+ f"[dim]Created temporary requirements file: { temp_file_path } [/dim]"
127+ )
120128 else :
121129 err .print (
122130 f"[red]Failed to create temporary requirements file at { temp_file_path } [/red]"
@@ -147,13 +155,14 @@ def is_safety_installed(project=None, system=False):
147155 return False
148156
149157
150- def install_safety (project , system = False , auto_install = False ):
158+ def install_safety (project , system = False , auto_install = False , quiet = False ):
151159 """Install safety and its dependencies."""
152160 python = project_python (project , system = system )
153161
154- console .print (
155- "[yellow bold]Safety package is required for vulnerability scanning but not installed.[/yellow bold]"
156- )
162+ if not quiet :
163+ console .print (
164+ "[yellow bold]Safety package is required for vulnerability scanning but not installed.[/yellow bold]"
165+ )
157166
158167 install = auto_install
159168 if not auto_install :
@@ -163,12 +172,14 @@ def install_safety(project, system=False, auto_install=False):
163172 )
164173
165174 if not install :
166- console .print (
167- "[yellow]Vulnerability scanning skipped. Install safety with 'pip install pipenv[safety]'[/yellow]"
168- )
175+ if not quiet :
176+ console .print (
177+ "[yellow]Vulnerability scanning skipped. Install safety with 'pip install pipenv[safety]'[/yellow]"
178+ )
169179 return False
170180
171- console .print ("[green]Installing safety...[/green]" )
181+ if not quiet :
182+ console .print ("[green]Installing safety...[/green]" )
172183 # Install safety directly rather than as an extra to ensure it works in development mode
173184 cmd = [python , "-m" , "pip" , "install" , "safety>=3.0.0" , "typer>=0.9.0" , "--quiet" ]
174185 c = run_command (cmd )
@@ -179,7 +190,8 @@ def install_safety(project, system=False, auto_install=False):
179190 )
180191 return False
181192
182- console .print ("[green]Safety installed successfully![/green]" )
193+ if not quiet :
194+ console .print ("[green]Safety installed successfully![/green]" )
183195 return True
184196
185197
@@ -339,7 +351,7 @@ def do_check( # noqa: PLR0913
339351 )
340352
341353 requirements = get_requirements (project , use_installed , categories )
342- temp_requirements = create_temp_requirements (project , requirements )
354+ temp_requirements = create_temp_requirements (project , requirements , quiet = quiet )
343355
344356 try :
345357 options = build_safety_options (
@@ -350,12 +362,16 @@ def do_check( # noqa: PLR0913
350362 policy_file = policy_file ,
351363 safety_project = safety_project ,
352364 temp_requirements_name = temp_requirements .name ,
365+ quiet = quiet ,
353366 )
354367
355368 # Check if safety is installed
356369 if not is_safety_installed (project , system = system ):
357- if not install_safety (project , system = system , auto_install = auto_install ):
358- console .print ("[yellow]Vulnerability scanning aborted.[/yellow]" )
370+ if not install_safety (
371+ project , system = system , auto_install = auto_install , quiet = quiet
372+ ):
373+ if not quiet :
374+ console .print ("[yellow]Vulnerability scanning aborted.[/yellow]" )
359375 return
360376
361377 # Check again after installation
0 commit comments