Created
December 27, 2015 10:37
-
-
Save dustindorroh/ab5f863d19445049ee9a to your computer and use it in GitHub Desktop.
Wraps various os methods to be more forgiving.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Wraps various os methods to be more forgiving. | |
:since: Dec 22, 2015 | |
:author: Dustin Dorroh <[email protected]> | |
''' | |
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