From f31344fd71b31a0106b17e629933a73bd695de84 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 4 May 2019 18:21:43 +0100 Subject: [PATCH] Fix setup script; proper escaping to make Systemd happy --- server/grasp_server.py | 5 ++--- server/setup | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/server/grasp_server.py b/server/grasp_server.py index 9f24f5b..961af52 100755 --- a/server/grasp_server.py +++ b/server/grasp_server.py @@ -113,8 +113,7 @@ class GraspRequestHandler(BaseHTTPRequestHandler): def run(port: str, capture_path: str, template: str): logger = get_logger() - if template == repr(DEFAULT_TEMPLATE): - template = DEFAULT_TEMPLATE # hack so argparse renders default template with \n characters... + logger.info("Using template %s", template) # not sure if there is a simpler way to communicate with the server... os.environ[CAPTURE_PATH_VAR] = capture_path @@ -126,7 +125,7 @@ def run(port: str, capture_path: str, template: str): def setup_parser(p): p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension') p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into') - p.add_argument('--template', type=str, default=repr(DEFAULT_TEMPLATE), help=f""" + p.add_argument('--template', type=str, default=DEFAULT_TEMPLATE, help=f""" {as_org.__doc__} """) diff --git a/server/setup b/server/setup index bdeb59d..2446c9e 100755 --- a/server/setup +++ b/server/setup @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import argparse - +import shlex from pathlib import Path from subprocess import check_call, run @@ -32,15 +32,15 @@ def setup(args): server_bin = Path(__file__).parent.joinpath('grasp_server.py').absolute() - template = args.template.replace('%', '%%') # escape for systemd... - extra_args = f'--port {args.port} --path {args.path} --template \"{template}\"' + template = args.template.replace('%', '%%').replace('\n', r'\n') # escape for systemd... + template = shlex.quote(template) + extra_args = f'--port {args.port} --path {args.path} --template {template}' with out.open('w') as fo: fo.write(SYSTEMD_CONFIG.format( server=server_bin, extra_args=extra_args, )) - try: systemd('stop' , unit_name, method=run) # ignore errors here if it wasn't running in the first place systemd('daemon-reload')