Fix setup script; proper escaping to make Systemd happy

This commit is contained in:
Dima Gerasimov 2019-05-04 18:21:43 +01:00
parent fda03e28b0
commit f31344fd71
2 changed files with 6 additions and 7 deletions

View file

@ -113,8 +113,7 @@ class GraspRequestHandler(BaseHTTPRequestHandler):
def run(port: str, capture_path: str, template: str): def run(port: str, capture_path: str, template: str):
logger = get_logger() logger = get_logger()
if template == repr(DEFAULT_TEMPLATE): logger.info("Using template %s", template)
template = DEFAULT_TEMPLATE # hack so argparse renders default template with \n characters...
# not sure if there is a simpler way to communicate with the server... # not sure if there is a simpler way to communicate with the server...
os.environ[CAPTURE_PATH_VAR] = capture_path os.environ[CAPTURE_PATH_VAR] = capture_path
@ -126,7 +125,7 @@ def run(port: str, capture_path: str, template: str):
def setup_parser(p): def setup_parser(p):
p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension') 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('--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__} {as_org.__doc__}
""") """)

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import shlex
from pathlib import Path from pathlib import Path
from subprocess import check_call, run from subprocess import check_call, run
@ -32,15 +32,15 @@ def setup(args):
server_bin = Path(__file__).parent.joinpath('grasp_server.py').absolute() server_bin = Path(__file__).parent.joinpath('grasp_server.py').absolute()
template = args.template.replace('%', '%%') # escape for systemd... template = args.template.replace('%', '%%').replace('\n', r'\n') # escape for systemd...
extra_args = f'--port {args.port} --path {args.path} --template \"{template}\"' template = shlex.quote(template)
extra_args = f'--port {args.port} --path {args.path} --template {template}'
with out.open('w') as fo: with out.open('w') as fo:
fo.write(SYSTEMD_CONFIG.format( fo.write(SYSTEMD_CONFIG.format(
server=server_bin, server=server_bin,
extra_args=extra_args, extra_args=extra_args,
)) ))
try: try:
systemd('stop' , unit_name, method=run) # ignore errors here if it wasn't running in the first place systemd('stop' , unit_name, method=run) # ignore errors here if it wasn't running in the first place
systemd('daemon-reload') systemd('daemon-reload')