author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Wed, 12 Apr 2023 21:05:00 +0200 | |
branch | python3 |
changeset 3793 | 9958cf865da0 |
parent 3777 | a26453d1c54d |
child 3847 | 832c257d5618 |
permissions | -rw-r--r-- |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
3566
7f5659b1eeb8
LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
3 |
|
7f5659b1eeb8
LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
4 |
import os |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
5 |
import sys |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
6 |
import tempfile |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
7 |
import random |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
8 |
import shutil |
3714
c3a13fcbda3a
cosmetic fix
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3566
diff
changeset
|
9 |
|
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
from util.ProcessLogger import ProcessLogger |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
from util.paths import Bpath |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
|
3777 | 13 |
_exec = sys.executable if "darwin" not in sys.platform else sys.executable + 'w' |
14 |
LocalRuntimeInterpreterPath = os.environ.get("BEREMIZPYTHONPATH", _exec) |
|
3566
7f5659b1eeb8
LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
15 |
|
3721
6365e33fb11f
Tests: allow overriding local host's hostname with BEREIS_LOCAL_HOST env variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3714
diff
changeset
|
16 |
LocalHost = os.environ.get("BEREMIZ_LOCAL_HOST", "localhost") |
6365e33fb11f
Tests: allow overriding local host's hostname with BEREIS_LOCAL_HOST env variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3714
diff
changeset
|
17 |
|
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
class LocalRuntimeMixin(): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
19 |
|
3539
c2eec6aae07e
CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3501
diff
changeset
|
20 |
def __init__(self, log, use_gui=True): |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
self.local_runtime_log = log |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
self.local_runtime = None |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
self.runtime_port = None |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
self.local_runtime_tmpdir = None |
3539
c2eec6aae07e
CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3501
diff
changeset
|
25 |
self.use_gui = use_gui |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
|
3539
c2eec6aae07e
CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3501
diff
changeset
|
27 |
def StartLocalRuntime(self): |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
28 |
if (self.local_runtime is None) or (self.local_runtime.exitcode is not None): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
29 |
# create temporary directory for runtime working directory |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
self.local_runtime_tmpdir = tempfile.mkdtemp() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
# choose an arbitrary random port for runtime |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
self.runtime_port = int(random.random() * 1000) + 61131 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
self.local_runtime_log.write(_("Starting local runtime...\n")) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
34 |
# launch local runtime |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
35 |
self.local_runtime = ProcessLogger( |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
36 |
self.local_runtime_log, |
3721
6365e33fb11f
Tests: allow overriding local host's hostname with BEREIS_LOCAL_HOST env variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3714
diff
changeset
|
37 |
("\"%s\" \"%s\" -p %s -i "+LocalHost+" %s %s") % ( |
3566
7f5659b1eeb8
LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
38 |
LocalRuntimeInterpreterPath, |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
39 |
Bpath("Beremiz_service.py"), |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
40 |
self.runtime_port, |
3539
c2eec6aae07e
CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3501
diff
changeset
|
41 |
{False: "-x 0", True: "-x 1"}[self.use_gui], |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
self.local_runtime_tmpdir), |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
43 |
no_gui=False, |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
44 |
timeout=500, keyword=self.local_runtime_tmpdir, |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
cwd=self.local_runtime_tmpdir) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
self.local_runtime.spin() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
47 |
return self.runtime_port |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
48 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
49 |
def KillLocalRuntime(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
50 |
if self.local_runtime is not None: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
# shutdown local runtime |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
self.local_runtime.kill(gently=False) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
# clear temp dir |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
shutil.rmtree(self.local_runtime_tmpdir) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
self.local_runtime = None |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |