Skip to content

Commit 0a0cdb7

Browse files
committed
Update remaining files operations to properly quote filenames.
1 parent 3e1b63b commit 0a0cdb7

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

pyinfra/operations/files.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
operation,
2424
OperationError,
2525
OperationTypeError,
26+
QuoteString,
2627
RsyncCommand,
2728
StringCommand,
2829
)
2930
from pyinfra.api.command import make_formatted_string_command
30-
from pyinfra.api.connectors.util import escape_unix_path
3131
from pyinfra.api.util import (
3232
get_file_sha1,
3333
get_path_permissions_mode,
@@ -363,8 +363,6 @@ def replace(
363363
)
364364
'''
365365

366-
path = escape_unix_path(path)
367-
368366
existing_lines = host.fact.find_in_file(path, match)
369367

370368
# Only do the replacement if the file does not exist (it may be created earlier)
@@ -592,8 +590,6 @@ def get(
592590
)
593591
'''
594592

595-
src = escape_unix_path(src)
596-
597593
if add_deploy_dir and state.deploy_dir:
598594
dest = os_path.join(state.deploy_dir, dest)
599595

@@ -667,8 +663,6 @@ def put(
667663
)
668664
'''
669665

670-
dest = escape_unix_path(dest)
671-
672666
# Upload IO objects as-is
673667
if hasattr(src, 'read'):
674668
local_file = src
@@ -816,8 +810,6 @@ def template(
816810
{% endfor %}
817811
'''
818812

819-
dest = escape_unix_path(dest)
820-
821813
if state.deploy_dir:
822814
src = os_path.join(state.deploy_dir, src)
823815

@@ -934,19 +926,18 @@ def link(
934926
if present and not target:
935927
raise OperationError('If present is True target must be provided')
936928

937-
path = escape_unix_path(path)
938929
info = host.fact.link(path)
939930

940931
# Not a link?
941932
if info is False:
942933
raise OperationError('{0} exists and is not a link'.format(path))
943934

944-
add_cmd = 'ln{0} {1} {2}'.format(
945-
' -s' if symbolic else '',
946-
target, path,
947-
)
935+
add_args = ['ln']
936+
if symbolic:
937+
add_args.append('-s')
948938

949-
remove_cmd = 'rm -f {0}'.format(path)
939+
add_cmd = StringCommand(' '.join(add_args), QuoteString(target), QuoteString(path))
940+
remove_cmd = StringCommand('rm', '-f', QuoteString(path))
950941

951942
# No link and we want it
952943
if not assume_present and info is None and present:
@@ -1060,7 +1051,6 @@ def file(
10601051
_validate_path(path)
10611052

10621053
mode = ensure_mode_int(mode)
1063-
path = escape_unix_path(path)
10641054
info = host.fact.file(path)
10651055

10661056
# Not a file?!
@@ -1072,7 +1062,7 @@ def file(
10721062
if create_remote_dir:
10731063
yield _create_remote_dir(state, host, path, user, group)
10741064

1075-
yield 'touch {0}'.format(path)
1065+
yield StringCommand('touch', QuoteString(path))
10761066

10771067
if mode:
10781068
yield chmod(path, mode)
@@ -1087,7 +1077,7 @@ def file(
10871077

10881078
# It exists and we don't want it
10891079
elif (assume_present or info) and not present:
1090-
yield 'rm -f {0}'.format(path)
1080+
yield StringCommand('rm', '-f', QuoteString(path))
10911081
host.fact._delete('file', args=(path,))
10921082

10931083
# It exists & we want to ensure its state
@@ -1100,7 +1090,7 @@ def file(
11001090

11011091
if touch:
11021092
changed = True
1103-
yield 'touch {0}'.format(path)
1093+
yield StringCommand('touch', QuoteString(path))
11041094

11051095
# Check mode
11061096
if mode and (not info or info['mode'] != mode):
@@ -1176,7 +1166,6 @@ def directory(
11761166
_validate_path(path)
11771167

11781168
mode = ensure_mode_int(mode)
1179-
path = escape_unix_path(path)
11801169
info = host.fact.directory(path)
11811170

11821171
# Not a directory?!
@@ -1188,7 +1177,7 @@ def directory(
11881177

11891178
# Doesn't exist & we want it
11901179
if not assume_present and info is None and present:
1191-
yield 'mkdir -p {0}'.format(path)
1180+
yield StringCommand('mkdir', '-p', QuoteString(path))
11921181
if mode:
11931182
yield chmod(path, mode, recursive=recursive)
11941183
if user or group:
@@ -1202,7 +1191,7 @@ def directory(
12021191

12031192
# It exists and we don't want it
12041193
elif (assume_present or info) and not present:
1205-
yield 'rm -rf {0}'.format(path)
1194+
yield StringCommand('rm', '-rf', QuoteString(path))
12061195
host.fact._delete('directory', args=(path,))
12071196

12081197
# It exists & we want to ensure its state

pyinfra/operations/util/files.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ def sed_replace(
7676

7777

7878
def chmod(target, mode, recursive=False):
79-
return 'chmod {0}{1} {2}'.format(('-R ' if recursive else ''), mode, target)
79+
args = ['chmod']
80+
if recursive:
81+
args.append('-R')
82+
83+
args.append('{0}'.format(mode))
84+
85+
return StringCommand(' '.join(args), QuoteString(target))
8086

8187

8288
def chown(target, user, group=None, recursive=False, dereference=True):
@@ -93,10 +99,11 @@ def chown(target, user, group=None, recursive=False, dereference=True):
9399
command = 'chgrp'
94100
user_group = group
95101

96-
return '{0}{1}{2} {3} {4}'.format(
97-
command,
98-
' -R' if recursive else '',
99-
' -h' if not dereference else '',
100-
user_group,
101-
target,
102-
)
102+
args = [command]
103+
if recursive:
104+
args.append('-R')
105+
106+
if not dereference:
107+
args.append('-h')
108+
109+
return StringCommand(' '.join(args), user_group, QuoteString(target))

tests/operations/files.get/path_with_spaces.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"args": ["/home/some file.txt", "somefile.txt"],
33
"facts": {
44
"file": {
5-
"/home/some\\ file.txt": null
5+
"/home/some file.txt": null
66
}
77
},
88
"commands": [
9-
["download", "/home/some\\ file.txt", "/somefile.txt"]
9+
["download", "/home/some file.txt", "/somefile.txt"]
1010
],
1111
"idempotent": false
1212
}

tests/operations/files.put/path_with_spaces.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
},
1212
"commands": [
13-
["upload", "/somefile.txt", "/home/some\\ file.txt"]
13+
["upload", "/somefile.txt", "/home/some file.txt"]
1414
],
1515
"idempotent": false
1616
}

0 commit comments

Comments
 (0)