LocalRuntimeMixin.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 19 Oct 2022 20:53:01 +0200
branchwxPython4
changeset 3635 5107a2712b17
parent 3566 7f5659b1eeb8
child 3714 c3a13fcbda3a
permissions -rw-r--r--
Update README.md
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
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
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
    10
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
    11
3566
7f5659b1eeb8 LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    12
LocalRuntimeInterpreterPath = \
7f5659b1eeb8 LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    13
    os.environ["BEREMIZPYTHONPATH"] \
7f5659b1eeb8 LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    14
    if os.environ.has_key("BEREMIZPYTHONPATH") \
7f5659b1eeb8 LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    15
    else sys.executable
7f5659b1eeb8 LocalRuntime: Allow overriding local runtime python binary path using BEREMIZPYTHONPATH environment variable.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    16
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
class LocalRuntimeMixin():
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
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
    19
    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
    20
        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
    21
        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
    22
        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
    23
        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
    24
        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
    25
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
    26
    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
    27
        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
    28
            # 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
    29
            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
    30
            # 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
    31
            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
    32
            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
    33
            # 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
    34
            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
    35
                self.local_runtime_log,
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
                "\"%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
    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"),
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
                    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
    40
                    {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
    41
                    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
    42
                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
    43
                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
    44
                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
    45
            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
    46
        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
    47
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
    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
    49
        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
    50
            # 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
    51
            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
    52
            # 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
    53
            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
    54
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
            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
    56