##// END OF EJS Templates
pidlock: use fstring where aplicable
super-admin -
r5021:a562499c default
parent child Browse files
Show More
@@ -24,7 +24,6 b' import errno'
24 from multiprocessing.util import Finalize
24 from multiprocessing.util import Finalize
25
25
26
26
27
28 class LockHeld(Exception):
27 class LockHeld(Exception):
29 pass
28 pass
30
29
@@ -49,7 +48,7 b' class DaemonLock(object):'
49 self.desc = desc
48 self.desc = desc
50 self.debug = debug
49 self.debug = debug
51 self.held = False
50 self.held = False
52 #run the lock automatically !
51 # run the lock automatically !
53 self.lock()
52 self.lock()
54 self._finalize = Finalize(self, DaemonLock._on_finalize,
53 self._finalize = Finalize(self, DaemonLock._on_finalize,
55 args=(self, debug), exitpriority=10)
54 args=(self, debug), exitpriority=10)
@@ -58,7 +57,7 b' class DaemonLock(object):'
58 def _on_finalize(lock, debug):
57 def _on_finalize(lock, debug):
59 if lock.held:
58 if lock.held:
60 if debug:
59 if debug:
61 print('leck held finilazing and running lock.release()')
60 print('lock held finalazing and running lock.release()')
62 lock.release()
61 lock.release()
63
62
64 def lock(self):
63 def lock(self):
@@ -66,7 +65,7 b' class DaemonLock(object):'
66 locking function, if lock is present it
65 locking function, if lock is present it
67 will raise LockHeld exception
66 will raise LockHeld exception
68 """
67 """
69 lockname = '%s' % (os.getpid())
68 lockname = f'{os.getpid()}'
70 if self.debug:
69 if self.debug:
71 print('running lock')
70 print('running lock')
72 self.trylock()
71 self.trylock()
@@ -85,8 +84,8 b' class DaemonLock(object):'
85 running_pid = -1
84 running_pid = -1
86
85
87 if self.debug:
86 if self.debug:
88 print('lock file present running_pid: %s, '
87 print(f'lock file present running_pid: {running_pid}, '
89 'checking for execution' % (running_pid,))
88 f'checking for execution')
90 # Now we check the PID from lock file matches to the current
89 # Now we check the PID from lock file matches to the current
91 # process PID
90 # process PID
92 if running_pid:
91 if running_pid:
@@ -94,15 +93,14 b' class DaemonLock(object):'
94 os.kill(running_pid, 0)
93 os.kill(running_pid, 0)
95 except OSError as exc:
94 except OSError as exc:
96 if exc.errno in (errno.ESRCH, errno.EPERM):
95 if exc.errno in (errno.ESRCH, errno.EPERM):
97 print("Lock File is there but"
96 print("Lock File is there but the program is not running")
98 " the program is not running")
97 print(f"Removing lock file for the: {running_pid}")
99 print("Removing lock file for the: %s" % running_pid)
100 self.release()
98 self.release()
101 else:
99 else:
102 raise
100 raise
103 else:
101 else:
104 print("You already have an instance of the program running")
102 print("You already have an instance of the program running")
105 print("It is running as process %s" % running_pid)
103 print(f"It is running as process {running_pid}")
106 raise LockHeld()
104 raise LockHeld()
107
105
108 except IOError as e:
106 except IOError as e:
@@ -118,16 +116,16 b' class DaemonLock(object):'
118 if self.callbackfn:
116 if self.callbackfn:
119 # execute callback function on release
117 # execute callback function on release
120 if self.debug:
118 if self.debug:
121 print('executing callback function %s' % self.callbackfn)
119 print(f'executing callback function {self.callbackfn}')
122 self.callbackfn()
120 self.callbackfn()
123 try:
121 try:
124 if self.debug:
122 if self.debug:
125 print('removing pidfile %s' % self.pidfile)
123 print(f'removing pidfile {self.pidfile}')
126 os.remove(self.pidfile)
124 os.remove(self.pidfile)
127 self.held = False
125 self.held = False
128 except OSError as e:
126 except OSError as e:
129 if self.debug:
127 if self.debug:
130 print('removing pidfile failed %s' % e)
128 print(f'removing pidfile failed {e}')
131 pass
129 pass
132
130
133 def makelock(self, lockname, pidfile):
131 def makelock(self, lockname, pidfile):
@@ -138,7 +136,7 b' class DaemonLock(object):'
138 :param pidfile: the file to write the pid in
136 :param pidfile: the file to write the pid in
139 """
137 """
140 if self.debug:
138 if self.debug:
141 print('creating a file %s and pid: %s' % (pidfile, lockname))
139 print(f'creating a file {lockname} and pid: {pidfile}')
142
140
143 dir_, file_ = os.path.split(pidfile)
141 dir_, file_ = os.path.split(pidfile)
144 if not os.path.isdir(dir_):
142 if not os.path.isdir(dir_):
General Comments 0
You need to be logged in to leave comments. Login now