File: test_cli.py

package info (click to toggle)
zeekctl 2.2.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,544 kB
  • sloc: python: 5,639; sh: 1,374; makefile: 71; awk: 24
file content (45 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import sys
import requests
import time
import pprint
import json

def log(id, last):
    res = requests.get("http://localhost:8082/log/%d/%d" % (id, last)).json()
    if not res['log']:
        sys.stdout.write("waiting...\r")
        sys.stdout.flush()
        return last

    for rec in res['log']:
        print ' '.join(rec), '                '
    return last+len(res['log'])

def wait(id):
    print "Waiting for job %d to finish" % id
    last = 0
    while True:
        res = requests.get("http://localhost:8082/result/%d" % id).json()
        last = log(id, last)
        if res['result'] is not None:
            return json.loads(res['result'])
        time.sleep(.2)


def run(action):
    res = requests.get("http://localhost:8082/%s" % action).json()
    print(wait(res['id']))

def call(action):
    out = requests.get("http://localhost:8082/%s" % action).text
    try:
        return json.loads(out)
    except:
        return out

if __name__ == "__main__":
    if sys.argv[1] == 'bg':
        print call(sys.argv[2])
    else:
        run(sys.argv[1])