Skip to content

Commit e772ce7

Browse files
authored
Merge pull request #80 from stdweird/no_close_on_report
handle fpy3 file exception
2 parents 2f2a18b + ad11de2 commit e772ce7

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

lib/vsc/utils/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import time
3535

3636
from vsc.utils import fancylogger
37-
from vsc.utils.py2vs3 import pickle
37+
from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc
3838

3939

4040
class FileCache(object):
@@ -82,7 +82,7 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
8282
try:
8383
g = gzip.GzipFile(mode='rb', fileobj=f) # no context manager available in python 26 yet
8484
s = g.read()
85-
except IOError as err:
85+
except (IOError) as err:
8686
self.log.error("Cannot load data from cache file %s as gzipped json", self.filename)
8787
try:
8888
f.seek(0)
@@ -106,7 +106,7 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
106106
finally:
107107
g.close()
108108

109-
except (OSError, IOError, ValueError) as err:
109+
except (OSError, IOError, ValueError, FileNotFoundErrorExc) as err:
110110
self.log.warning("Could not access the file cache at %s [%s]", self.filename, err)
111111
self.shelf = {}
112112
self.log.info("Cache in %s starts with an empty shelf", (self.filename,))

lib/vsc/utils/nagios.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
from vsc.utils.cache import FileCache
5454
from vsc.utils.fancylogger import getLogger
55-
from vsc.utils.py2vs3 import is_string
55+
from vsc.utils.py2vs3 import is_string, FileNotFoundErrorExc
5656

5757
log = getLogger(__name__)
5858

@@ -316,7 +316,7 @@ def cache(self, nagios_exit, nagios_message):
316316
else:
317317
self.log.warn("Not running as root: Cannot chown the nagios check file %s to %s",
318318
self.filename, self.nagios_username)
319-
except OSError:
319+
except (OSError, FileNotFoundErrorExc):
320320
self.log.raiseException("Cannot chown the nagios check file %s to the nagios user" % (self.filename))
321321

322322
return True

lib/vsc/utils/pickle_files.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@
3535
@author: Stijn De Weirdt (Ghent University)
3636
"""
3737

38+
import logging
3839
import os
3940
import stat
4041

41-
from vsc.utils import fancylogger
42-
from vsc.utils.py2vs3 import pickle
42+
43+
from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc
4344

4445

4546
class TimestampPickle(object):
4647
"""Stores a timestamp in some format in a file."""
4748

4849
def __init__(self, filename):
4950
self.filename = filename
50-
self.log = fancylogger.getLogger(self.__class__.__name__)
5151

5252
def read(self):
5353
"""Read a timestamp value from a pickled file.
@@ -61,8 +61,8 @@ def read(self):
6161

6262
try:
6363
timestamp = pickle.load(open(self.filename, "rb"))
64-
except (IOError, OSError):
65-
self.log.exception("Failed to load timestamp pickle from filename %s.", self.filename)
64+
except (IOError, OSError, FileNotFoundErrorExc):
65+
logging.exception("Failed to load timestamp pickle from filename %s.", self.filename)
6666
return None
6767

6868
return timestamp
@@ -80,11 +80,11 @@ def write(self, timestamp):
8080
try:
8181
pickle.dump(timestamp, open(self.filename, "wb"))
8282
except Exception:
83-
self.log.exception("Failed to dump timestamp %s to pickle in filename %s", timestamp, self.filename)
83+
logging.exception("Failed to dump timestamp %s to pickle in filename %s", timestamp, self.filename)
8484
raise
8585

8686
try:
8787
os.chmod(self.filename, stat.S_IRWXU)
8888
except Exception:
89-
self.log.exception("Failed to set permissions on filename %s", self.filename)
89+
logging.exception("Failed to set permissions on filename %s", self.filename)
9090
raise

lib/vsc/utils/timestamp_pid_lockfile.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
@author: Andy Georges (Ghent University)
3333
"""
3434
import errno
35+
import logging
3536
import os
3637
import signal
3738
import time
3839

3940
from lockfile.linklockfile import LockBase, LockFailed, NotLocked, NotMyLock
40-
41-
from vsc.utils import fancylogger
42-
41+
from vsc.utils.py2vs3 import FileNotFoundErrorExc, FileExistsErrorExc
4342

4443
class LockFileReadError(Exception):
4544
'''Exception raised when we cannot get the expected information from the lock file.'''
@@ -54,7 +53,6 @@ def __init__(self, path, threshold=60):
5453
'''Intializer.'''
5554
LockBase.__init__(self, path, False)
5655
self.threshold = threshold
57-
self.logger = fancylogger.getLogger(self.__class__.__name__)
5856

5957
def read_pid_timestamp(self):
6058
'''Obtain the PID and timestamp from the lockfile.
@@ -84,24 +82,24 @@ def acquire(self, timeout=None):
8482
timeout = self.threshold
8583
try:
8684
_write_pid_timestamp_file(self.path)
87-
self.logger.info('Obtained lock on timestamped pid lockfile %s', self.path)
88-
except OSError as err:
85+
logging.info('Obtained lock on timestamped pid lockfile %s', self.path)
86+
except (OSError, FileNotFoundErrorExc, FileExistsErrorExc) as err:
8987
doraise = True
9088
if err.errno == errno.EEXIST:
9189
## Check if the timestamp is older than the threshold
9290
(pid, timestamp) = _read_pid_timestamp_file(self.path)
9391
if time.time() - timestamp > timeout:
9492
_find_and_kill(pid)
9593
os.unlink(self.path)
96-
self.logger.warning('Obsolete lockfile detected at %s: pid = %d, timestamp = %s',
97-
self.path, pid, time.ctime(timestamp))
94+
logging.warning('Obsolete lockfile detected at %s: pid = %d, timestamp = %s',
95+
self.path, pid, time.ctime(timestamp))
9896
try:
9997
_write_pid_timestamp_file(self.path)
10098
doraise = False
10199
except Exception:
102100
pass
103101
if doraise:
104-
self.logger.error('Unable to obtain lock on %s: %s', self.path, err)
102+
logging.error('Unable to obtain lock on %s: %s', self.path, err)
105103
raise LockFailed
106104

107105
def release(self):
@@ -110,10 +108,10 @@ def release(self):
110108
Remove the lockfile to indicate the lock was released.
111109
'''
112110
if not self.is_locked():
113-
self.logger.error('Trying to release a lock that does not exist at %s.', self.path)
111+
logging.error('Trying to release a lock that does not exist at %s.', self.path)
114112
raise NotLocked
115113
if not self.i_am_locking():
116-
self.logger.error('Trying to release a lock the current process is not holding at %s', self.path)
114+
logging.error('Trying to release a lock the current process is not holding at %s', self.path)
117115
raise NotMyLock
118116
os.remove(self.path)
119117

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from vsc.install.shared_setup import ag, sdw
3737

3838
install_requires = [
39-
'vsc-base >= 3.0.2',
39+
'vsc-base >= 3.2.4',
4040
'lockfile >= 0.9.1',
4141
'netifaces',
4242
'jsonpickle',
@@ -54,7 +54,7 @@
5454

5555

5656
PACKAGE = {
57-
'version': '2.1.9',
57+
'version': '2.1.10',
5858
'author': [ag, sdw],
5959
'maintainer': [ag, sdw],
6060
'excluded_pkgs_rpm': ['vsc', 'vsc.utils'], # vsc is default, vsc.utils is provided by vsc-base

0 commit comments

Comments
 (0)