@@ -296,14 +296,20 @@ class DockerBuildCommand(BuildCommand):
296
296
Build command to execute in docker container
297
297
"""
298
298
299
- def run (self ):
299
+ def __init__ (self , * args , escape_command = True , ** kwargs ):
300
300
"""
301
- Execute command in existing Docker container .
301
+ Override default to extend behavior .
302
302
303
- :param cmd_input: input to pass to command in STDIN
304
- :type cmd_input: str
305
- :param combine_output: combine STDERR into STDOUT
303
+ :param escape_command: whether escape special chars the command before
304
+ executing it in the container. This should only be disabled on
305
+ trusted or internal commands.
306
+ :type escape_command: bool
306
307
"""
308
+ self .escape_command = escape_command
309
+ super (DockerBuildCommand , self ).__init__ (* args , ** kwargs )
310
+
311
+ def run (self ):
312
+ """Execute command in existing Docker container."""
307
313
log .info (
308
314
"Running in container %s: '%s' [%s]" ,
309
315
self .build_env .container_id ,
@@ -316,7 +322,7 @@ def run(self):
316
322
try :
317
323
exec_cmd = client .exec_create (
318
324
container = self .build_env .container_id ,
319
- cmd = self .get_wrapped_command (),
325
+ cmd = self .get_wrapped_command (self . escape_command ),
320
326
stdout = True ,
321
327
stderr = True ,
322
328
)
@@ -352,13 +358,15 @@ def run(self):
352
358
353
359
def get_wrapped_command (self ):
354
360
"""
355
- Escape special bash characters in command to wrap in shell .
361
+ Wrap command in a shell and optionally escape special bash characters .
356
362
357
363
In order to set the current working path inside a docker container, we
358
- need to wrap the command in a shell call manually. Some characters will
359
- be interpreted as shell characters without escaping, such as: ``pip
360
- install requests<0.8``. This escapes a good majority of those
361
- characters.
364
+ need to wrap the command in a shell call manually.
365
+
366
+ Some characters will be interpreted as shell characters without
367
+ escaping, such as: ``pip install requests<0.8``. When passing
368
+ ``escape_command=True`` in the init method this escapes a good majority
369
+ of those characters.
362
370
"""
363
371
bash_escape_re = re .compile (
364
372
r"([\t\ \!\"\#\$\&\'\(\)\*\:\;\<\>\?\@"
@@ -367,16 +375,18 @@ def get_wrapped_command(self):
367
375
prefix = ''
368
376
if self .bin_path :
369
377
prefix += 'PATH={}:$PATH ' .format (self .bin_path )
378
+
379
+ command = (
380
+ ' ' .join ([
381
+ bash_escape_re .sub (r'\\\1' , part ) if self .escape_command else part
382
+ for part in self .command
383
+ ])
384
+ )
370
385
return (
371
386
"/bin/sh -c 'cd {cwd} && {prefix}{cmd}'" .format (
372
387
cwd = self .cwd ,
373
388
prefix = prefix ,
374
- cmd = (
375
- ' ' .join ([
376
- bash_escape_re .sub (r'\\\1' , part )
377
- for part in self .command
378
- ])
379
- ),
389
+ cmd = command ,
380
390
)
381
391
)
382
392
0 commit comments