File: clear_zk.py

package info (click to toggle)
python-taskflow 1.30.0-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 3,252 kB
  • sloc: python: 26,508; sh: 204; makefile: 48
file content (50 lines) | stat: -rw-r--r-- 1,332 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
46
47
48
49
50
#!/usr/bin/env python

import contextlib
import os
import re
import sys

top_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                       os.pardir))
sys.path.insert(0, top_dir)

from taskflow.utils import kazoo_utils


@contextlib.contextmanager
def finalize_client(client):
    try:
        yield client
    finally:
        kazoo_utils.finalize_client(client)


def iter_children(client, path):
    if client.exists(path):
        for child_path in client.get_children(path):
            if path == "/":
                child_path = "/%s" % (child_path)
            else:
                child_path = "%s/%s" % (path, child_path)
            yield child_path
            for child_child_path in iter_children(client, child_path):
                yield child_child_path


def main():
    conf = {}
    if len(sys.argv) > 1:
        conf['hosts'] = sys.argv[1:]
    with finalize_client(kazoo_utils.make_client(conf)) as client:
        client.start(timeout=1.0)
        children = list(iter_children(client, "/taskflow"))
        for child_path in reversed(children):
            if not re.match(r"^/taskflow/(.*?)-test/(.*)$", child_path):
                continue
            print("Deleting %s" % child_path)
            client.delete(child_path)


if __name__ == "__main__":
    main()