Skip to content

Commit

Permalink
remove usage of _remote_temp_file()
Browse files Browse the repository at this point in the history
This commit removes the usage of `_remote_temp_file()`
in favor of generating an upload file with a randomized
extension in the final destination.
  • Loading branch information
christian-monch committed Mar 26, 2024
1 parent 129cc94 commit 4a3252b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 94 deletions.
78 changes: 0 additions & 78 deletions datalad_ria/annexremotes/demo.py

This file was deleted.

40 changes: 24 additions & 16 deletions datalad_ria/annexremotes/ssh_riahandler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import random
import traceback
from contextlib import contextmanager
from functools import partial
Expand Down Expand Up @@ -48,6 +49,8 @@

layout_version = '1'

random.seed()


class OperationBase:
def __init__(self, args: tuple):
Expand Down Expand Up @@ -376,24 +379,29 @@ def transfer_store(self, key: str, local_file: str) -> None:
if self._locked_checkpresent(key):
return

with self._remote_tempfile() as remote_temp_file:
# Upload the local file to the temporary file.
self.ssh_thread.upload(
Path(local_file),
remote_temp_file,
self.progress_handler
)
# Ensure that the remote path exists.
final_path = self.get_ria_key_path(key)
self.ssh_thread.execute(f'mkdir -p {final_path.parent}')

# Ensure that the remote path exists.
final_path = self.get_ria_key_path(key)
self.ssh_thread.execute(f'mkdir -p {final_path.parent}')
# Create a temporary file name
remote_temp_file = self.get_ria_key_path(
key,
extension=f'transfer-{random.randint(100000000, 999999999)}'
)

# Ensure that the remote path is writable.
with self._ensure_writable(final_path.parent):
# Move the temporary file to its final destination.
self.ssh_thread.execute(
f'mv -f {remote_temp_file} {final_path}'
)
# Upload the local file to the temporary file.
self.ssh_thread.upload(
Path(local_file),
remote_temp_file,
self.progress_handler
)

# Ensure that the remote path is writable.
with self._ensure_writable(final_path.parent):
# Move the temporary file to its final destination.
self.ssh_thread.execute(
f'mv -f {remote_temp_file} {final_path}'
)

def transfer_retrieve(self, key: str, local_file: str) -> None:
key = _sanitize_key(key)
Expand Down

0 comments on commit 4a3252b

Please sign in to comment.