def write_after_flock(self, pid_file): inst = ApplicationInstance(os.path.abspath(pid_file), autoExit=False, flock=True) with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock()
def write_after_flock(self, pid_file): inst = ApplicationInstance(os.path.abspath(pid_file), auto_exit = False, flock = True) with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock()
def test_autoExit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit = True) self.assertExists(self.file_name) this_pid = os.getpid() this_procname = tools.processName(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname))
def test_autoExit_other_running_process(self): pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) with self.assertRaises(SystemExit): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit=True)
def test_auto_flock(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit=False, flock=True) thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.startApplication() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo')
def shutdown(args): """ Command for shutting down the computer after the current snapshot has finished. Args: args (argparse.Namespace): previously parsed arguments Raises: SystemExit: 0 if successful; 1 if it failed either because there is no active snapshot for this profile or shutdown is not supported. """ setQuiet(args) printHeader() cfg = getConfig(args) sd = tools.ShutDown() if not sd.canShutdown(): logger.warning('Shutdown is not supported.') sys.exit(RETURN_ERR) instance = ApplicationInstance(cfg.takeSnapshotInstanceFile(), False) profile = '='.join((cfg.currentProfile(), cfg.profileName())) if not instance.busy(): logger.info( 'There is no active snapshot for profile %s. Skip shutdown.' % profile) sys.exit(RETURN_ERR) print( 'Shutdown is waiting for the snapshot in profile %s to end.\nPress CTRL+C to interrupt shutdown.\n' % profile) sd.activate_shutdown = True try: while instance.busy(): logger.debug('Snapshot is still active. Wait for shutdown.') sleep(5) except KeyboardInterrupt: print('Shutdown interrupted.') else: logger.info('Shutdown now.') sd.shutdown() sys.exit(RETURN_OK)
def test_auto_exit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True) self.assertTrue(os.path.exists(self.file_name)) this_pid = os.getpid() this_procname = tools.process_name(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname))
def shutdown(args): """ Command for shutting down the computer after the current snapshot has finished. Args: args (argparse.Namespace): previously parsed arguments Raises: SystemExit: 0 if successful; 1 if it failed either because there is no active snapshot for this profile or shutdown is not supported. """ setQuiet(args) printHeader() cfg = getConfig(args) sd = tools.ShutDown() if not sd.canShutdown(): logger.warning('Shutdown is not supported.') sys.exit(RETURN_ERR) instance = ApplicationInstance(cfg.takeSnapshotInstanceFile(), False) profile = '='.join((cfg.currentProfile(), cfg.profileName())) if not instance.busy(): logger.info('There is no active snapshot for profile %s. Skip shutdown.' %profile) sys.exit(RETURN_ERR) print('Shutdown is waiting for the snapshot in profile %s to end.\nPress CTRL+C to interrupt shutdown.\n' %profile) sd.activate_shutdown = True try: while instance.busy(): logger.debug('Snapshot is still active. Wait for shutdown.') sleep(5) except KeyboardInterrupt: print('Shutdown interrupted.') else: logger.info('Shutdown now.') sd.shutdown() sys.exit(RETURN_OK)
def test_auto_exit_other_running_process(self): pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) with self.assertRaises(SystemExit): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True)
def write_after_flock( self, pid_file, ): inst = ApplicationInstance(os.path.abspath(pid_file), False) inst.flockExclusiv() with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock()
def test_auto_flock(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = False, flock = True) thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.start_application() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo')
def setUp(self): logger.DEBUG = '-v' in sys.argv self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None
class Daemon: """ A generic daemon class. Usage: subclass the Daemon class and override the run() method Daemon Copyright by Sander Marechal License CC BY-SA 3.0 http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ """ def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/stdout', stderr='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile self.appInstance = ApplicationInstance(pidfile, auto_exit = False, flock = False) def daemonize(self): """ do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 """ logger.debug('start', self) try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError as e: logger.error("fork #1 failed: %d (%s)" % (e.errno, str(e)), self) sys.exit(1) # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent sys.exit(0) except OSError as e: logger.error("fork #2 failed: %d (%s)" % (e.errno, str(e)), self) sys.exit(1) # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() si = open(self.stdin, 'r') so = open(self.stdout, 'w') se = open(self.stderr, 'w') os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) # write pidfile logger.debug('write pidfile', self) atexit.register(self.appInstance.exit_application) signal.signal(signal.SIGTERM, self._cleanup_handler) self.appInstance.start_application() def _cleanup_handler(self, signum, frame): self.fifo.delfifo() self.appInstance.exit_application() sys.exit(0) def start(self): """ Start the daemon """ # Check for a pidfile to see if the daemon already runs if not self.appInstance.check(): message = "pidfile %s already exist. Daemon already running?\n" logger.error(message % self.pidfile, self) sys.exit(1) # Start the daemon self.daemonize() self.run() def stop(self): """ Stop the daemon """ # Get the pid from the pidfile pid, procname = self.appInstance.readPidFile() if not pid: message = "pidfile %s does not exist. Daemon not running?\n" logger.error(message % self.pidfile, self) return # not an error in a restart # Try killing the daemon process try: while True: os.kill(pid, signal.SIGTERM) time.sleep(0.1) except OSError as err: if err.errno == errno.ESRCH: #no such process self.appInstance.exit_application() else: logger.error(str(err), self) sys.exit(1) def restart(self): """ Restart the daemon """ self.stop() self.start() def reload(self): """ send SIGHUP signal to process """ # Get the pid from the pidfile pid, procname = self.appInstance.readPidFile() if not pid: message = "pidfile %s does not exist. Daemon not running?\n" logger.error(message % self.pidfile, self) return # Try killing the daemon process try: os.kill(pid, signal.SIGHUP) except OSError as err: if err.errno == errno.ESRCH: #no such process self.appInstance.exit_application() else: sys.stderr.write(str(err)) sys.exit(1) def status(self): """ return status """ return not self.appInstance.check() def run(self): """ You should override this method when you subclass Daemon. It will be called after the process has been daemonized by start() or restart(). """ pass
class TestApplicationInstance(unittest.TestCase): def setUp(self): logger.DEBUG = '-v' in sys.argv self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None def tearDown(self): for f in (self.temp_file, self.file_name): if os.path.exists(f): os.remove(f) if self.subproc: self.subproc.kill() self.subproc = None def createProcess(self): with open(self.temp_file, 'wt') as output: self.subproc = subprocess.Popen("top", stdout=output) return self.subproc.pid def getProcName(self, pid): with open('/proc/%s/cmdline' % pid, 'r') as file: return file.read().strip('\n') def test_create_and_remove_pid_file(self): #create pid file self.inst.start_application() self.assertTrue(os.path.isfile(self.file_name)) #remove pid file self.inst.exit_application() self.assertFalse(os.path.isfile(self.file_name)) def test_write_pid_file(self): self.inst.start_application() #get pid/procname of current process this_pid = os.getpid() this_procname = self.getProcName(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '%s\n%s' %(this_pid, this_procname)) def test_existing_process_with_correct_procname(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) def test_existing_process_with_wrong_procname(self): """ Test the check function with an existing process with wrong process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname + "DELETE") # Execute test self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_pid(self): """ Test the check function with an existing process with wrong pid """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write(procname) # Execute test self.assertTrue(self.inst.check()) def test_killing_existing_process(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) #kill process self.subproc.kill() self.subproc.wait() self.subproc = None # Execute test self.assertTrue(self.inst.check()) def test_non_existing_process(self): """ Test the check function with a non existing process """ # GIVE # # create file with fake pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write("FAKE_PROCNAME") # Execute test self.assertTrue(self.inst.check()) def test_leftover_empty_lockfile(self): with open(self.file_name, 'wt')as f: pass self.assertTrue(self.inst.check()) def write_after_flock(self, pid_file,): inst = ApplicationInstance(os.path.abspath(pid_file), False) inst.flockExclusiv() with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock() def test_thread_write_without_flock(self): thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_flock_exclusive(self): self.inst.flockExclusiv() thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #give the thread some time sleep(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.flockUnlock() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo')
import os, sys pathname = os.path.dirname(sys.argv[0]) sys.path.append(os.path.abspath(pathname)) sys.path.append( os.path.normpath(os.path.join(os.path.abspath(pathname), '../'))) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from applicationinstance import ApplicationInstance from places.models import Currency import logging log = logging.getLogger('genel') if __name__ == '__main__': o = None inst = None try: inst = ApplicationInstance('/tmp/update_currency.pid') Currency.updateRates() if inst: inst.exitApplication() except SystemExit: if inst: inst.exitApplication() pass except: if inst: inst.exitApplication() log.exception('beklenmeyen hata')
# def update_place_status(self, p): # translated_langs = p.get_translation_list(reset=True) # if translated_langs: #TODO: en az bir dil oldugu icin bu herzaman True oluyor. # status = p.translation_status # p.translation_status = 20 # if all([ l in translated_langs for l in self.auto_langs ] ): # p.translation_status = 30 # if status != p.translation_status : # p.save() if __name__ == '__main__': o = None inst = None try: inst = ApplicationInstance('/tmp/gtranslate.pid') o = TranslationMachine() o.run() if inst: inst.exitApplication() except SystemExit: if inst: inst.exitApplication() pass except: if inst: inst.exitApplication() log.exception('beklenmeyen hata')
class TestApplicationInstance(unittest.TestCase): def setUp(self): logger.DEBUG = '-v' in sys.argv self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None def tearDown(self): for f in (self.temp_file, self.file_name): if os.path.exists(f): os.remove(f) self.killProcess() def createProcess(self): dummy = 'dummy_proc.sh' dummyPath = os.path.join(os.path.dirname(__file__), dummy) self.subproc = subprocess.Popen(dummyPath) return self.subproc.pid def killProcess(self): if self.subproc: self.subproc.kill() self.subproc.wait() self.subproc = None def test_create_and_remove_pid_file(self): #create pid file self.inst.start_application() self.assertTrue(os.path.isfile(self.file_name)) #remove pid file self.inst.exit_application() self.assertFalse(os.path.isfile(self.file_name)) def test_write_pid_file(self): self.inst.start_application() #get pid/procname of current process this_pid = os.getpid() this_procname = tools.process_name(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname)) def test_existing_process_with_correct_procname(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) def test_existing_process_with_correct_proc_cmdline(self): """ Test the check function with an existing process with correct process cmdline (for backwards compatibility) """ pid = self.createProcess() procname = tools.process_cmdline(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) def test_no_pid_file(self): self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_procname(self): """ Test the check function with an existing process with wrong process name """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname + "DELETE") # Execute test self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_pid(self): """ Test the check function with an existing process with wrong pid """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write(procname) # Execute test self.assertTrue(self.inst.check()) def test_killing_existing_process(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) self.assertFalse(self.inst.check()) self.killProcess() # Execute test self.assertTrue(self.inst.check()) def test_non_existing_process(self): """ Test the check function with a non existing process """ # GIVE # # create file with fake pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write("FAKE_PROCNAME") # Execute test self.assertTrue(self.inst.check()) def test_leftover_empty_lockfile(self): with open(self.file_name, 'wt')as f: pass self.assertTrue(self.inst.check()) def write_after_flock(self, pid_file): inst = ApplicationInstance(os.path.abspath(pid_file), auto_exit = False, flock = True) with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock() def test_thread_write_without_flock(self): thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_flock_exclusive(self): self.inst.flockExclusiv() thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.flockUnlock() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_auto_flock(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = False, flock = True) thread = Thread(target = self.write_after_flock, args = (self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.start_application() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_auto_exit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True) self.assertTrue(os.path.exists(self.file_name)) this_pid = os.getpid() this_procname = tools.process_name(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname)) def test_auto_exit_other_running_process(self): pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) with self.assertRaises(SystemExit): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True)
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/stdout', stderr='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile self.appInstance = ApplicationInstance(pidfile, auto_exit = False, flock = False)
class TestApplicationInstance(unittest.TestCase): def setUp(self): logger.DEBUG = '-v' in sys.argv self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None def tearDown(self): for f in (self.temp_file, self.file_name): if os.path.exists(f): os.remove(f) if self.subproc: self.subproc.kill() self.subproc = None def createProcess(self): with open(self.temp_file, 'wt') as output: self.subproc = subprocess.Popen("top", stdout=output) return self.subproc.pid def getProcName(self, pid): with open('/proc/%s/cmdline' % pid, 'r') as file: return file.read().strip('\n') def test_create_and_remove_pid_file(self): #create pid file self.inst.start_application() self.assertTrue(os.path.isfile(self.file_name)) #remove pid file self.inst.exit_application() self.assertFalse(os.path.isfile(self.file_name)) def test_write_pid_file(self): self.inst.start_application() #get pid/procname of current process this_pid = os.getpid() this_procname = self.getProcName(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '%s\n%s' % (this_pid, this_procname)) def test_existing_process_with_correct_procname(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) def test_existing_process_with_wrong_procname(self): """ Test the check function with an existing process with wrong process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname + "DELETE") # Execute test self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_pid(self): """ Test the check function with an existing process with wrong pid """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write(procname) # Execute test self.assertTrue(self.inst.check()) def test_killing_existing_process(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = self.getProcName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) #kill process self.subproc.kill() self.subproc.wait() self.subproc = None # Execute test self.assertTrue(self.inst.check()) def test_non_existing_process(self): """ Test the check function with a non existing process """ # GIVE # # create file with fake pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write("FAKE_PROCNAME") # Execute test self.assertTrue(self.inst.check()) def test_leftover_empty_lockfile(self): with open(self.file_name, 'wt') as f: pass self.assertTrue(self.inst.check()) def write_after_flock( self, pid_file, ): inst = ApplicationInstance(os.path.abspath(pid_file), False) inst.flockExclusiv() with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock() def test_thread_write_without_flock(self): thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_flock_exclusive(self): self.inst.flockExclusiv() thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #give the thread some time sleep(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.flockUnlock() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo')
def write_after_flock(self, pid_file,): inst = ApplicationInstance(os.path.abspath(pid_file), False) inst.flockExclusiv() with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock()
class TestApplicationInstance(generic.TestCase): def setUp(self): super(TestApplicationInstance, self).setUp() self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None def tearDown(self): super(TestApplicationInstance, self).tearDown() for f in (self.temp_file, self.file_name): if os.path.exists(f): os.remove(f) self.killProcess() def createProcess(self): dummyPath = os.path.join(os.path.dirname(__file__), generic.DUMMY) self.subproc = subprocess.Popen(dummyPath) return self.subproc.pid def killProcess(self): if self.subproc: self.subproc.kill() self.subproc.wait() self.subproc = None def test_create_and_remove_pid_file(self): #create pid file self.inst.startApplication() self.assertTrue(os.path.isfile(self.file_name)) #remove pid file self.inst.exitApplication() self.assertFalse(os.path.isfile(self.file_name)) def test_write_pid_file(self): self.inst.startApplication() #get pid/procname of current process this_pid = os.getpid() this_procname = tools.processName(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname)) @patch('builtins.open') def test_write_pid_fail(self, mock_open): mock_open.side_effect = OSError() self.inst.startApplication() def test_existing_process_with_correct_procname(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) self.assertTrue(self.inst.busy()) def test_existing_process_with_correct_proc_cmdline(self): """ Test the check function with an existing process with correct process cmdline (for backwards compatibility) """ pid = self.createProcess() procname = tools.processCmdline(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check()) def test_no_pid_file(self): self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_procname(self): """ Test the check function with an existing process with wrong process name """ pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname + "DELETE") # Execute test self.assertTrue(self.inst.check()) def test_existing_process_with_wrong_pid(self): """ Test the check function with an existing process with wrong pid """ pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write(procname) # Execute test self.assertTrue(self.inst.check()) def test_killing_existing_process(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) self.assertFalse(self.inst.check()) self.killProcess() # Execute test self.assertTrue(self.inst.check()) def test_non_existing_process(self): """ Test the check function with a non existing process """ # GIVE # # create file with fake pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write("FAKE_PROCNAME") # Execute test self.assertTrue(self.inst.check()) def test_leftover_empty_lockfile(self): with open(self.file_name, 'wt') as f: pass self.assertTrue(self.inst.check()) def write_after_flock(self, pid_file): inst = ApplicationInstance(os.path.abspath(pid_file), autoExit=False, flock=True) with open(self.temp_file, 'wt') as f: f.write('foo') inst.flockUnlock() def test_thread_write_without_flock(self): thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_flock_exclusive(self): self.inst.flockExclusiv() thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.flockUnlock() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') @patch('builtins.open') def test_flock_exclusive_fail(self, mock_open): mock_open.side_effect = OSError() self.inst.flockExclusiv() def test_auto_flock(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit=False, flock=True) thread = Thread(target=self.write_after_flock, args=(self.file_name, )) thread.start() #give the thread some time thread.join(0.01) self.assertFalse(os.path.exists(self.temp_file)) self.inst.startApplication() #wait for the thread to finish thread.join() self.assertTrue(os.path.exists(self.temp_file)) with open(self.temp_file, 'rt') as f: self.assertEqual(f.read(), 'foo') def test_autoExit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit=True) self.assertTrue(os.path.exists(self.file_name)) this_pid = os.getpid() this_procname = tools.processName(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname)) def test_autoExit_other_running_process(self): pid = self.createProcess() procname = tools.processName(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) with self.assertRaises(SystemExit): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit=True) def test_readPidFile(self): with open(self.file_name, "wt") as f: f.write('123\nfoo') self.assertEqual(self.inst.readPidFile(), (123, 'foo')) # ValueError with open(self.file_name, "wt") as f: f.write('foo\nbar') self.assertEqual(self.inst.readPidFile(), (0, 'bar')) @patch('builtins.open') def test_readPidFile_fail(self, mock_open): mock_open.side_effect = OSError() self.assertEqual(self.inst.readPidFile(), (0, ''))
import logging log = logging.getLogger('genel') if __name__ == '__main__': o = None inst = None try: inst = ApplicationInstance( '/tmp/update_currency.pid' ) Currency.updateRates() if inst: inst.exitApplication() except SystemExit: if inst: inst.exitApplication() pass except: if inst: inst.exitApplication() log.exception('beklenmeyen hata')
def setUp(self): super(TestApplicationInstance, self).setUp() self.temp_file = '/tmp/temp.txt' self.file_name = "/tmp/file_with_pid" self.inst = ApplicationInstance(os.path.abspath(self.file_name), False) self.subproc = None