git @ Cat's Eye Technologies Falderal / 579fbb6
Shell quoting rules are highly awesome. Chris Pressey 3 years ago
1 changed file(s) with 11 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
566566 def subst(self, command, var_name, value):
567567 """Replace all occurrences of `var_name` in `command` with
568568 `value`, but make sure `value` is properly shell-escaped first."""
569
569570 # We could do this with shlex.quote, but that only appeared in 3.3.
570571 # To support Python 2.7, we just take every character that is a
571572 # shell metacharacter, and escape it. Note that we have to handle
572573 # backslashes first, lest we escape backslashes we just added in.
574
573575 value = value.replace('\\', '\\\\')
574576 for c in """ ><*?[]'"`$()|;&#""":
575577 value = value.replace(c, '\\' + c)
578
579 # Note that, to handle putting multi-line strings into a single
580 # command line, we need to escape newlines. Note, however, that
581 # shells don't understand escape sequence like "\n"! Instead, we
582 # put an actual newline in single quotes. We do this for tabs too.
583
584 value = value.replace("\n", "'\n'")
585 value = value.replace("\t", "'\t'")
586
576587 return command.replace(var_name, value)
577588
578589 def run(self, body=None, input=None):