comparison rhodecode/lib/pidlock.py @ 1307:c1516b35f91d beta

pep8ify
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 03 May 2011 16:54:43 +0200
parents 07963dd1f0f1
children adcfbe0fb453
comparison
equal deleted inserted replaced
1306:6e1d24503383 1307:c1516b35f91d
1 import os, time 1 import os
2 import sys 2 import sys
3 import time
4 import errno
5
3 from warnings import warn 6 from warnings import warn
4 from multiprocessing.util import Finalize 7 from multiprocessing.util import Finalize
5 import errno
6 8
7 from rhodecode import __platform__, PLATFORM_WIN 9 from rhodecode import __platform__, PLATFORM_WIN
8 10
9 if __platform__ in PLATFORM_WIN: 11 if __platform__ in PLATFORM_WIN:
10 import ctypes 12 import ctypes
13
11 def kill(pid): 14 def kill(pid):
12 """kill function for Win32""" 15 """kill function for Win32"""
13 kernel32 = ctypes.windll.kernel32 16 kernel32 = ctypes.windll.kernel32
14 handle = kernel32.OpenProcess(1, 0, pid) 17 handle = kernel32.OpenProcess(1, 0, pid)
15 return (0 != kernel32.TerminateProcess(handle, 0)) 18 return (0 != kernel32.TerminateProcess(handle, 0))
16 19
17 else: 20 else:
18 kill = os.kill 21 kill = os.kill
19 22
20 class LockHeld(Exception):pass 23
24 class LockHeld(Exception):
25 pass
21 26
22 27
23 class DaemonLock(object): 28 class DaemonLock(object):
24 """daemon locking 29 """daemon locking
25 USAGE: 30 USAGE:
32 """ 37 """
33 38
34 def __init__(self, file=None, callbackfn=None, 39 def __init__(self, file=None, callbackfn=None,
35 desc='daemon lock', debug=False): 40 desc='daemon lock', debug=False):
36 41
37 self.pidfile = file if file else os.path.join(os.path.dirname(__file__), 42 self.pidfile = file if file else os.path.join(
38 'running.lock') 43 os.path.dirname(__file__),
44 'running.lock')
39 self.callbackfn = callbackfn 45 self.callbackfn = callbackfn
40 self.desc = desc 46 self.desc = desc
41 self.debug = debug 47 self.debug = debug
42 self.held = False 48 self.held = False
43 #run the lock automatically ! 49 #run the lock automatically !
50 if lock.held: 56 if lock.held:
51 if debug: 57 if debug:
52 print 'leck held finilazing and running lock.release()' 58 print 'leck held finilazing and running lock.release()'
53 lock.release() 59 lock.release()
54 60
55
56 def lock(self): 61 def lock(self):
57 """locking function, if lock is present it will raise LockHeld exception 62 """
63 locking function, if lock is present it
64 will raise LockHeld exception
58 """ 65 """
59 lockname = '%s' % (os.getpid()) 66 lockname = '%s' % (os.getpid())
60 if self.debug: 67 if self.debug:
61 print 'running lock' 68 print 'running lock'
62 self.trylock() 69 self.trylock()
73 running_pid = int(pidfile.readline()) 80 running_pid = int(pidfile.readline())
74 81
75 pidfile.close() 82 pidfile.close()
76 83
77 if self.debug: 84 if self.debug:
78 print 'lock file present running_pid: %s, checking for execution'\ 85 print ('lock file present running_pid: %s, '
79 % running_pid 86 'checking for execution') % running_pid
80 # Now we check the PID from lock file matches to the current 87 # Now we check the PID from lock file matches to the current
81 # process PID 88 # process PID
82 if running_pid: 89 if running_pid:
83 try: 90 try:
84 kill(running_pid, 0) 91 kill(running_pid, 0)
85 except OSError, exc: 92 except OSError, exc:
86 if exc.errno in (errno.ESRCH, errno.EPERM): 93 if exc.errno in (errno.ESRCH, errno.EPERM):
87 print "Lock File is there but the program is not running" 94 print ("Lock File is there but"
95 " the program is not running")
88 print "Removing lock file for the: %s" % running_pid 96 print "Removing lock file for the: %s" % running_pid
89 self.release() 97 self.release()
90 else: 98 else:
91 raise 99 raise
92 else: 100 else:
120 pass 128 pass
121 129
122 def makelock(self, lockname, pidfile): 130 def makelock(self, lockname, pidfile):
123 """ 131 """
124 this function will make an actual lock 132 this function will make an actual lock
133
125 :param lockname: acctual pid of file 134 :param lockname: acctual pid of file
126 :param pidfile: the file to write the pid in 135 :param pidfile: the file to write the pid in
127 """ 136 """
128 if self.debug: 137 if self.debug:
129 print 'creating a file %s and pid: %s' % (pidfile, lockname) 138 print 'creating a file %s and pid: %s' % (pidfile, lockname)