Skip to content

Commit

Permalink
Fix tilde expansion for LocalConnector.put_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhammes committed Nov 2, 2024
1 parent 9ce7ac4 commit 8cd8485
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pyinfra/connectors/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Tuple

import click
import shlex
from typing_extensions import Unpack

from pyinfra import logger
Expand Down Expand Up @@ -118,6 +119,12 @@ def put_file(
bool: Indicating success or failure
"""

if remote_filename.startswith("~/"):
# Do not quote leading tilde to ensure that it gets properly expanded by the shell
remote_filename = f"~/{shlex.quote(remote_filename[2:])}"
else:
remote_filename = QuoteString(remote_filename)

_, temp_filename = mkstemp()

try:
Expand All @@ -133,7 +140,7 @@ def put_file(

# Copy the file using `cp` such that we support sudo/su
status, output = self.run_shell_command(
StringCommand("cp", temp_filename, QuoteString(remote_filename)),
StringCommand("cp", temp_filename, remote_filename),
print_output=print_output,
print_input=print_input,
**arguments,
Expand All @@ -146,6 +153,7 @@ def put_file(

if print_output:
click.echo(
# TODO: Check if the modification of remote_filename affects the output
"{0}file copied: {1}".format(self.host.print_prefix, remote_filename),
err=True,
)
Expand Down

0 comments on commit 8cd8485

Please sign in to comment.