Skip to content

Commit 3ef526f

Browse files
Function Support for pyfiles
1 parent be1f63c commit 3ef526f

5 files changed

Lines changed: 37 additions & 14 deletions

File tree

src/datacustomcode/deploy.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,11 @@ def create_deployment(
279279
DEPENDENCIES_ARCHIVE_PATH = os.path.join(
280280
"payload", "archives", DEPENDENCIES_ARCHIVE_FULL_NAME
281281
)
282+
PY_FILES_PATH = os.path.join("payload", "py-files")
282283
ZIP_FILE_NAME = "deployment.zip"
283284

284285

285-
def prepare_dependency_archive(directory: str, docker_network: str) -> None:
286+
def prepare_dependency_archive(directory: str, docker_network: str, package_type: str) -> None:
286287
cmd = f"docker images -q {DOCKER_IMAGE_NAME}"
287288
image_exists = cmd_output(cmd)
288289

@@ -299,11 +300,21 @@ def prepare_dependency_archive(directory: str, docker_network: str) -> None:
299300
shutil.copy("build_native_dependencies.sh", temp_dir)
300301
cmd = docker_run_cmd(docker_network, temp_dir)
301302
cmd_output(cmd)
302-
archives_temp_path = os.path.join(temp_dir, DEPENDENCIES_ARCHIVE_FULL_NAME)
303-
os.makedirs(os.path.dirname(DEPENDENCIES_ARCHIVE_PATH), exist_ok=True)
304-
shutil.copy(archives_temp_path, DEPENDENCIES_ARCHIVE_PATH)
305303

306-
logger.info(f"Dependencies archived to {DEPENDENCIES_ARCHIVE_PATH}")
304+
if package_type == "function":
305+
source_py_files = os.path.join(temp_dir, "py-files")
306+
os.makedirs(os.path.dirname(PY_FILES_PATH), exist_ok=True)
307+
if os.path.exists(PY_FILES_PATH):
308+
shutil.rmtree(PY_FILES_PATH)
309+
shutil.copytree(source_py_files, PY_FILES_PATH)
310+
logger.info(f"Dependencies copied to {PY_FILES_PATH}")
311+
else:
312+
archives_temp_path = os.path.join(
313+
temp_dir, DEPENDENCIES_ARCHIVE_FULL_NAME
314+
)
315+
os.makedirs(os.path.dirname(DEPENDENCIES_ARCHIVE_PATH), exist_ok=True)
316+
shutil.copy(archives_temp_path, DEPENDENCIES_ARCHIVE_PATH)
317+
logger.info(f"Dependencies archived to {DEPENDENCIES_ARCHIVE_PATH}")
307318

308319

309320
def docker_build_cmd(network: str) -> str:
@@ -511,16 +522,28 @@ def upload_zip(file_upload_url: str) -> None:
511522
response.raise_for_status()
512523

513524

525+
def _get_package_type_for_directory(directory: str) -> str:
526+
"""Resolve package type (script/function) for the given payload directory."""
527+
try:
528+
entrypoint_path = os.path.join(os.path.abspath(directory), "entrypoint.py")
529+
base_directory = find_base_directory(entrypoint_path)
530+
return get_package_type(base_directory)
531+
except (ValueError, FileNotFoundError):
532+
return "script"
533+
534+
514535
def zip(
515536
directory: str,
516537
docker_network: str,
517538
):
518539
# Create a zip file excluding .DS_Store files
519540
import zipfile
520541

542+
package_type = _get_package_type_for_directory(directory)
543+
521544
# prepare payload only if requirements.txt is non-empty
522545
if has_nonempty_requirements_file(directory):
523-
prepare_dependency_archive(directory, docker_network)
546+
prepare_dependency_archive(directory, docker_network, package_type)
524547
else:
525548
logger.info(
526549
f"Skipping dependency archive: requirements.txt is missing or empty "
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
# Description: build native dependencies
4+
# Description: build native dependencies for function (unpacked pip install to py-files)
55

66
python3.11 -m venv --copies .venv
7-
source .venv/bin/activate
8-
pip install -r requirements.txt
9-
venv-pack -o native_dependencies.tar.gz -f
7+
source .venv/bin/activate
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Packages required for the custom code
1+
# Packages required for the custom code

tests/test_deploy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def test_zip_with_requirements(
611611
zip("/test/dir", "default")
612612

613613
mock_has_requirements.assert_called_once_with("/test/dir")
614-
mock_prepare.assert_called_once_with("/test/dir", "default")
614+
mock_prepare.assert_called_once_with("/test/dir", "default", "script")
615615
mock_zipfile.assert_called_once_with(
616616
"deployment.zip", "w", zipfile.ZIP_DEFLATED
617617
)

tests/test_scan.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import pytest
99

1010
from datacustomcode.scan import (
11-
SDK_CONFIG_DIR,
1211
DataAccessLayerCalls,
12+
SDK_CONFIG_DIR,
13+
SDK_CONFIG_FILE,
1314
dc_config_json_from_file,
15+
get_sdk_config_path,
1416
scan_file,
1517
scan_file_for_imports,
1618
update_config,
@@ -40,7 +42,7 @@ def create_sdk_config(base_directory: str, package_type: str = "script") -> str:
4042
"""
4143
sdk_config = {"type": package_type}
4244
write_sdk_config(base_directory, sdk_config)
43-
return os.path.join(base_directory, SDK_CONFIG_DIR, "config.json")
45+
return os.path.join(base_directory, SDK_CONFIG_DIR, SDK_CONFIG_FILE)
4446

4547

4648
class TestClientMethodVisitor:

0 commit comments

Comments
 (0)