forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_trigger.py
391 lines (335 loc) · 13.1 KB
/
resource_trigger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""ResourceTrigger Classes for Creating PathHandlers According to a Resource"""
import re
import platform
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, cast
from typing_extensions import Protocol
from watchdog.events import FileSystemEvent, RegexMatchingEventHandler
from samcli.lib.providers.exceptions import MissingCodeUri, MissingLocalDefinition, InvalidTemplateFile
from samcli.lib.providers.provider import Function, LayerVersion, ResourceIdentifier, Stack, get_resource_by_id
from samcli.lib.providers.sam_function_provider import SamFunctionProvider
from samcli.lib.providers.sam_layer_provider import SamLayerProvider
from samcli.lib.utils.definition_validator import DefinitionValidator
from samcli.lib.utils.path_observer import PathHandler
from samcli.local.lambdafn.exceptions import FunctionNotFound, ResourceNotFound
from samcli.lib.utils.resources import RESOURCES_WITH_LOCAL_PATHS
AWS_SAM_FOLDER_REGEX = "^.*\\.aws-sam.*$"
class OnChangeCallback(Protocol):
"""Callback Type"""
def __call__(self, event: Optional[FileSystemEvent] = None) -> None:
pass
class ResourceTrigger(ABC):
"""Abstract class for creating PathHandlers for a resource.
PathHandlers returned by get_path_handlers() can then be used with an observer for
detecting file changes associated with the resource."""
def __init__(self) -> None:
pass
@abstractmethod
def get_path_handlers(self) -> List[PathHandler]:
"""List of PathHandlers that corresponds to a resource
Returns
-------
List[PathHandler]
List of PathHandlers that corresponds to a resource
"""
raise NotImplementedError("get_path_handleres is not implemented.")
@staticmethod
def get_single_file_path_handler(file_path: Path) -> PathHandler:
"""Get PathHandler for watching a single file
Parameters
----------
file_path : Path
File path object
Returns
-------
PathHandler
The PathHandler for the file specified
"""
file_path = file_path.resolve()
folder_path = file_path.parent
case_sensitive = platform.system().lower() != "windows"
file_handler = RegexMatchingEventHandler(
regexes=[f"^{re.escape(str(file_path))}$"],
ignore_regexes=[],
ignore_directories=True,
case_sensitive=case_sensitive,
)
return PathHandler(path=folder_path, event_handler=file_handler, recursive=False)
@staticmethod
def get_dir_path_handler(dir_path: Path, ignore_regexes: Optional[List[str]] = None) -> PathHandler:
"""Get PathHandler for watching a single directory
Parameters
----------
dir_path : Path
Folder path object
ignore_regexes : List[str], Optional
List of regexes that should be ignored
Returns
-------
PathHandler
The PathHandler for the folder specified
"""
dir_path = dir_path.resolve()
case_sensitive = platform.system().lower() != "windows"
file_handler = RegexMatchingEventHandler(
regexes=["^.*$"],
ignore_regexes=ignore_regexes,
ignore_directories=False,
case_sensitive=case_sensitive,
)
return PathHandler(path=dir_path, event_handler=file_handler, recursive=True, static_folder=True)
class TemplateTrigger(ResourceTrigger):
_template_file: str
_stack_name: str
_on_template_change: OnChangeCallback
_validator: DefinitionValidator
def __init__(self, template_file: str, stack_name: str, on_template_change: OnChangeCallback) -> None:
"""
Parameters
----------
template_file : str
Template file to be watched
stack_name: str
Stack name of the template
on_template_change : OnChangeCallback
Callback when template changes
"""
super().__init__()
self._template_file = template_file
self._stack_name = stack_name
self._on_template_change = on_template_change
self._validator = DefinitionValidator(Path(self._template_file))
def validate_template(self):
if not self._validator.validate_file():
raise InvalidTemplateFile(self._template_file, self._stack_name)
def _validator_wrapper(self, event: Optional[FileSystemEvent] = None) -> None:
"""Wrapper for callback that only executes if the template is valid and non-trivial changes are detected.
Parameters
----------
event : Optional[FileSystemEvent], optional
"""
if self._validator.validate_change():
self._on_template_change(event)
def get_path_handlers(self) -> List[PathHandler]:
file_path_handler = ResourceTrigger.get_single_file_path_handler(Path(self._template_file))
file_path_handler.event_handler.on_any_event = self._validator_wrapper
return [file_path_handler]
class CodeResourceTrigger(ResourceTrigger):
"""Parent class for ResourceTriggers that are for a single template resource."""
_resource_identifier: ResourceIdentifier
_resource: Dict[str, Any]
_on_code_change: OnChangeCallback
def __init__(
self,
resource_identifier: ResourceIdentifier,
stacks: List[Stack],
base_dir: Path,
on_code_change: OnChangeCallback,
):
"""
Parameters
----------
resource_identifier : ResourceIdentifier
ResourceIdentifier
stacks : List[Stack]
List of stacks
base_dir: Path
Base directory for the resource. This should be the path to template file in most cases.
on_code_change : OnChangeCallback
Callback when the resource files are changed.
Raises
------
ResourceNotFound
Raised when the resource cannot be found in the stacks.
"""
super().__init__()
self._resource_identifier = resource_identifier
resource = get_resource_by_id(stacks, resource_identifier)
if not resource:
raise ResourceNotFound()
self._resource = resource
self._on_code_change = on_code_change
self.base_dir = base_dir
class LambdaFunctionCodeTrigger(CodeResourceTrigger):
_function: Function
_code_uri: str
def __init__(
self,
function_identifier: ResourceIdentifier,
stacks: List[Stack],
base_dir: Path,
on_code_change: OnChangeCallback,
):
"""
Parameters
----------
function_identifier : ResourceIdentifier
ResourceIdentifier for the function
stacks : List[Stack]
List of stacks
base_dir: Path
Base directory for the function. This should be the path to template file in most cases.
on_code_change : OnChangeCallback
Callback when function code files are changed.
Raises
------
FunctionNotFound
raised when the function cannot be found in stacks
MissingCodeUri
raised when there is no CodeUri property in the function definition.
"""
super().__init__(function_identifier, stacks, base_dir, on_code_change)
function = SamFunctionProvider(stacks).get(str(function_identifier))
if not function:
raise FunctionNotFound()
self._function = function
code_uri = self._get_code_uri()
if not code_uri:
raise MissingCodeUri()
self._code_uri = code_uri
@abstractmethod
def _get_code_uri(self) -> Optional[str]:
"""
Returns
-------
Optional[str]
Path for the folder to be watched.
"""
raise NotImplementedError()
def get_path_handlers(self) -> List[PathHandler]:
"""
Returns
-------
List[PathHandler]
PathHandlers for the code folder associated with the function
"""
dir_path_handler = ResourceTrigger.get_dir_path_handler(
self.base_dir.joinpath(self._code_uri), ignore_regexes=[AWS_SAM_FOLDER_REGEX]
)
dir_path_handler.self_create = self._on_code_change
dir_path_handler.self_delete = self._on_code_change
dir_path_handler.event_handler.on_any_event = self._on_code_change
return [dir_path_handler]
class LambdaZipCodeTrigger(LambdaFunctionCodeTrigger):
def _get_code_uri(self) -> Optional[str]:
return self._function.codeuri
class LambdaImageCodeTrigger(LambdaFunctionCodeTrigger):
def _get_code_uri(self) -> Optional[str]:
if not self._function.metadata:
return None
return cast(Optional[str], self._function.metadata.get("DockerContext", None))
class LambdaLayerCodeTrigger(CodeResourceTrigger):
_layer: LayerVersion
_code_uri: str
def __init__(
self,
layer_identifier: ResourceIdentifier,
stacks: List[Stack],
base_dir: Path,
on_code_change: OnChangeCallback,
):
"""
Parameters
----------
layer_identifier : ResourceIdentifier
ResourceIdentifier for the layer
stacks : List[Stack]
List of stacks
base_dir: Path
Base directory for the layer. This should be the path to template file in most cases.
on_code_change : OnChangeCallback
Callback when layer code files are changed.
Raises
------
ResourceNotFound
raised when the layer cannot be found in stacks
MissingCodeUri
raised when there is no CodeUri property in the function definition.
"""
super().__init__(layer_identifier, stacks, base_dir, on_code_change)
layer = SamLayerProvider(stacks).get(str(layer_identifier))
if not layer:
raise ResourceNotFound()
self._layer = layer
code_uri = self._layer.codeuri
if not code_uri:
raise MissingCodeUri()
self._code_uri = code_uri
def get_path_handlers(self) -> List[PathHandler]:
"""
Returns
-------
List[PathHandler]
PathHandlers for the code folder associated with the layer
"""
dir_path_handler = ResourceTrigger.get_dir_path_handler(
self.base_dir.joinpath(self._code_uri), ignore_regexes=[AWS_SAM_FOLDER_REGEX]
)
dir_path_handler.self_create = self._on_code_change
dir_path_handler.self_delete = self._on_code_change
dir_path_handler.event_handler.on_any_event = self._on_code_change
return [dir_path_handler]
class DefinitionCodeTrigger(CodeResourceTrigger):
_validator: DefinitionValidator
_definition_file: str
def __init__(
self,
resource_identifier: ResourceIdentifier,
resource_type: str,
stacks: List[Stack],
base_dir: Path,
on_code_change: OnChangeCallback,
):
"""
Parameters
----------
resource_identifier : ResourceIdentifier
ResourceIdentifier for the Resource
resource_type : str
Resource type
stacks : List[Stack]
List of stacks
base_dir: Path
Base directory for the definition file. This should be the path to template file in most cases.
on_code_change : OnChangeCallback
Callback when definition file is changed.
"""
super().__init__(resource_identifier, stacks, base_dir, on_code_change)
self._resource_type = resource_type
self._definition_file = self._get_definition_file()
self._validator = DefinitionValidator(self.base_dir.joinpath(self._definition_file))
def _get_definition_file(self) -> str:
"""
Returns
-------
str
JSON/YAML definition file path
Raises
------
MissingLocalDefinition
raised when resource property related to definition path is not specified.
"""
property_name = RESOURCES_WITH_LOCAL_PATHS[self._resource_type][0]
definition_file = self._resource.get("Properties", {}).get(property_name)
if not definition_file or not isinstance(definition_file, str):
raise MissingLocalDefinition(self._resource_identifier, property_name)
return definition_file
def _validator_wrapper(self, event: Optional[FileSystemEvent] = None):
"""Wrapper for callback that only executes if the definition is valid and non-trivial changes are detected.
Parameters
----------
event : Optional[FileSystemEvent], optional
"""
if self._validator.validate_change():
self._on_code_change(event)
def get_path_handlers(self) -> List[PathHandler]:
"""
Returns
-------
List[PathHandler]
A single PathHandler for watching the definition file.
"""
file_path_handler = ResourceTrigger.get_single_file_path_handler(self.base_dir.joinpath(self._definition_file))
file_path_handler.event_handler.on_any_event = self._validator_wrapper
return [file_path_handler]