From 1b93ed57ed80739f55fd3d9e8715163b2aa6089b Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Tue, 28 Dec 2010 15:34:23 +0200 Subject: [PATCH] Don't modify commands when logging them --- ubuntutools/logger.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ubuntutools/logger.py b/ubuntutools/logger.py index 1b0ecf7..f871e30 100644 --- a/ubuntutools/logger.py +++ b/ubuntutools/logger.py @@ -20,6 +20,12 @@ import os import sys +def escape_arg(arg): + "Shell-escpae arg, if necessary" + if ' ' not in arg: + return arg + return '"%s"' % arg.replace('\\', r'\\').replace('"', r'\"') + class Logger(object): script_name = os.path.basename(sys.argv[0]) verbose = False @@ -30,10 +36,9 @@ class Logger(object): @classmethod def command(cls, cmd): if cls.verbose: - for i in xrange(len(cmd)): - if cmd[i].find(" ") >= 0: - cmd[i] = '"' + cmd[i] + '"' - print >> cls.stdout, "%s: I: %s" % (cls.script_name, " ".join(cmd)) + print >> cls.stdout, "%s: I: %s" % (cls.script_name, + " ".join(escape_arg(arg) + for arg in cmd)) @classmethod def debug(cls, message, *args):