mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-15 21:40:19 -06:00
Remove plumbum
This commit is contained in:
@@ -7,10 +7,21 @@ import os
|
||||
import os.path
|
||||
import pkg_resources
|
||||
import shutil
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def cwd(path):
|
||||
original_cwd = os.getcwd()
|
||||
os.chdir(path)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def memoize_by_cwd(func):
|
||||
"""Memoize a function call based on os.getcwd()."""
|
||||
@functools.wraps(func)
|
||||
@@ -83,3 +94,59 @@ def resource_filename(filename):
|
||||
'pre_commit',
|
||||
os.path.join('resources', filename),
|
||||
)
|
||||
|
||||
|
||||
class CalledProcessError(RuntimeError):
|
||||
def __init__(self, returncode, cmd, expected_returncode, output=None):
|
||||
super(CalledProcessError, self).__init__(
|
||||
returncode, cmd, expected_returncode, output,
|
||||
)
|
||||
self.returncode = returncode
|
||||
self.cmd = cmd
|
||||
self.expected_returncode = expected_returncode
|
||||
self.output = output
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
'Command: {0!r}\n'
|
||||
'Return code: {1}\n'
|
||||
'Expected return code: {2}\n'
|
||||
'Output: {3!r}\n'.format(
|
||||
self.cmd,
|
||||
self.returncode,
|
||||
self.expected_returncode,
|
||||
self.output,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def cmd_output(*cmd, **kwargs):
|
||||
retcode = kwargs.pop('retcode', 0)
|
||||
stdin = kwargs.pop('stdin', None)
|
||||
encoding = kwargs.pop('encoding', 'UTF-8')
|
||||
__popen = kwargs.pop('__popen', subprocess.Popen)
|
||||
|
||||
popen_kwargs = {
|
||||
'stdin': subprocess.PIPE,
|
||||
'stdout': subprocess.PIPE,
|
||||
'stderr': subprocess.PIPE,
|
||||
}
|
||||
|
||||
if stdin is not None:
|
||||
stdin = stdin.encode('UTF-8')
|
||||
|
||||
popen_kwargs.update(kwargs)
|
||||
proc = __popen(cmd, **popen_kwargs)
|
||||
stdout, stderr = proc.communicate(stdin)
|
||||
if encoding is not None and stdout is not None:
|
||||
stdout = stdout.decode(encoding)
|
||||
if encoding is not None and stderr is not None:
|
||||
stderr = stderr.decode(encoding)
|
||||
returncode = proc.returncode
|
||||
|
||||
if retcode is not None and retcode != returncode:
|
||||
raise CalledProcessError(
|
||||
returncode, cmd, retcode, output=(stdout, stderr),
|
||||
)
|
||||
|
||||
return proc.returncode, stdout, stderr
|
||||
|
||||
Reference in New Issue
Block a user