Skip to content
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

Fix tilde expansion for LocalConnector.put_file() #1236

Draft
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Draft
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: 9 additions & 1 deletion pyinfra/connectors/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shlex
from shutil import which
from tempfile import mkstemp
from typing import TYPE_CHECKING, Tuple
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
38 changes: 38 additions & 0 deletions tests/test_connectors/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,44 @@ def test_put_file_with_spaces(self):
stdin=PIPE,
)

def test_put_file_tilde_expansion(self):
inventory = make_inventory(hosts=("@local",))
State(inventory, Config())

host = inventory.get_host("@local")

fake_process = MagicMock(returncode=0)
self.fake_popen_mock.return_value = fake_process

host.put_file("not-a-file", "~/file-in-home-directory", print_output=True)

self.fake_popen_mock.assert_called_with(
"sh -c 'cp __tempfile__ ~/file-in-home-directory'",
shell=True,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)

def test_put_file_tilde_expansion_with_spaces(self):
inventory = make_inventory(hosts=("@local",))
State(inventory, Config())

host = inventory.get_host("@local")

fake_process = MagicMock(returncode=0)
self.fake_popen_mock.return_value = fake_process

host.put_file("not-a-file", "~/not another file with spaces", print_output=True)

self.fake_popen_mock.assert_called_with(
"sh -c 'cp __tempfile__ ~/'\"'\"'not another file with spaces'\"'\"''",
shell=True,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)

def test_put_file_error(self):
inventory = make_inventory(hosts=("@local",))
State(inventory, Config())
Expand Down
Loading