summaryrefslogtreecommitdiff
path: root/cli.py
blob: 441a50efd4b6eaf9d28e117390f2f18550872920 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
import click
from subprocess import call
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from pathlib import Path
import time
import os
import yaml


os.environ['PYTHONPATH'] = os.getcwd()


def sh(cmd, all=False, **kwargs):
    click.echo('$ {0}'.format(cmd))
    return call(cmd, shell=True, **kwargs)


@click.group()
def cli():
    pass


@cli.command()
def antlr():
    cwd = str(Path('qface/idl/parser').absolute())
    sh('antlr4 -Dlanguage=Python3 -Werror -package qface.idl.parser -o . -listener -visitor T.g4', cwd=cwd)

@cli.command()
def test():
    sh('python3 -m pytest -v -s -l --pdb')


@cli.command()
def test_ci():
    sh('python3 -m pytest -v -s -l')


class RunTestChangeHandler(FileSystemEventHandler):
    def __init__(self, clickContext):
        super(RunTestChangeHandler).__init__()
        self.clickContext = clickContext

    def on_any_event(self, event):
        if event.is_directory:
            return
        if Path(event.src_path).suffix == '.py':
            sh('python3 -m pytest -v -s -l')


@cli.command()
@click.pass_context
def test_monitor(ctx):
    sh('python3 -m pytest -v -s -l')
    while True:
        event_handler = RunTestChangeHandler(ctx)
        observer = Observer()
        observer.schedule(event_handler, './tests', recursive=False)
        observer.schedule(event_handler, './examples', recursive=False)
        observer.schedule(event_handler, './qface/idl', recursive=False)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()


class RunScriptChangeHandler(FileSystemEventHandler):
    def __init__(self, script):
        super(RunTestChangeHandler).__init__()
        self.script = script

    def on_modified(self, event):
        if event.src_path.endswith('.cache'):
            return
        if event.is_directory:
            return
        print(event)
        sh('python3 {0}'.format(self.script))


@cli.command()
@click.option('--script')
def generator_monitor(script):
    print('run script: ' + script)
    event_handler = RunScriptChangeHandler(script)
    observer = Observer()
    observer.schedule(event_handler, './templates', recursive=True)
    observer.schedule(event_handler, './examples', recursive=True)
    observer.schedule(event_handler, str(Path(script).parent), recursive=False)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()


@cli.command()
@click.option('--runner', type=click.File('r'))
@click.option('--generator', type=click.Path(exists=True))
@click.option('--input', type=click.Path(exists=True))
@click.option('--output', type=click.Path(exists=True))
def generate(runner, generator, input, output):
    if runner:
        config = yaml.load(runner)
        generator = config['generator']
        input = config['input']
        output = config['output']
    input = Path(input).absolute()
    output = Path(output).absolute()
    sh('python3 ./generator/{0}/{0}.py --input {1} --output {2}'.format(generator, input, output))

if __name__ == '__main__':
    cli()