Skip to content

Commit

Permalink
fix: adjust logic for workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mikita-sakalouski committed Sep 15, 2024
1 parent 45c115f commit b8e700b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 12 deletions.
23 changes: 21 additions & 2 deletions brickflow/codegen/databricks_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,23 +426,42 @@ def adjust_source(self) -> str:
return "WORKSPACE" if self.env == BrickflowDefaultEnvs.LOCAL.value else "GIT"

def adjust_file_path(self, file_path: str) -> str:
"""
Adjusts the given file path based on the environment and project settings.
If the environment is local and the project has a defined bundle base path and bundle object name,
the method constructs a new file path by appending the local bundle path to the given file path.
Args:
file_path (str): The original file path to be adjusted.
Returns:
str: The adjusted file path.
"""
if (
self.env == BrickflowDefaultEnvs.LOCAL.value
and self.project.bundle_base_path is not None
and self.project.bundle_obj_name is not None
):
bundle_files_local_path = "/".join(
[
"Workspace",
self.project.bundle_base_path,
self.project.bundle_obj_name,
self.project.name,
str(BrickflowDefaultEnvs.LOCAL.value),
"files",
]
)
).replace("//", "/")

# Finds the start position of the project name in the given file path and calculates the cut position.
# - `file_path.find(self.project.name)`: Finds the start index of the project name in the file path.
# - `+ len(self.project.name) + 1`: Moves the start position to the character after the project name.
# - Adjusts the file path by appending the local bundle path to the cut file path.
cut_file_path = file_path[
file_path.find(self.project.name) + len(self.project.name) + 1 :
]
file_path = (
bundle_files_local_path + file_path
if file_path.startswith("/")
else f"{bundle_files_local_path}/{file_path}"
else f"{bundle_files_local_path}/{cut_file_path}"
)
return file_path

Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/expected_bundles/dev_bundle_monorepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ targets:
max_retries: null
min_retry_interval_millis: null
spark_python_task:
python_file: path/to/python/file.py
python_file: ./products/test-project/spark/python/src/run_task.py
parameters: ["--param1", "World!"]
source: GIT
retry_on_timeout: null
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/expected_bundles/dev_bundle_polyrepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ targets:
max_retries: null
min_retry_interval_millis: null
spark_python_task:
python_file: path/to/python/file.py
python_file: ./products/test-project/spark/python/src/run_task.py
parameters: ["--param1", "World!"]
source: GIT
retry_on_timeout: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ targets:
max_retries: null
min_retry_interval_millis: null
spark_python_task:
python_file: path/to/python/file.py
python_file: ./products/test-project/spark/python/src/run_task.py
parameters: ["--param1", "World!"]
source: GIT
retry_on_timeout: null
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/expected_bundles/local_bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
"min_retry_interval_millis": null
"retry_on_timeout": null
"spark_python_task":
"python_file": "/Users/${workspace.current_user.userName}/.brickflow_bundles/test-project/local/path/to/python/file.py"
"python_file": "Workspace/Users/${workspace.current_user.userName}/.brickflow_bundles/test-project/local/files/spark/python/src/run_task.py"
"source": "WORKSPACE"
"parameters":
- "--param1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ targets:
max_retries: null
min_retry_interval_millis: null
spark_python_task:
python_file: /Users/${workspace.current_user.userName}/.brickflow_bundles/test-project/local/path/to/python/file.py
python_file: Workspace/Users/${workspace.current_user.userName}/.brickflow_bundles/test-project/local/files/spark/python/src/run_task.py
parameters: ["--param1", "World!"]
source: WORKSPACE
retry_on_timeout: null
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/sample_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def spark_jar_task_a():
)
def spark_python_task_a():
return SparkPythonTask(
python_file="path/to/python/file.py",
python_file="./products/test-project/spark/python/src/run_task.py",
source="GIT",
parameters=["--param1", "World!"],
) # type: ignore
Expand Down
8 changes: 4 additions & 4 deletions tests/engine/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,19 @@ def test_without_params_spark_jar(self):

def test_init_spark_python(self):
task = SparkPythonTask(
python_file="path/to/python/file.py",
python_file="./products/test-project/path/to/python/file.py",
source="GIT",
parameters=["--param1", "World!"],
)
assert task.python_file == "path/to/python/file.py"
assert task.python_file == "./products/test-project/path/to/python/file.py"
assert task.source == "GIT"
assert task.parameters == ["--param1", "World!"]

def test_without_params_spark_python(self):
task = SparkPythonTask(
python_file="path/to/python/file.py",
python_file="./products/test-project/path/to/python/file.py",
)
assert task.python_file == "path/to/python/file.py"
assert task.python_file == "./products/test-project/path/to/python/file.py"
assert task.source is None
assert task.parameters is None

Expand Down

0 comments on commit b8e700b

Please sign in to comment.