4
4
5
5
from datetime import datetime
6
6
7
- from six .moves import shlex_quote
8
-
9
- from pyinfra .api .connectors .util import escape_unix_path
7
+ from pyinfra .api .command import make_formatted_string_command , QuoteString
10
8
from pyinfra .api .facts import FactBase
11
9
from pyinfra .api .util import try_int
12
10
@@ -66,12 +64,11 @@ class File(FactBase):
66
64
test_flag = '-e'
67
65
68
66
def command (self , path ):
69
- path = escape_unix_path (path )
70
- return (
71
- '! test {test_flag} {path} || ' # only stat if the file exists
72
- '({linux_stat_command} {path} 2> /dev/null || {bsd_stat_command} {path})'
73
- ).format (
74
- path = path ,
67
+ return make_formatted_string_command ((
68
+ '! test {test_flag} {0} || ' # only stat if the file exists
69
+ '( {linux_stat_command} {0} 2> /dev/null || {bsd_stat_command} {0} )'
70
+ ),
71
+ QuoteString (path ),
75
72
test_flag = self .test_flag ,
76
73
linux_stat_command = LINUX_STAT_COMMAND ,
77
74
bsd_stat_command = BSD_STAT_COMMAND ,
@@ -143,17 +140,16 @@ class Sha1File(FactBase):
143
140
]
144
141
145
142
def command (self , name ):
146
- name = escape_unix_path (name )
147
143
self .name = name
148
- return (
149
- 'test -e {0} && ('
150
- 'sha1sum {0} 2> /dev/null || shasum {0} 2> /dev/null || sha1 {0}'
144
+ return make_formatted_string_command ( (
145
+ 'test -e {0} && ( '
146
+ 'sha1sum {0} 2> /dev/null || shasum {0} 2> /dev/null || sha1 {0} '
151
147
') || true'
152
- ). format (name )
148
+ ), QuoteString (name ) )
153
149
154
150
def process (self , output ):
155
151
for regex in self ._regexes :
156
- regex = regex % self .name
152
+ regex = regex % re . escape ( self .name )
157
153
matches = re .match (regex , output [0 ])
158
154
if matches :
159
155
return matches .group (1 )
@@ -170,19 +166,18 @@ class Sha256File(FactBase):
170
166
]
171
167
172
168
def command (self , name ):
173
- name = escape_unix_path (name )
174
169
self .name = name
175
- return (
176
- 'test -e {0} && ('
170
+ return make_formatted_string_command ( (
171
+ 'test -e {0} && ( '
177
172
'sha256sum {0} 2> /dev/null '
178
173
'|| shasum -a 256 {0} 2> /dev/null '
179
- '|| sha256 {0}'
174
+ '|| sha256 {0} '
180
175
') || true'
181
- ). format (name )
176
+ ), QuoteString (name ) )
182
177
183
178
def process (self , output ):
184
179
for regex in self ._regexes :
185
- regex = regex % self .name
180
+ regex = regex % re . escape ( self .name )
186
181
matches = re .match (regex , output [0 ])
187
182
if matches :
188
183
return matches .group (1 )
@@ -199,13 +194,15 @@ class Md5File(FactBase):
199
194
]
200
195
201
196
def command (self , name ):
202
- name = escape_unix_path (name )
203
197
self .name = name
204
- return 'test -e {0} && (md5sum {0} 2> /dev/null || md5 {0}) || true' .format (name )
198
+ return make_formatted_string_command (
199
+ 'test -e {0} && ( md5sum {0} 2> /dev/null || md5 {0} ) || true' ,
200
+ QuoteString (name ),
201
+ )
205
202
206
203
def process (self , output ):
207
204
for regex in self ._regexes :
208
- regex = regex % self .name
205
+ regex = regex % re . escape ( self .name )
209
206
matches = re .match (regex , output [0 ])
210
207
if matches :
211
208
return matches .group (1 )
@@ -218,21 +215,20 @@ class FindInFile(FactBase):
218
215
'''
219
216
220
217
def command (self , name , pattern ):
221
- name = escape_unix_path (name )
222
- pattern = shlex_quote (pattern )
218
+ self .exists_name = '__pyinfra_exists_{0}' .format (name )
223
219
224
- self . name = name
220
+ # TODO: remove special charts from __pyinfra_exists thing
225
221
226
- return (
222
+ return make_formatted_string_command ( (
227
223
'grep -e {0} {1} 2> /dev/null || '
228
- '(find {1} -type f > /dev/null && echo "__pyinfra_exists_{1}" || true)'
229
- ). format (pattern , name ). strip ( )
224
+ '( find {1} -type f > /dev/null && echo {2} || true )'
225
+ ), QuoteString (pattern ), QuoteString ( name ), QuoteString ( self . exists_name ) )
230
226
231
227
def process (self , output ):
232
228
# If output is the special string: no matches, so return an empty list;
233
229
# this allows us to differentiate between no matches in an existing file
234
230
# or a file not existing.
235
- if output and output [0 ] == '__pyinfra_exists_{0}' . format ( self .name ) :
231
+ if output and output [0 ] == self .exists_name :
236
232
return []
237
233
238
234
return output
@@ -245,7 +241,10 @@ class FindFiles(FactBase):
245
241
246
242
@staticmethod
247
243
def command (name ):
248
- return 'find {0} -type f || true' .format (escape_unix_path (name ))
244
+ return make_formatted_string_command (
245
+ 'find {0} -type f || true' ,
246
+ QuoteString (name ),
247
+ )
249
248
250
249
@staticmethod
251
250
def process (output ):
@@ -259,7 +258,10 @@ class FindLinks(FindFiles):
259
258
260
259
@staticmethod
261
260
def command (name ):
262
- return 'find {0} -type l || true' .format (escape_unix_path (name ))
261
+ return make_formatted_string_command (
262
+ 'find {0} -type l || true' ,
263
+ QuoteString (name ),
264
+ )
263
265
264
266
265
267
class FindDirectories (FindFiles ):
@@ -269,4 +271,7 @@ class FindDirectories(FindFiles):
269
271
270
272
@staticmethod
271
273
def command (name ):
272
- return 'find {0} -type d || true' .format (escape_unix_path (name ))
274
+ return make_formatted_string_command (
275
+ 'find {0} -type d || true' ,
276
+ QuoteString (name ),
277
+ )
0 commit comments