|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 import os |
|
5 import posixpath |
|
6 import sys |
|
7 from functools import wraps |
|
8 |
|
9 import click |
|
10 from importlib import import_module |
|
11 |
|
12 |
|
13 class CLISession(object): |
|
14 def __init__(self, **kwargs): |
|
15 self.__dict__.update(kwargs) |
|
16 self.controller = None |
|
17 |
|
18 pass_session = click.make_pass_decorator(CLISession) |
|
19 |
|
20 |
|
21 @click.group(chain=True) |
|
22 @click.option( |
|
23 "--project-home", |
|
24 envvar="PROJECT_HOME", |
|
25 default=".", |
|
26 metavar="PATH", |
|
27 help="Changes the project folder location.", |
|
28 ) |
|
29 @click.option( |
|
30 "--config", |
|
31 nargs=2, |
|
32 multiple=True, |
|
33 metavar="KEY VALUE", |
|
34 help="Overrides a config key/value pair.", |
|
35 ) |
|
36 @click.option( |
|
37 "--keep", "-k", is_flag=True, |
|
38 help="Keep local runtime, do not kill it after executing commands.", |
|
39 ) |
|
40 @click.option("--verbose", "-v", is_flag=True, help="Enables verbose mode.") |
|
41 @click.option( |
|
42 "--buildpath", "-b", help="Where to store files created during build." |
|
43 ) |
|
44 @click.option( |
|
45 "--uri", "-u", help="URI to reach remote PLC." |
|
46 ) |
|
47 @click.version_option("0.1") |
|
48 @click.pass_context |
|
49 def cli(ctx, **kwargs): |
|
50 """Beremiz CLI manipulates beremiz projects and runtimes. """ |
|
51 |
|
52 ctx.obj = CLISession(**kwargs) |
|
53 |
|
54 def ensure_controller(func): |
|
55 @wraps(func) |
|
56 def func_wrapper(session, *args, **kwargs): |
|
57 if session.controller is None: |
|
58 session.controller = import_module("CLIController").CLIController(session) |
|
59 ret = func(session, *args, **kwargs) |
|
60 return ret |
|
61 |
|
62 return func_wrapper |
|
63 |
|
64 @cli.command() |
|
65 @click.option( |
|
66 "--target", "-t", help="Target system triplet." |
|
67 ) |
|
68 @pass_session |
|
69 @ensure_controller |
|
70 def build(session, target): |
|
71 """Builds project. """ |
|
72 def processor(): |
|
73 return session.controller.build_project(target) |
|
74 return processor |
|
75 |
|
76 @cli.command() |
|
77 @pass_session |
|
78 @ensure_controller |
|
79 def transfer(session): |
|
80 """Transfer program to PLC runtim.""" |
|
81 def processor(): |
|
82 return session.controller.transfer_project() |
|
83 return processor |
|
84 |
|
85 @cli.command() |
|
86 @pass_session |
|
87 @ensure_controller |
|
88 def run(session): |
|
89 """Run program already present in PLC. """ |
|
90 def processor(): |
|
91 return session.controller.run_project() |
|
92 return processor |
|
93 |
|
94 |
|
95 @cli.resultcallback() |
|
96 @pass_session |
|
97 def process_pipeline(session, processors, **kwargs): |
|
98 ret = 0 |
|
99 for processor in processors: |
|
100 ret = processor() |
|
101 if ret != 0: |
|
102 if len(processors) > 1 : |
|
103 click.echo("Command sequence aborted") |
|
104 break |
|
105 |
|
106 session.controller.finish() |
|
107 |
|
108 return ret |
|
109 |
|
110 if __name__ == '__main__': |
|
111 cli() |
|
112 |