11#! /usr/bin/env python
22
3+ import fcntl
34import logging
45import os
56import sys
67import tempfile
78
8- if sys .platform != "win32" :
9- import fcntl
10-
119
1210class SingleInstanceException (BaseException ):
1311 pass
@@ -46,28 +44,13 @@ def __init__(self, flavor_id="", lockfile=""):
4644 logger .debug (f"SingleInstance lockfile: { self .lockfile } " )
4745
4846 def __enter__ (self ):
49- if sys .platform == "win32" :
50- try :
51- # file already exists, we try to remove (in case previous
52- # execution was interrupted)
53- if os .path .exists (self .lockfile ):
54- os .unlink (self .lockfile )
55- self .fd = os .open (self .lockfile , os .O_CREAT | os .O_EXCL | os .O_RDWR )
56- except OSError :
57- type , e , tb = sys .exc_info ()
58- if e .errno == 13 :
59- logger .error ("Another instance is already running, quitting." )
60- raise SingleInstanceException
61- print (e .errno )
62- raise
63- else : # non Windows
64- self .fp = open (self .lockfile , "w" )
65- self .fp .flush ()
66- try :
67- fcntl .lockf (self .fp , fcntl .LOCK_EX | fcntl .LOCK_NB )
68- except OSError :
69- logger .warning ("Another instance is already running, quitting." )
70- raise SingleInstanceException
47+ self .fp = open (self .lockfile , "w" )
48+ self .fp .flush ()
49+ try :
50+ fcntl .lockf (self .fp , fcntl .LOCK_EX | fcntl .LOCK_NB )
51+ except OSError :
52+ logger .warning ("Another instance is already running, quitting." )
53+ raise SingleInstanceException
7154 self .initialized = True
7255 return self
7356
@@ -77,15 +60,10 @@ def __exit__(self, exc_type, exc_value, exc_tb):
7760 if exc_value is not None :
7861 logger .warning ("Error: %s" % exc_value , exc_info = True )
7962 try :
80- if sys .platform == "win32" :
81- if hasattr (self , "fd" ):
82- os .close (self .fd )
83- os .unlink (self .lockfile )
84- else :
85- fcntl .lockf (self .fp , fcntl .LOCK_UN )
86- # os.close(self.fp)
87- if os .path .isfile (self .lockfile ):
88- os .unlink (self .lockfile )
63+ fcntl .lockf (self .fp , fcntl .LOCK_UN )
64+ # os.close(self.fp)
65+ if os .path .isfile (self .lockfile ):
66+ os .unlink (self .lockfile )
8967 except Exception as e :
9068 if logger :
9169 logger .warning (e )
0 commit comments