LocalRuntimeMixin.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sun, 17 Jul 2022 23:30:02 +0200
branchwxPython4
changeset 3560 f0ed67b01a9d
parent 3539 c2eec6aae07e
child 3566 7f5659b1eeb8
permissions -rw-r--r--
GitHub CI: my bad. m( Was just wrong version
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 -*-
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
import sys
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
import tempfile
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import random
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
import shutil
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
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
     8
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
     9
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
class LocalRuntimeMixin():
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
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
    12
    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
    13
        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
    14
        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
    15
        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
    16
        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
    17
        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
    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 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
    20
        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
    21
            # 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
    22
            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
    23
            # 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
    24
            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
    25
            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
    26
            # 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
    27
            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
    28
                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
    29
                "\"%s\" \"%s\" -p %s -i localhost %s %s" % (
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
                    sys.executable,
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
                    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
    32
                    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
    33
                    {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
    34
                    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
    35
                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
    36
                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
    37
                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
    38
            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
    39
        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
    40
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
    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
    42
        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
    43
            # 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
    44
            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
    45
            # 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
    46
            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
    47
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
            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
    49