From 7f120a87e74f915e16126a90d4c10cb0d7914fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Abdelkader=20Mart=C3=ADnez=20P=C3=A9rez?= Date: Fri, 26 Apr 2019 08:38:32 +0200 Subject: [PATCH] Fixing poc. --- poc/.pylintrc | 4 +++- poc/bin/kroute | 12 ++++++++---- poc/examples/helloworld.pow | 24 ++++-------------------- poc/kapow | 17 +++++------------ 4 files changed, 20 insertions(+), 37 deletions(-) diff --git a/poc/.pylintrc b/poc/.pylintrc index 6b865ce..715b3ab 100644 --- a/poc/.pylintrc +++ b/poc/.pylintrc @@ -149,7 +149,9 @@ disable=print-statement, unused-argument, no-value-for-parameter, missing-docstring, - fixme + fixme, + broad-except, + redefined-builtin # Enable the message, report, category or checker with the given id(s). You can diff --git a/poc/bin/kroute b/poc/bin/kroute index 02ae4ba..5fd23ab 100755 --- a/poc/bin/kroute +++ b/poc/bin/kroute @@ -14,17 +14,21 @@ def kroute(): @click.option("-X", "--method", default="GET") @click.option("--url", envvar='KAPOW_URL') @click.argument("url_pattern", nargs=1) -@click.argument("command_file") +@click.argument("command_file", required=False) def add(url_pattern, entrypoint, command, method, url, command_file): if command: + # Command is given inline source = command + elif command_file is None: + # No command + source = "" elif command_file == '-': + # Read commands from stdin source = sys.stdin.read() - elif command_file is not None: + else: + # Read commands from a file with open(command_file, 'r', encoding='utf-8') as handler: source = handler.read() - else: - source = "" response = requests.post(f"{url}/routes", json={"method": method, diff --git a/poc/examples/helloworld.pow b/poc/examples/helloworld.pow index 4ee8f58..90d61d3 100755 --- a/poc/examples/helloworld.pow +++ b/poc/examples/helloworld.pow @@ -1,30 +1,14 @@ #!/bin/bash -touch input -touch output -tail -F input | tr '[:lower:]' '[:upper:]' > output & +kroute add -X POST --entrypoint ./topdf '/convert/{from}/pdf' -kroute add -X POST --entrypoint /home/nil/Project/kapow/kapow2/examples/topdf '/convert/{from}/pdf' - - -kroute add -X POST --entrypoint /bin/zsh '/convert/{from}/{to}' - <<-'EOF' +kroute add -X POST --entrypoint '/bin/zsh -c' '/convert/{from}/{to}' - <<-'EOF' pandoc --from=$(request /match/from) --to=$(request /match/to) --output=>(response /body) =(request /body) EOF -kroute add -X GET '/formats/input' - <<-EOF +kroute add -X GET '/formats/input' - <<-'EOF' pandoc --list-input-formats | response /body EOF -kroute add '/formats/output' - <<-EOF - pandoc --list-output-formats | grep -v pdf | response /body -EOF - -kroute add '/tail' - <<-EOF - tail -f /tmp/mispelotas | response /stream -EOF - -kroute add -X POST '/tr' --command ' - request /body >> input - tail -n1 output | response /body -' +kroute add -X GET '/formats/output' -c 'pandoc --list-output-formats | grep -v pdf | response /body' diff --git a/poc/kapow b/poc/kapow index 4634194..9a719ea 100755 --- a/poc/kapow +++ b/poc/kapow @@ -161,18 +161,18 @@ def handle_route(entrypoint, command): connection = CONNECTIONS[id] = ConnectionHandler(request) executable, *params = shlex.split(entrypoint) - args = ' '.join([shlex.quote(token) for token in params] + [shlex.quote(command)]) + args = ' '.join([executable] + [shlex.quote(token) for token in params] + [shlex.quote(command)]) + # Run the source - shell_task = await asyncio.create_subprocess_exec( + shell_task = await asyncio.create_subprocess_shell( args, - executable=executable, env={**os.environ, "KAPOW_URL": "http://localhost:8080/kapow", "KAPOW_CONNECTION": id, "PATH": ":".join([os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin"), os.environ["PATH"]]), }, - shell=False) + stdin=asyncio.subprocess.DEVNULL) await shell_task.wait() del CONNECTIONS[id] @@ -193,7 +193,6 @@ async def get_routes(request): async def create_route(request): request.app.router._frozen = False content = await request.json() - print(f'Defined new route {content["method"]} {content["url_pattern"]}') name = "ROUTE_" + str(uuid4()).replace('-', '_') request.app.router.add_route(content["method"], content["url_pattern"], @@ -215,13 +214,7 @@ async def delete_route(request): ######################################################################## async def run_init_script(): - if len(sys.argv) > 1: - # Script given - # with open(sys.argv[1], 'r') as scriptfile: - # script = scriptfile.read() - pass - else: - # script = sys.stdin.read() + if len(sys.argv) < 2: raise RuntimeError("Script file is mandatory.") shell_task = await asyncio.create_subprocess_shell(