From 9ba365dee501f0c393b3847868d25edb4e3f7c5c Mon Sep 17 00:00:00 2001 From: Nick Stenning Date: Sun, 10 Apr 2016 13:23:01 +0200 Subject: [PATCH] Escape % symbol in supervisor exporter As reported in #73, the % symbol needs to be escaped in the command or in env var values when exporting to supervisord. Fixes #73. --- honcho/export/base.py | 8 ++++++++ honcho/export/templates/supervisord/supervisord.conf | 4 ++-- tests/test_export_base.py | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/honcho/export/base.py b/honcho/export/base.py index 09b9133..7c06172 100644 --- a/honcho/export/base.py +++ b/honcho/export/base.py @@ -62,8 +62,16 @@ def dashrepl(value): return re.sub(patt, '-', value) +def percentescape(value): + """ + Double any % signs. + """ + return value.replace('%', '%%') + + def _default_template_env(loader): env = jinja2.Environment(loader=loader) env.filters['shellquote'] = shellquote env.filters['dashrepl'] = dashrepl + env.filters['percentescape'] = percentescape return env diff --git a/honcho/export/templates/supervisord/supervisord.conf b/honcho/export/templates/supervisord/supervisord.conf index e1218ec..1417a76 100644 --- a/honcho/export/templates/supervisord/supervisord.conf +++ b/honcho/export/templates/supervisord/supervisord.conf @@ -1,6 +1,6 @@ {% for p in processes -%} [program:{{ p.name|dashrepl }}] -command={{ shell }} -c '{{ p.cmd }}' +command={{ shell }} -c '{{ p.cmd|percentescape }}' autostart=true autorestart=true stopsignal=QUIT @@ -10,7 +10,7 @@ user={{ user }} directory={{ app_root }} environment= {%- for key,value in p.env.items() -%} - {{ key }}={{ value|shellquote }} + {{ key }}={{ value|shellquote|percentescape }} {%- if not loop.last %},{% endif %} {%- endfor %} diff --git a/tests/test_export_base.py b/tests/test_export_base.py index 816e1f1..93545db 100644 --- a/tests/test_export_base.py +++ b/tests/test_export_base.py @@ -8,6 +8,7 @@ from honcho.export.base import BaseExport from honcho.export.base import dashrepl +from honcho.export.base import percentescape class GiraffeExport(BaseExport): @@ -45,3 +46,13 @@ def test_get_template(self): )) def test_dashrepl(name_in, name_out): assert dashrepl(name_in) == name_out + + +@pytest.mark.parametrize('val_in,val_out', ( + ('foo', 'foo'), + ('foo name.%h', 'foo name.%%h'), + ('%one two% %three', '%%one two%% %%three'), + ('foo%%bar', 'foo%%%%bar'), +)) +def test_percentescape(val_in, val_out): + assert percentescape(val_in) == val_out