Skip to content

Commit

Permalink
Adding postCopyAction auto dir creation with file/dir existence check…
Browse files Browse the repository at this point in the history
…. Added test as pca_3.
  • Loading branch information
atroiano-glue authored Nov 8, 2024
1 parent 2c38b82 commit 4ded1d9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/opentaskpy/remotehandlers/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,23 +482,34 @@ def handle_post_copy_action(self, files: list[str]) -> int:
# Use the SFTP client, and check that the destination directory exists
move_dir = os.path.dirname(self.spec["postCopyAction"]["destination"])

try:
stat_result = self.sftp_client.stat(move_dir)
# If it exists, then we need to ensure its a directory and not just a file
if not stat.S_ISDIR(stat_result.st_mode): # type: ignore[arg-type]
current_dir = ""
# If it exists, then we need to ensure its a directory and not just a file
for dir_part in move_dir.split("/"):
if not dir_part:
continue
current_dir += f"/{dir_part}"
try:
self.sftp_client.stat(current_dir)
except FileNotFoundError:
self.sftp_client.mkdir(current_dir)
self.logger.error(f"Creating directory {current_dir}")

try:
file_attr = self.sftp_client.stat(current_dir)
if stat.S_ISREG(file_attr.st_mode): # type: ignore[arg-type]
raise OSError(f"A file with name {current_dir} already exists.")
if stat.S_ISDIR(file_attr.st_mode): # type: ignore[arg-type]
pass
else:
raise OSError("Found unrecognized file in postcopy directory")

except OSError as os_e:
self.logger.error(
f"[{self.spec['hostname']}] Destination directory {move_dir} is"
" not a directory on source host"
f"[{self.spec['hostname']}] Failed attempted creation of Destination directory {move_dir} FAILED. {os_e}"
)
return 1
except OSError:
self.logger.error(
f"[{self.spec['hostname']}] Destination directory {move_dir} does"
" not exist on source host"
)
return 1

# Loop through the files and move them
# Loop through the files and move them

for file in files:
try:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_taskhandler_transfer_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@
],
}

sftp_pca_move_task_definition_3 = {
"type": "transfer",
"source": {
"hostname": "172.16.0.21",
"directory": "/home/application/testFiles/src",
"fileRegex": "pca_move_nested_3\\.txt",
"protocol": {"name": "sftp", "credentials": {"username": "application"}},
"postCopyAction": {
"action": "move",
"destination": "/home/application/testFiles/src/nested/",
},
},
"destination": [
{
"hostname": "172.16.0.22",
"directory": "/home/application/testFiles/dest",
"protocol": {"name": "sftp", "credentials": {"username": "application"}},
},
],
}

sftp_pca_delete_task_definition = {
"type": "transfer",
"source": {
Expand Down Expand Up @@ -1447,3 +1468,36 @@ def test_sftp_filewatch_counts_error(root_dir, setup_sftp_keys):
# Run the transfer and expect a RemoteFileNotFoundError exception
with pytest.raises(exceptions.RemoteFileNotFoundError):
transfer_obj.run()


def test_pca_move_nested(root_dir, setup_sftp_keys):
# Empty the PCA archive directory
for file in os.listdir(f"{root_dir}/testFiles/sftp_1/archive"):
os.remove(f"{root_dir}/testFiles/sftp_1/archive/{file}")

# Create the test file
fs.create_files(
[
{
f"{root_dir}/testFiles/sftp_1/src/pca_move_nested_3.txt": {
"content": "test1234"
}
}
]
)
# Create a transfer object
transfer_obj = transfer.Transfer(
None, "scp-pca-move-3", sftp_pca_move_task_definition_3
)

# Run the transfer and expect a true status
assert transfer_obj.run()
# Check the destination file exists
assert os.path.exists(f"{root_dir}/testFiles/sftp_2/dest/pca_move_nested_3.txt")
# Check the source file no longer exists
assert not os.path.exists(f"{root_dir}/testFiles/sftp_1/src/pca_move_nested_3.txt")

# Check the source file has been created in nested folder
assert os.path.exists(
f"{root_dir}/testFiles/sftp_1/src/nested/pca_move_nested_3.txt"
)

0 comments on commit 4ded1d9

Please sign in to comment.