11import os
22import shutil
3- from pathlib import Path
4- import sys
5-
6- IAC_DIRECTORY_NAME = "iac"
7- SOURCE_DIRECTORY_NAME = "src"
8- LAMBDA_LAYER_PREFIX = os .path .join ("python" , "src" )
9-
10-
11- def adjust_layer_directory (shared_dir_name : str , destination : str ):
12- # Get the root directory of the source directory
13- root_directory = Path (__file__ ).parent .parent
14- iac_directory = os .path .join (root_directory , IAC_DIRECTORY_NAME )
15-
16- print (f"Root directory: { root_directory } " )
17- print (f"Root direcotry files: { os .listdir (root_directory )} " )
18- print (f"IaC directory: { iac_directory } " )
19- print (f"IaC directory files: { os .listdir (iac_directory )} " )
20-
21-
22- # Get the destination and source directory
23- destination_directory = os .path .join (root_directory , IAC_DIRECTORY_NAME , destination )
24- source_directory = os .path .join (root_directory , SOURCE_DIRECTORY_NAME , shared_dir_name )
25-
26- # Delete the destination directory if it exists
27- if os .path .exists (destination_directory ):
28- shutil .rmtree (destination_directory )
29-
30- # Copy the source directory to the destination directory
31- shutil .copytree (source_directory , os .path .join (destination_directory , LAMBDA_LAYER_PREFIX , shared_dir_name ))
32- print (
33- f"Copying files from { source_directory } to { os .path .join (destination_directory , LAMBDA_LAYER_PREFIX , shared_dir_name )} " )
34-
35-
3+ import subprocess
4+ from pathlib import Path # Importe a biblioteca pathlib
5+
6+ # --- Configurações ---
7+ BUILD_DIRECTORY = "build"
8+ PYTHON_TOP_LEVEL_DIR = os .path .join (BUILD_DIRECTORY , "python" )
9+ REQUIREMENTS_FILE = "requirements-layer.txt"
10+
11+ # --- CONSTRUÇÃO CORRETA DO CAMINHO ---
12+ # Pega o diretório do projeto (a raiz 'reservation_api') subindo um nível a partir do script atual.
13+ PROJECT_ROOT = Path (__file__ ).parent .parent
14+ # Agora, constrói o caminho para 'src/shared' a partir da raiz do projeto.
15+ SHARED_CODE_SOURCE = os .path .join (PROJECT_ROOT , "src" , "shared" )
16+
17+
18+ def adjust_layer_directory ():
19+ """
20+ Prepara um diretório 'build' para uma Lambda Layer do AWS CDK.
21+
22+ A função junta o código local compartilhado e as dependências externas (pip)
23+ na estrutura de pastas que a Lambda espera (/python).
24+ """
25+
26+ # Garante que o build seja sempre limpo, removendo qualquer artefato antigo.
27+ if os .path .exists (BUILD_DIRECTORY ):
28+ shutil .rmtree (BUILD_DIRECTORY )
29+
30+ # Cria a estrutura de pastas 'build/python/src/'.
31+ # Isso é necessário para que os imports 'from src.shared...' funcionem na Lambda.
32+ shared_code_intermediate_dir = os .path .join (PYTHON_TOP_LEVEL_DIR , "src" )
33+ os .makedirs (shared_code_intermediate_dir )
34+
35+ # Copia o código compartilhado (de 'src/shared') para dentro da estrutura da Layer.
36+ # O resultado final será 'build/python/src/shared'.
37+ print (f"Copiando código de: { SHARED_CODE_SOURCE } " ) # Adicionado para debug
38+ shared_code_dest = os .path .join (shared_code_intermediate_dir , os .path .basename (SHARED_CODE_SOURCE ))
39+ shutil .copytree (SHARED_CODE_SOURCE , shared_code_dest )
40+
41+ # Se o arquivo de dependências existir, instala todas as bibliotecas.
42+ # O arquivo de requirements também precisa ser lido a partir da raiz.
43+ requirements_path = os .path .join (PROJECT_ROOT , REQUIREMENTS_FILE )
44+ if os .path .exists (requirements_path ):
45+ # Instala os pacotes diretamente na pasta 'build/python'.
46+ # Isso permite que a Lambda importe as bibliotecas de forma padrão (ex: import requests).
47+ subprocess .check_call (
48+ ["pip" , "install" , "-r" , requirements_path , "-t" , PYTHON_TOP_LEVEL_DIR , "--no-cache-dir" ]
49+ )
50+ else :
51+ # Apenas um aviso caso o arquivo não seja encontrado.
52+ print (f"Aviso: Arquivo '{ requirements_path } ' não encontrado. Nenhuma dependência externa será instalada." )
53+
54+
55+ # Ponto de entrada do script.
3656if __name__ == '__main__' :
37- adjust_layer_directory (shared_dir_name = "shared" , destination = "copied_shared" )
57+ adjust_layer_directory ()
0 commit comments