1
1
from pathlib import Path
2
2
import sys
3
3
from typing import TYPE_CHECKING , List , Optional , Tuple
4
-
4
+ import logging
5
5
import typer
6
6
7
- from .constants import MSG_MISSING_SAFETY_SOURCE
7
+ from safety .tool .utils import PoetryPyprojectConfigurator
8
+
9
+ from .constants import MSG_SAFETY_SOURCE_ADDED , MSG_SAFETY_SOURCE_NOT_ADDED
8
10
from .parser import PoetryParser
9
11
10
12
from ..base import BaseCommand , ToolIntentionType
24
26
else :
25
27
import tomli as tomllib
26
28
29
+ logger = logging .getLogger (__name__ )
30
+
27
31
28
32
class PoetryCommand (BaseCommand ):
29
33
"""
@@ -74,23 +78,7 @@ def get_package_list_command(self) -> List[str]:
74
78
"""
75
79
return ["poetry" , "run" , "pip" , "list" , "--format=json" ]
76
80
77
- @classmethod
78
- def from_args (cls , args : List [str ], ** kwargs ):
79
- parser = PoetryParser ()
80
-
81
- if intention := parser .parse (args ):
82
- if intention .intention_type is ToolIntentionType .ADD_PACKAGE :
83
- return PoetryAddCommand (args , intention = intention , ** kwargs )
84
-
85
- return PoetryGenericCommand (args , ** kwargs )
86
-
87
-
88
- class PoetryGenericCommand (PoetryCommand ):
89
- pass
90
-
91
-
92
- class PoetryAddCommand (PoetryCommand ):
93
- def has_safety_source_in_pyproject (self ) -> bool :
81
+ def _has_safety_source_in_pyproject (self ) -> bool :
94
82
"""
95
83
Check if 'safety' source exists in pyproject.toml
96
84
"""
@@ -113,6 +101,55 @@ def has_safety_source_in_pyproject(self) -> bool:
113
101
except (FileNotFoundError , KeyError , tomllib .TOMLDecodeError ):
114
102
return False
115
103
104
+ def before (self , ctx : typer .Context ):
105
+ super ().before (ctx )
106
+
107
+ if self ._intention and self ._intention .intention_type in [
108
+ ToolIntentionType .SYNC_PACKAGES ,
109
+ ToolIntentionType .ADD_PACKAGE ,
110
+ ]:
111
+ if not self ._has_safety_source_in_pyproject ():
112
+ org_slug = None
113
+ try :
114
+ data = ctx .obj .auth .client .initialize ()
115
+ org_slug = data .get ("organization-data" , {}).get ("slug" )
116
+ except Exception :
117
+ logger .exception (
118
+ "Unable to pull the org slug from the initialize endpoint."
119
+ )
120
+
121
+ try :
122
+ configurator = PoetryPyprojectConfigurator ()
123
+ prj_slug = ctx .obj .project .id if ctx .obj .project else None
124
+ if configurator .configure (
125
+ Path ("pyproject.toml" ), org_slug , prj_slug
126
+ ):
127
+ console .print (
128
+ MSG_SAFETY_SOURCE_ADDED ,
129
+ )
130
+ except Exception :
131
+ logger .exception ("Unable to configure the pyproject.toml file." )
132
+ console .print (
133
+ MSG_SAFETY_SOURCE_NOT_ADDED ,
134
+ )
135
+
136
+ @classmethod
137
+ def from_args (cls , args : List [str ], ** kwargs ):
138
+ parser = PoetryParser ()
139
+
140
+ if intention := parser .parse (args ):
141
+ kwargs ["intention" ] = intention
142
+ if intention .intention_type is ToolIntentionType .ADD_PACKAGE :
143
+ return PoetryAddCommand (args , ** kwargs )
144
+
145
+ return PoetryGenericCommand (args , ** kwargs )
146
+
147
+
148
+ class PoetryGenericCommand (PoetryCommand ):
149
+ pass
150
+
151
+
152
+ class PoetryAddCommand (PoetryCommand ):
116
153
def patch_source_option (
117
154
self , args : List [str ], new_source : str = "safety"
118
155
) -> Tuple [Optional [str ], List [str ]]:
@@ -146,11 +183,5 @@ def patch_source_option(
146
183
def before (self , ctx : typer .Context ):
147
184
super ().before (ctx )
148
185
149
- if not self .has_safety_source_in_pyproject ():
150
- console .print (
151
- MSG_MISSING_SAFETY_SOURCE ,
152
- )
153
- sys .exit (1 )
154
-
155
186
_ , modified_args = self .patch_source_option (self ._args )
156
187
self ._args = modified_args
0 commit comments