LocalRuntimeMixin.py
branchwxPython4
changeset 3501 fa291393aac7
child 3539 c2eec6aae07e
equal deleted inserted replaced
3491:88c4b18453d5 3501:fa291393aac7
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 import sys
       
     4 import tempfile
       
     5 import random
       
     6 import shutil
       
     7 from util.ProcessLogger import ProcessLogger
       
     8 from util.paths import Bpath
       
     9 
       
    10 class LocalRuntimeMixin():
       
    11 
       
    12     def __init__(self, log):
       
    13         self.local_runtime_log = log
       
    14         self.local_runtime = None
       
    15         self.runtime_port = None
       
    16         self.local_runtime_tmpdir = None
       
    17 
       
    18     def StartLocalRuntime(self, taskbaricon=True):
       
    19         if (self.local_runtime is None) or (self.local_runtime.exitcode is not None):
       
    20             # create temporary directory for runtime working directory
       
    21             self.local_runtime_tmpdir = tempfile.mkdtemp()
       
    22             # choose an arbitrary random port for runtime
       
    23             self.runtime_port = int(random.random() * 1000) + 61131
       
    24             self.local_runtime_log.write(_("Starting local runtime...\n"))
       
    25             # launch local runtime
       
    26             self.local_runtime = ProcessLogger(
       
    27                 self.local_runtime_log,
       
    28                 "\"%s\" \"%s\" -p %s -i localhost %s %s" % (
       
    29                     sys.executable,
       
    30                     Bpath("Beremiz_service.py"),
       
    31                     self.runtime_port,
       
    32                     {False: "-x 0", True: "-x 1"}[taskbaricon],
       
    33                     self.local_runtime_tmpdir),
       
    34                 no_gui=False,
       
    35                 timeout=500, keyword=self.local_runtime_tmpdir,
       
    36                 cwd=self.local_runtime_tmpdir)
       
    37             self.local_runtime.spin()
       
    38         return self.runtime_port
       
    39 
       
    40     def KillLocalRuntime(self):
       
    41         if self.local_runtime is not None:
       
    42             # shutdown local runtime
       
    43             self.local_runtime.kill(gently=False)
       
    44             # clear temp dir
       
    45             shutil.rmtree(self.local_runtime_tmpdir)
       
    46 
       
    47             self.local_runtime = None
       
    48