Skip to content
Snippets Groups Projects
Select Git revision
  • e9e6a7f61907c4e129101e17942aa194efc525e2
  • master default protected
  • L2SS-1914-fix_job_dispatch
  • TMSS-3170
  • TMSS-3167
  • TMSS-3161
  • TMSS-3158-Front-End-Only-Allow-Changing-Again
  • TMSS-3133
  • TMSS-3319-Fix-Templates
  • test-fix-deploy
  • TMSS-3134
  • TMSS-2872
  • defer-state
  • add-custom-monitoring-points
  • TMSS-3101-Front-End-Only
  • TMSS-984-choices
  • SDC-1400-Front-End-Only
  • TMSS-3079-PII
  • TMSS-2936
  • check-for-max-244-subbands
  • TMSS-2927---Front-End-Only-PXII
  • Before-Remove-TMSS
  • LOFAR-Release-4_4_318 protected
  • LOFAR-Release-4_4_317 protected
  • LOFAR-Release-4_4_316 protected
  • LOFAR-Release-4_4_315 protected
  • LOFAR-Release-4_4_314 protected
  • LOFAR-Release-4_4_313 protected
  • LOFAR-Release-4_4_312 protected
  • LOFAR-Release-4_4_311 protected
  • LOFAR-Release-4_4_310 protected
  • LOFAR-Release-4_4_309 protected
  • LOFAR-Release-4_4_308 protected
  • LOFAR-Release-4_4_307 protected
  • LOFAR-Release-4_4_306 protected
  • LOFAR-Release-4_4_304 protected
  • LOFAR-Release-4_4_303 protected
  • LOFAR-Release-4_4_302 protected
  • LOFAR-Release-4_4_301 protected
  • LOFAR-Release-4_4_300 protected
  • LOFAR-Release-4_4_299 protected
41 results

Commands.py

Blame
  • user avatar
    Martin Gels authored
    e9e6a7f6
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Commands.py 2.48 KiB
    import os
    import threading
    import time
    
    
    
    class Command(object):
        """
        Executes an external command
        """
    
        def __init__(self, commandstr):
            self.commandstr = commandstr
            self.done = False
            self.success = False
        def isDone(self):
            return self.done
        def waitForDone(self):
            raise Exception('Waiting for done on the Command Base class')
        def isSuccess(self):
            return self.success
    
    
    class ExtCommand(Command):
        """
        Executes an external command immediately
        """
    
        def __init__(self, commandstr):
            Command.__init__(self, commandstr)
            print('Immediately executing ' + self.commandstr)
            if os.system(self.commandstr) == 0:
                self.success = True
            else:
                self.success = False
            self.done = True
    
    
          
    
    class AsyncThreadCommand(Command):
        """
        Executes an external command asynchronously using threads
        """
    
        class CommandThread(threading.Thread):
            def __init__(self, commandstr):
                threading.Thread.__init__(self)
                self.commandstr = commandstr
                self.lock = threading.Lock()
                self.done = False
                self.success = False
    
            def run(self):
                result = (os.system(self.commandstr) == 0)
               
                self.lock.acquire()
                self.done = True
                self.success = result
                self.lock.release()
    
            def isDone(self):
                self.lock.acquire()
                done = self.done
                self.lock.release()
                return done
    
            def isSuccess(self):
                self.lock.acquire()
                success = self.success
                self.lock.release()
                return success
                
        def __init__(self, commandstr, timeout):
            Command.__init__(self, commandstr)
            self.thread = AsyncThreadCommand.CommandThread(commandstr)
            print('Threaded executing ' + self.commandstr)
            self.thread.start()
            self.startTimeOfRun = time.time()
            self.timeout = timeout
    
        def isTimedOut(self):
            return (self.timeout and (time.time() - self.startTimeOfRun > self.timeout))
    
        def isDone(self):
            return self.thread.isDone()
    
        def waitForDone(self):
            while not self.thread.isDone():
                time.sleep(2)
    
        def isSuccess(self):
            if not self.thread.isDone():
                self.waitForDone()
            return self.thread.isSuccess()
    
        def abort(self):
            print('Cannot stop asynchronous commands (yet)')