|
6 | 6 | import os
|
7 | 7 | import warnings
|
8 | 8 | from binascii import Error as BinAsciiError
|
| 9 | +from pathlib import Path |
9 | 10 | from typing import Any, Dict, Generator, Optional, Union, overload
|
10 | 11 |
|
11 | 12 | from aws_lambda_powertools.shared import constants
|
@@ -250,3 +251,32 @@ def dataclass_to_dict(data) -> dict:
|
250 | 251 | import dataclasses
|
251 | 252 |
|
252 | 253 | return dataclasses.asdict(data)
|
| 254 | + |
| 255 | + |
| 256 | +def abs_lambda_path(relative_path: str = "") -> str: |
| 257 | + """Return the absolute path from the given relative path to lambda handler. |
| 258 | +
|
| 259 | + Parameters |
| 260 | + ---------- |
| 261 | + relative_path : str, optional |
| 262 | + The relative path to the lambda handler, by default an empty string. |
| 263 | +
|
| 264 | + Returns |
| 265 | + ------- |
| 266 | + str |
| 267 | + The absolute path generated from the given relative path. |
| 268 | + If the environment variable LAMBDA_TASK_ROOT is set, it will use that value. |
| 269 | + Otherwise, it will use the current working directory. |
| 270 | + If the path is empty, it will return the current working directory. |
| 271 | + """ |
| 272 | + # Retrieve the LAMBDA_TASK_ROOT environment variable or default to an empty string |
| 273 | + current_working_directory = os.environ.get("LAMBDA_TASK_ROOT", "") |
| 274 | + |
| 275 | + # If LAMBDA_TASK_ROOT is not set, use the current working directory |
| 276 | + if not current_working_directory: |
| 277 | + current_working_directory = str(Path.cwd()) |
| 278 | + |
| 279 | + # Combine the current working directory and the relative path to get the absolute path |
| 280 | + absolute_path = str(Path(current_working_directory, relative_path)) |
| 281 | + |
| 282 | + return absolute_path |
0 commit comments