Skip to content

Commit

Permalink
Try to escape command lines in logfiles
Browse files Browse the repository at this point in the history
For example, an echo now appears as
  echo "hello world, won't you come out?"
Fixes #815
  • Loading branch information
djmitche committed Dec 12, 2010
1 parent e77b4b3 commit 6c9af57
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions slave/buildslave/runprocess.py
Expand Up @@ -31,6 +31,28 @@
from buildslave import util
from buildslave.exceptions import AbandonChain

def shell_quote(cmd_list):
# attempt to quote cmd_list such that a shell will properly re-interpret
# it. The pipes module is only available on UNIX, and Windows "shell"
# quoting is indescribably convoluted - so much so that it's not clear it's
# reversible. Also, the quote function is undocumented (although it looks
# like it will be documentd soon: http://bugs.python.org/issue9723).
# Finally, it has a nasty bug in some versions where an empty string is not
# quoted.
#
# So:
# - use pipes.quote on UNIX, handling '' as a special case
# - use Python's repr() on Windows, as a best effort
if runtime.platformType == 'win32':
return " ".join([ `e` for e in cmd_list ])
else:
import pipes
def quote(e):
if not e:
return '""'
return pipes.quote(e)
return " ".join([ quote(e) for e in cmd_list ])

class LogFileWatcher:
POLL_INTERVAL = 2

Expand Down Expand Up @@ -389,7 +411,8 @@ def _startCommand(self):
argv += list(self.command)
else:
argv = self.command
display = " ".join(self.fake_command)
# Attempt to format this for use by a shell, although the process isn't perfect
display = shell_quote(self.fake_command)

# $PWD usually indicates the current directory; spawnProcess may not
# update this value, though, so we set it explicitly here. This causes
Expand All @@ -399,10 +422,6 @@ def _startCommand(self):

# self.stdin is handled in RunProcessPP.connectionMade

# first header line is the command in plain text, argv joined with
# spaces. You should be able to cut-and-paste this into a shell to
# obtain the same results. If there are spaces in the arguments, too
# bad.
log.msg(" " + display)
self._addToBuffers('header', display+"\n")

Expand Down

0 comments on commit 6c9af57

Please sign in to comment.