summaryrefslogtreecommitdiff
path: root/cli.py
blob: 1960108a1f73c714684fbfe297415b962978643d (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
# Copyright (c) Pelagicore AG 2016

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 sys
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():
    """generate a new parser based on the grammar using 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()
@click.option('--debug/--nodebug')
def test(debug):
    """run the tests"""
    sh('python3 -m pytest -v -s -l {0}'.format('-pdb' if debug else ''))


@cli.command()
def test_ci():
    """run the tests for CI integration"""
    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):
    """run the tests and re-run on changes"""
    sh('python3 -m pytest -v -s -l')
    while True:
        event_handler = RunTestChangeHandler(ctx)
        observer = Observer()
        observer.schedule(event_handler, './tests', recursive=True)
        observer.schedule(event_handler, './qface', recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()


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

    def on_modified(self, event):
        if event.src_path.endswith('.cache'):
            return
        if event.is_directory:
            return
        self.run()

    def run(self):
        sh('python3 {0}'.format(self.script), cwd=self.cwd)


@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_monitor(runner, generator, input, output):
    """run the named generator and monitor the input and generator folder"""
    if runner:
        config = yaml.load(runner)
        generator = config['generator']
        input = config['input']
        output = config['output']
    generator = Path(generator).absolute()
    input = Path(input).absolute()
    output = Path(output).absolute()
    script = generator / '{0}.py --input {1} --output {2}'.format(generator.name, input, output)
    event_handler = RunScriptChangeHandler(script, cwd=generator.as_posix())
    observer = Observer()
    observer.schedule(event_handler, generator.as_posix(), recursive=True)
    observer.schedule(event_handler, input.as_posix(), recursive=True)
    observer.schedule(event_handler, './qface', recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()


@cli.command()
@click.option('--runner', type=click.File('r'), help="use the runner YAML file to configure the generation")
@click.option('--reload/--no-reload', default=False, help="if enabled auto-reload the generator on input changes")
@click.option('--generator', help="specifies the generator (either by name or path)")
@click.option('--input', type=click.Path(exists=True), help="specifies the input folder")
@click.option('--output', type=click.Path(exists=True), help="specified the output folder")
@click.option('--list/--no-list', help="lists the available generators")
def generate(runner, generator, input, output, reload, list):
    if list:
        entries = [e.name for e in Path('generator').iterdir()]
        click.echo('generators: {0}'.format(entries))
        sys.exit(0)
    """run the named generator"""
    if runner:
        config = yaml.load(runner)
        generator = config['generator']
        input = config['input']
        output = config['output']
    if not generator or not input or not output:
        print('generator, input and output arguments are required')
        sys.exit(-1)
    generator = Path('generator') / generator
    if not generator.exists():
        generator = Path(generator).absolute()
    if not generator.exists():
        print('can not find the specified generator: ' + str(generator))
        sys.exit(-1)
    input = Path(input).absolute()
    output = Path(output).absolute()
    if not reload:
        _generate_once(generator, input, output)
    else:
        _generate_reload(generator, input, output)



def _generate_once(generator, input, output):
    script = '{0}.py'.format(generator.name)
    input = Path(input).absolute()
    output = Path(output).absolute()
    sh('python3 {0} --input {1} --output {2}'
        .format(script, input, output),
        cwd=generator.as_posix())


def _generate_reload(generator, input, output):
    """run the named generator and monitor the input and generator folder"""
    script = generator / '{0}.py --input {1} --output {2}'.format(generator.name, input, output)
    event_handler = RunScriptChangeHandler(script, cwd=generator.as_posix())
    event_handler.run() # run always once
    observer = Observer()
    observer.schedule(event_handler, generator.as_posix(), recursive=True)
    observer.schedule(event_handler, input.as_posix(), recursive=True)
    observer.schedule(event_handler, './qface', recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(2)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()


@click.option('--editable/--no-editable', default=False, help='install editable package')
@cli.command()
def install(editable):
    """install the script onto the system using pip3"""
    script_dir = str(Path(__file__).parent.absolute())
    print(script_dir)
    if editable:
        sh('pip3 install --editable {0} --upgrade'.format(script_dir))
    else:
        sh('pip3 install {0} --upgrade'.format(script_dir))


@cli.command()
def uninstall():
    """uninstall the script from the system using pip3"""
    sh('pip3 uninstall qface')


if __name__ == '__main__':
    cli()