author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Tue, 03 Dec 2024 21:26:52 +0100 | |
changeset 4058 | 5472b88bbc9d |
parent 3967 | 8d82aa6e9dd9 |
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 |
|
3847
832c257d5618
IDE: connect local runtime with '127.0.0.1' instead of 'localhost'
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3777
diff
changeset
|
16 |
LocalHost = os.environ.get("BEREMIZ_LOCAL_HOST", "127.0.0.1") |
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
|
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( |
3911
ec3c7e76f694
IDE: Fix local runtime not starting on windows.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3847
diff
changeset
|
36 |
self.local_runtime_log,[ |
3566
7f5659b1eeb8
LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
37 |
LocalRuntimeInterpreterPath, |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
Bpath("Beremiz_service.py"), |
3911
ec3c7e76f694
IDE: Fix local runtime not starting on windows.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3847
diff
changeset
|
39 |
"-p", str(self.runtime_port), |
ec3c7e76f694
IDE: Fix local runtime not starting on windows.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3847
diff
changeset
|
40 |
"-i", LocalHost, |
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], |
3911
ec3c7e76f694
IDE: Fix local runtime not starting on windows.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3847
diff
changeset
|
42 |
self.local_runtime_tmpdir], |
3501
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 |
3967
8d82aa6e9dd9
LocalRuntime: no crash if zombie PLC prevents erasing workdir
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3911
diff
changeset
|
54 |
try: |
8d82aa6e9dd9
LocalRuntime: no crash if zombie PLC prevents erasing workdir
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3911
diff
changeset
|
55 |
shutil.rmtree(self.local_runtime_tmpdir) |
8d82aa6e9dd9
LocalRuntime: no crash if zombie PLC prevents erasing workdir
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3911
diff
changeset
|
56 |
except: |
8d82aa6e9dd9
LocalRuntime: no crash if zombie PLC prevents erasing workdir
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3911
diff
changeset
|
57 |
pass |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
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
|
60 |