summaryrefslogtreecommitdiffstats
path: root/formats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2015-06-23 22:18:06 +0200
committerJan Pokorný <jpokorny@redhat.com>2015-07-02 23:43:12 +0200
commitf7788fb566ae75ab75dd26b44207d7e0e04270b6 (patch)
treecda1c8fc47b1d1e9f133a3b709171ba0e7634112 /formats
parentfa248f6e4fcad3d67b2dd43e6b6eacc34476cfeb (diff)
downloadclufter-f7788fb566ae75ab75dd26b44207d7e0e04270b6.tar.gz
clufter-f7788fb566ae75ab75dd26b44207d7e0e04270b6.tar.xz
clufter-f7788fb566ae75ab75dd26b44207d7e0e04270b6.zip
formats/command: need to preserve quotation info
... otherwise original quotation is not preserved in, say, ccs2pcscmd(-flatiron) command due to command format usage in cmd-wrap filter (--noop=cmd-wrap would serve as a workaround before this fix). Also add a respective unit test. Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'formats')
-rw-r--r--formats/command.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/formats/command.py b/formats/command.py
index 04a49ad..8acd8fd 100644
--- a/formats/command.py
+++ b/formats/command.py
@@ -25,6 +25,27 @@ class command(SimpleFormat):
DICT = Protocol('dict')
MERGED = Protocol('merged')
+ @staticmethod
+ def _escape(base, qs=("'", '"')):
+ # rule: last but one item in qs cannot be escaped inside enquotion
+ ret = []
+ for b in base:
+ if ' ' in b or any(b.startswith(q) or b.endswith(q) for q in qs):
+ use_q = ''
+ for q in qs:
+ if q not in b:
+ use_q = q
+ break
+ else:
+ use_q = qs[-1]
+ if use_q != qs[0]:
+ b = b.replace(use_q, '\\' + use_q)
+ else:
+ raise RuntimeError('cannot quote the argument')
+ b = b.join((use_q, use_q))
+ ret.append(b)
+ return ret
+
@SimpleFormat.producing(BYTESTRING, chained=True)
def get_bytestring(self, *protodecl):
"""Return command as canonical single string"""
@@ -57,6 +78,9 @@ class command(SimpleFormat):
if self.BYTESTRING in self._representations: # break the possible loop
from shlex import split
ret = split(self.BYTESTRING())
+ enquote = self._dict.get('enquote', True)
+ if enquote:
+ ret = self._escape(ret)
for i, lexeme in enumerate(ret[:]):
# heuristic(!) method to normalize: '-a=b' -> '-a', 'b'
if (lexeme.count('=') == 1 and