def checkLocks(self, path, lockSuffix): """ Check if there are active locks ending with ``lockSuffix`` in ``path``. If the process owning the lock doesn't exist anymore this will remove the lock. Args: path (str): full path to lock directory lockSuffix (str): last part of locks name Returns: bool: ``True`` if there are active locks in ``path`` """ for f in os.listdir(path): if not f[-len(lockSuffix):] == lockSuffix: continue is_tmp = os.path.basename( f)[-len(lockSuffix) - len('.tmp'):-len(lockSuffix)] == '.tmp' if is_tmp: lock_pid = os.path.basename(f)[:-len('.tmp') - len(lockSuffix)] else: lock_pid = os.path.basename(f)[:-len(lockSuffix)] if lock_pid == self.pid: if is_tmp == self.tmp_mount: continue if tools.processAlive(int(lock_pid)): return True else: logger.debug('Remove old and invalid lock %s' % f, self) #clean up os.remove(os.path.join(path, f)) for symlink in os.listdir(self.mount_root): if symlink.endswith('_%s' % lock_pid): os.remove(os.path.join(self.mount_root, symlink)) return False
def checkLocks(self, path, lockSuffix): """ Check if there are active locks ending with ``lockSuffix`` in ``path``. If the process owning the lock doesn't exist anymore this will remove the lock. Args: path (str): full path to lock directory lockSuffix (str): last part of locks name Returns: bool: ``True`` if there are active locks in ``path`` """ for f in os.listdir(path): if not f[-len(lockSuffix):] == lockSuffix: continue is_tmp = os.path.basename(f)[-len(lockSuffix)-len('.tmp'):-len(lockSuffix)] == '.tmp' if is_tmp: lock_pid = os.path.basename(f)[:-len('.tmp')-len(lockSuffix)] else: lock_pid = os.path.basename(f)[:-len(lockSuffix)] if lock_pid == self.pid: if is_tmp == self.tmp_mount: continue if tools.processAlive(int(lock_pid)): return True else: logger.debug('Remove old and invalid lock %s' %f, self) #clean up os.remove(os.path.join(path, f)) for symlink in os.listdir(self.mount_root): if symlink.endswith('_%s' % lock_pid): os.remove(os.path.join(self.mount_root, symlink)) return False
def test_processAlive(self): """ Test the function processAlive """ #TODO: add test (in chroot) running proc as root and kill as non-root self.assertTrue(tools.processAlive(os.getpid())) pid = self.createProcess() self.assertTrue(tools.processAlive(pid)) self.killProcess() self.assertFalse(tools.processAlive(pid)) self.assertFalse(tools.processAlive(999999)) with self.assertRaises(ValueError): tools.processAlive(0) self.assertFalse(tools.processAlive(-1))
def updateInfo(self): if not tools.processAlive(self.ppid): self.prepairExit() self.qapp.exit(0) return paused = tools.processPaused(self.snapshots.pid()) self.btnPause.setVisible(not paused) self.btnResume.setVisible(paused) message = self.snapshots.takeSnapshotMessage() if message is None and self.last_message is None: message = (0, _('Working...')) if not message is None: if message != self.last_message: self.last_message = message if self.decode: message = (message[0], self.decode.log(message[1])) self.menuStatusMessage.setText('\n'.join(tools.wrapLine(message[1],\ size = 80,\ delimiters = '',\ new_line_indicator = '') \ )) self.status_icon.setToolTip(message[1]) pg = progress.ProgressFile(self.config) if pg.fileReadable(): pg.load() percent = pg.intValue('percent') if percent != self.progressBar.value(): self.progressBar.setValue(percent) self.progressBar.render(self.pixmap, sourceRegion=QRegion(0, -14, 24, 6), flags=QWidget.RenderFlags( QWidget.DrawChildren)) self.status_icon.setIcon(QIcon(self.pixmap)) self.menuProgress.setText(' | '.join(self.getMenuProgress(pg))) self.menuProgress.setVisible(True) else: self.status_icon.setIcon(self.icon.BIT_LOGO) self.menuProgress.setVisible(False)
def check(self, autoExit=False): """ Check if the current application is already running Args: autoExit (bool): automatically call sys.exit if there is an other instance running Returns: bool: ``True`` if this is the only application instance """ #check if the pidfile exists if not os.path.isfile(self.pidFile): return True self.pid, self.procname = self.readPidFile() #check if the process with specified by pid exists if 0 == self.pid: return True if not tools.processAlive(self.pid): return True #check if the process has the same procname #check cmdline for backwards compatibility if self.procname and \ self.procname != tools.processName(self.pid) and \ self.procname != tools.processCmdline(self.pid): return True if autoExit: #exit the application print("The application is already running !") exit( 0 ) #exit raise an exception so don't put it in a try/except block return False
def updateInfo(self): if not tools.processAlive(self.ppid): self.prepairExit() self.qapp.exit(0) return paused = tools.processPaused(self.snapshots.pid()) self.btnPause.setVisible(not paused) self.btnResume.setVisible(paused) message = self.snapshots.takeSnapshotMessage() if message is None and self.last_message is None: message = (0, _('Working...')) if not message is None: if message != self.last_message: self.last_message = message if self.decode: message = (message[0], self.decode.log(message[1])) self.menuStatusMessage.setText('\n'.join(tools.wrapLine(message[1],\ size = 80,\ delimiters = '',\ new_line_indicator = '') \ )) self.status_icon.setToolTip(message[1]) pg = progress.ProgressFile(self.config) if pg.fileReadable(): pg.load() percent = pg.intValue('percent') if percent != self.progressBar.value(): self.progressBar.setValue(percent) self.progressBar.render(self.pixmap, sourceRegion = QRegion(0, -14, 24, 6), flags = QWidget.RenderFlags(QWidget.DrawChildren)) self.status_icon.setIcon(QIcon(self.pixmap)) self.menuProgress.setText(' | '.join(self.getMenuProgress(pg))) self.menuProgress.setVisible(True) else: self.status_icon.setIcon(self.icon.BIT_LOGO) self.menuProgress.setVisible(False)
def check(self, autoExit = False): """ Check if the current application is already running Args: autoExit (bool): automatically call sys.exit if there is an other instance running Returns: bool: ``True`` if this is the only application instance """ #check if the pidfile exists if not os.path.isfile(self.pidFile): return True self.pid, self.procname = self.readPidFile() #check if the process with specified by pid exists if 0 == self.pid: return True if not tools.processAlive(self.pid): return True #check if the process has the same procname #check cmdline for backwards compatibility if self.procname and \ self.procname != tools.processName(self.pid) and \ self.procname != tools.processCmdline(self.pid): return True if autoExit: #exit the application print("The application is already running !") exit(0) #exit raise an exception so don't put it in a try/except block return False