forked from beremiz/beremiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalRuntimeMixin.py
70 lines (59 loc) · 2.36 KB
/
LocalRuntimeMixin.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import tempfile
import random
import shutil
from util.ProcessLogger import ProcessLogger
from util.paths import Bpath
from pubsub import pub
_exec = sys.executable if "darwin" not in sys.platform else sys.executable + 'w'
LocalRuntimeInterpreterPath = os.environ.get("BEREMIZPYTHONPATH", _exec)
LocalHost = os.environ.get("BEREMIZ_LOCAL_HOST", "localhost")
class LocalRuntimeMixin():
def __init__(self, log, use_gui=True):
self.local_runtime_log = log
self.local_runtime = None
self.runtime_port = None
self.local_runtime_tmpdir = None
self.use_gui = use_gui
def StartLocalRuntime(self):
if self.Controler.IsEmbeddedPlatform():
service = "service_pio.py"
if 'Win32' not in self.Controler.GetDefaultTargetName():
port = f'/dev/{self.port}'
else:
port = self.port
else:
service = 'service.py'
port = ''
if (self.local_runtime is None) or (self.local_runtime.exitcode is not None):
# create temporary directory for runtime working directory
self.local_runtime_tmpdir = tempfile.mkdtemp()
# choose an arbitrary random port for runtime
self.runtime_port = int(random.random() * 1000) + 61131
self.local_runtime_log.write(_("Starting local runtime...\n"))
# launch local runtime
self.local_runtime = ProcessLogger(
self.local_runtime_log,
"\"%s\" \"%s\" -p %s -i %s %s %s %s" % (
LocalRuntimeInterpreterPath,
Bpath(service),
self.runtime_port,
LocalHost,
{False: "-x 0", True: "-x 1"}[self.use_gui],
self.local_runtime_tmpdir,
port),
no_gui=False,
timeout=500, keyword=self.local_runtime_tmpdir,
cwd=self.local_runtime_tmpdir)
self.local_runtime.spin()
return self.runtime_port
def KillLocalRuntime(self):
if self.local_runtime is not None:
# shutdown local runtime
self.local_runtime.kill(gently=False)
# clear temp dir
shutil.rmtree(self.local_runtime_tmpdir)
self.local_runtime = None