mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
56 lines
1.2 KiB
Python
56 lines
1.2 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import contextlib
|
|
import functools
|
|
import os
|
|
import os.path
|
|
import shutil
|
|
import sys
|
|
|
|
|
|
def memoize_by_cwd(func):
|
|
"""Memoize a function call based on os.getcwd()."""
|
|
@functools.wraps(func)
|
|
def wrapper(*args):
|
|
cwd = os.getcwd()
|
|
key = (cwd,) + args
|
|
try:
|
|
return wrapper._cache[key]
|
|
except KeyError:
|
|
ret = wrapper._cache[key] = func(*args)
|
|
return ret
|
|
|
|
wrapper._cache = {}
|
|
|
|
return wrapper
|
|
|
|
|
|
def entry(func):
|
|
"""Allows a function that has `argv` as an argument to be used as a
|
|
commandline entry. This will make the function callable using either
|
|
explicitly passed argv or defaulting to sys.argv[1:]
|
|
"""
|
|
@functools.wraps(func)
|
|
def wrapper(argv=None):
|
|
if argv is None:
|
|
argv = sys.argv[1:]
|
|
return func(argv)
|
|
return wrapper
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def clean_path_on_failure(path):
|
|
"""Cleans up the directory on an exceptional failure."""
|
|
try:
|
|
yield
|
|
except BaseException:
|
|
if os.path.exists(path):
|
|
shutil.rmtree(path)
|
|
raise
|
|
|
|
|
|
# TODO: asottile.contextlib this with a forward port of nested
|
|
@contextlib.contextmanager
|
|
def noop_context():
|
|
yield
|