Skip to content

Instantly share code, notes, and snippets.

@dustindorroh
Created December 27, 2015 10:37
Show Gist options
  • Save dustindorroh/ab5f863d19445049ee9a to your computer and use it in GitHub Desktop.
Save dustindorroh/ab5f863d19445049ee9a to your computer and use it in GitHub Desktop.
Wraps various os methods to be more forgiving.
'''
Wraps various os methods to be more forgiving.
:since: Dec 22, 2015
:author: Dustin Dorroh <dustin.dorroh@decisionsciencescorp.com>
'''
import os
import stat
import errno
from functools import wraps
def OSErrorCatch(f,err=errno.EEXIST):
'''
Wraps a function and handles a select OSError exception silently.
:params:
f:
function to wrap
err:
error to handle silently. Default errno.EEXIST
:return:
wrapped function
'''
@wraps(f)
def wrapper(*args, **kwds):
try:
return f(*args, **kwds)
except OSError as oserr:
if oserr.errno != err:
raise oserr
return wrapper
makedirs = OSErrorCatch(os.makedirs)
symlink = OSErrorCatch(os.symlink)
def make_executable(path):
'''
Make a path executable using os.chmod
:param:
path:
path to make executable
'''
os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment