Skip to content

Allow specifying custom tempdir for files.download #1366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pyinfra/api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,18 @@ def _get_temp_directory(self):

return temp_directory

def get_temp_filename(self, hash_key: Optional[str] = None, hash_filename: bool = True):
def get_temp_filename(
self,
hash_key: Optional[str] = None,
hash_filename: bool = True,
*,
temp_directory: Optional[str] = None,
):
"""
Generate a temporary filename for this deploy.
"""

temp_directory = self._get_temp_directory()
temp_directory = temp_directory or self._get_temp_directory()

if not hash_key:
hash_key = str(uuid4())
Expand Down
6 changes: 5 additions & 1 deletion pyinfra/operations/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def download(
headers: dict[str, str] | None = None,
insecure=False,
proxy: str | None = None,
temp_dir: str | Path | None = None,
):
"""
Download files from remote locations using ``curl`` or ``wget``.
Expand All @@ -93,6 +94,7 @@ def download(
+ headers: optional dictionary of headers to set for the HTTP request
+ insecure: disable SSL verification for the HTTP request
+ proxy: simple HTTP proxy through which we can download files, form `http://<yourproxy>:<port>`
+ temp_dir: use this custom temporary directory during the download

**Example:**

Expand Down Expand Up @@ -148,7 +150,9 @@ def download(

# If we download, always do user/group/mode as SSH user may be different
if download:
temp_file = host.get_temp_filename(dest)
temp_file = host.get_temp_filename(
dest, temp_directory=str(temp_dir) if temp_dir is not None else None
)

curl_args: list[Union[str, StringCommand]] = ["-sSLf"]
wget_args: list[Union[str, StringCommand]] = ["-q"]
Expand Down
2 changes: 1 addition & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def print_prefix(self):
def noop(self, description):
self.noop_description = description

def get_temp_filename(*args):
def get_temp_filename(*args, **kwargs):
return "_tempfile_"

@staticmethod
Expand Down