2018-12-14 08:38:53 +00:00
|
|
|
"""Some helper functions that don't fit anywhere else."""
|
2010-12-11 18:32:40 -08:00
|
|
|
import os, errno, fcntl
|
2010-11-12 05:24:46 -08:00
|
|
|
|
|
|
|
|
|
2019-01-18 00:06:18 +00:00
|
|
|
class ImmediateReturn(Exception):
|
|
|
|
|
def __init__(self, rv):
|
|
|
|
|
Exception.__init__(self, "immediate return with exit code %d" % rv)
|
|
|
|
|
self.rv = rv
|
2010-12-19 01:38:38 -08:00
|
|
|
|
|
|
|
|
|
2010-11-12 05:24:46 -08:00
|
|
|
def unlink(f):
|
|
|
|
|
"""Delete a file at path 'f' if it currently exists.
|
|
|
|
|
|
|
|
|
|
Unlike os.unlink(), does not throw an exception if the file didn't already
|
|
|
|
|
exist.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
os.unlink(f)
|
2019-10-27 14:19:50 +01:00
|
|
|
except OSError as e:
|
2010-11-12 05:24:46 -08:00
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
|
pass # it doesn't exist, that's what you asked for
|
|
|
|
|
|
|
|
|
|
|
2010-11-22 03:21:17 -08:00
|
|
|
def close_on_exec(fd, yes):
|
|
|
|
|
fl = fcntl.fcntl(fd, fcntl.F_GETFD)
|
|
|
|
|
fl &= ~fcntl.FD_CLOEXEC
|
|
|
|
|
if yes:
|
|
|
|
|
fl |= fcntl.FD_CLOEXEC
|
|
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFD, fl)
|
2019-01-18 00:06:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def fd_exists(fd):
|
|
|
|
|
try:
|
|
|
|
|
fcntl.fcntl(fd, fcntl.F_GETFD)
|
|
|
|
|
except IOError:
|
|
|
|
|
return False
|
|
|
|
|
return True
|