44
55from datetime import datetime
66
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
108from pyinfra .api .facts import FactBase
119from pyinfra .api .util import try_int
1210
@@ -66,12 +64,11 @@ class File(FactBase):
6664 test_flag = '-e'
6765
6866 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 ),
7572 test_flag = self .test_flag ,
7673 linux_stat_command = LINUX_STAT_COMMAND ,
7774 bsd_stat_command = BSD_STAT_COMMAND ,
@@ -143,17 +140,16 @@ class Sha1File(FactBase):
143140 ]
144141
145142 def command (self , name ):
146- name = escape_unix_path (name )
147143 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} '
151147 ') || true'
152- ). format (name )
148+ ), QuoteString (name ) )
153149
154150 def process (self , output ):
155151 for regex in self ._regexes :
156- regex = regex % self .name
152+ regex = regex % re . escape ( self .name )
157153 matches = re .match (regex , output [0 ])
158154 if matches :
159155 return matches .group (1 )
@@ -170,19 +166,18 @@ class Sha256File(FactBase):
170166 ]
171167
172168 def command (self , name ):
173- name = escape_unix_path (name )
174169 self .name = name
175- return (
176- 'test -e {0} && ('
170+ return make_formatted_string_command ( (
171+ 'test -e {0} && ( '
177172 'sha256sum {0} 2> /dev/null '
178173 '|| shasum -a 256 {0} 2> /dev/null '
179- '|| sha256 {0}'
174+ '|| sha256 {0} '
180175 ') || true'
181- ). format (name )
176+ ), QuoteString (name ) )
182177
183178 def process (self , output ):
184179 for regex in self ._regexes :
185- regex = regex % self .name
180+ regex = regex % re . escape ( self .name )
186181 matches = re .match (regex , output [0 ])
187182 if matches :
188183 return matches .group (1 )
@@ -199,13 +194,15 @@ class Md5File(FactBase):
199194 ]
200195
201196 def command (self , name ):
202- name = escape_unix_path (name )
203197 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+ )
205202
206203 def process (self , output ):
207204 for regex in self ._regexes :
208- regex = regex % self .name
205+ regex = regex % re . escape ( self .name )
209206 matches = re .match (regex , output [0 ])
210207 if matches :
211208 return matches .group (1 )
@@ -218,21 +215,20 @@ class FindInFile(FactBase):
218215 '''
219216
220217 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 )
223219
224- self . name = name
220+ # TODO: remove special charts from __pyinfra_exists thing
225221
226- return (
222+ return make_formatted_string_command ( (
227223 '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 ) )
230226
231227 def process (self , output ):
232228 # If output is the special string: no matches, so return an empty list;
233229 # this allows us to differentiate between no matches in an existing file
234230 # 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 :
236232 return []
237233
238234 return output
@@ -245,7 +241,10 @@ class FindFiles(FactBase):
245241
246242 @staticmethod
247243 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+ )
249248
250249 @staticmethod
251250 def process (output ):
@@ -259,7 +258,10 @@ class FindLinks(FindFiles):
259258
260259 @staticmethod
261260 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+ )
263265
264266
265267class FindDirectories (FindFiles ):
@@ -269,4 +271,7 @@ class FindDirectories(FindFiles):
269271
270272 @staticmethod
271273 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