Remove plumbum

This commit is contained in:
Anthony Sottile
2014-10-01 17:27:23 -07:00
parent 5d9ba14841
commit bbd2572b11
24 changed files with 236 additions and 203 deletions

View File

@@ -5,7 +5,6 @@ import sys
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
from plumbum import local
import pre_commit.constants as C
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
@@ -13,6 +12,8 @@ from pre_commit.clientlib.validate_config import load_config
from pre_commit.jsonschema_extensions import remove_defaults
from pre_commit.ordereddict import OrderedDict
from pre_commit.repository import Repository
from pre_commit.util import cwd
from pre_commit.util import cmd_output
class RepositoryCannotBeUpdatedError(RuntimeError):
@@ -29,9 +30,9 @@ def _update_repository(repo_config, runner):
"""
repo = Repository.create(repo_config, runner.store)
with local.cwd(repo.repo_path_getter.repo_path):
local['git']('fetch')
head_sha = local['git']('rev-parse', 'origin/master').strip()
with cwd(repo.repo_path_getter.repo_path):
cmd_output('git', 'fetch')
head_sha = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
# Don't bother trying to update if our sha is the same
if head_sha == repo_config['sha']:

View File

@@ -5,9 +5,9 @@ import logging
import os
import os.path
import re
from plumbum import local
from pre_commit.errors import FatalError
from pre_commit.util import cmd_output
from pre_commit.util import memoize_by_cwd
@@ -54,21 +54,21 @@ def get_conflicted_files():
# This will get the rest of the changes made after the merge.
# If they resolved the merge conflict by choosing a mesh of both sides
# this will also include the conflicted files
tree_hash = local['git']('write-tree').strip()
merge_diff_filenames = local['git'](
'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
).splitlines()
tree_hash = cmd_output('git', 'write-tree')[1].strip()
merge_diff_filenames = cmd_output(
'git', 'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
)[1].splitlines()
return set(merge_conflict_filenames) | set(merge_diff_filenames)
@memoize_by_cwd
def get_staged_files():
return local['git']('diff', '--staged', '--name-only').splitlines()
return cmd_output('git', 'diff', '--staged', '--name-only')[1].splitlines()
@memoize_by_cwd
def get_all_files():
return local['git']('ls-files').splitlines()
return cmd_output('git', 'ls-files')[1].splitlines()
def get_files_matching(all_file_list_strategy):

View File

@@ -4,7 +4,7 @@ import contextlib
import io
from pre_commit.languages import helpers
from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import CalledProcessError
from pre_commit.util import clean_path_on_failure
from pre_commit.util import resource_filename
from pre_commit.util import tarfile_open

View File

@@ -4,8 +4,9 @@ from __future__ import unicode_literals
import os.path
import shutil
from plumbum import local
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import tarfile_open
from pre_commit.util import tmpdir
@@ -42,9 +43,9 @@ def make_archive(name, repo, ref, destdir):
output_path = os.path.join(destdir, name + '.tar.gz')
with tmpdir() as tempdir:
# Clone the repository to the temporary directory
local['git']('clone', repo, tempdir)
with local.cwd(tempdir):
local['git']('checkout', ref)
cmd_output('git', 'clone', repo, tempdir)
with cwd(tempdir):
cmd_output('git', 'checkout', ref)
# We don't want the '.git' directory
# It adds a bunch of size to the archive and we don't use it at

View File

@@ -4,29 +4,7 @@ import os
import os.path
import subprocess
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,
)
)
from pre_commit.util import cmd_output
def _replace_cmd(cmd, **kwargs):
@@ -57,32 +35,10 @@ class PrefixedCommandRunner(object):
if not os.path.exists(self.prefix_dir):
self.__makedirs(self.prefix_dir)
def run(self, cmd, retcode=0, stdin=None, encoding='UTF-8', **kwargs):
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)
def run(self, cmd, **kwargs):
self._create_path_if_not_exists()
replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir)
proc = self.__popen(replaced_cmd, **popen_kwargs)
stdout, stderr = proc.communicate(stdin)
if encoding is not None:
stdout = stdout.decode(encoding)
if encoding is not None:
stderr = stderr.decode(encoding)
returncode = proc.returncode
if retcode is not None and retcode != returncode:
raise CalledProcessError(
returncode, replaced_cmd, retcode, output=(stdout, stderr),
)
return proc.returncode, stdout, stderr
return cmd_output(*replaced_cmd, __popen=self.__popen, **kwargs)
def path(self, *parts):
path = os.path.join(self.prefix_dir, *parts)

View File

@@ -5,7 +5,7 @@ import io
import logging
import time
from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import CalledProcessError
logger = logging.getLogger('pre_commit')

View File

@@ -6,10 +6,11 @@ import os
import os.path
import tempfile
from cached_property import cached_property
from plumbum import local
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import hex_md5
@@ -85,9 +86,9 @@ class Store(object):
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
with clean_path_on_failure(dir):
local['git']('clone', '--no-checkout', url, dir)
with local.cwd(dir):
local['git']('checkout', sha)
cmd_output('git', 'clone', '--no-checkout', url, dir)
with cwd(dir):
cmd_output('git', 'checkout', sha)
# Make a symlink from sha->repo
os.symlink(dir, sha_path)

View File

@@ -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