Skip to content

Commit

Permalink
Remove plumbum
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Oct 2, 2014
1 parent 5d9ba14 commit bbd2572
Show file tree
Hide file tree
Showing 24 changed files with 236 additions and 203 deletions.
9 changes: 5 additions & 4 deletions pre_commit/commands/autoupdate.py
Expand Up @@ -5,14 +5,15 @@

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
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):
Expand All @@ -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']:
Expand Down
14 changes: 7 additions & 7 deletions pre_commit/git.py
Expand Up @@ -5,9 +5,9 @@
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


Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pre_commit/languages/ruby.py
Expand Up @@ -4,7 +4,7 @@
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
Expand Down
9 changes: 5 additions & 4 deletions pre_commit/make_archives.py
Expand Up @@ -4,8 +4,9 @@

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

Expand Down Expand Up @@ -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
Expand Down
50 changes: 3 additions & 47 deletions pre_commit/prefixed_command_runner.py
Expand Up @@ -4,29 +4,7 @@
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):
Expand Down Expand Up @@ -57,32 +35,10 @@ def _create_path_if_not_exists(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion pre_commit/staged_files_only.py
Expand Up @@ -5,7 +5,7 @@
import logging
import time

from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import CalledProcessError


logger = logging.getLogger('pre_commit')
Expand Down
9 changes: 5 additions & 4 deletions pre_commit/store.py
Expand Up @@ -6,10 +6,11 @@
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


Expand Down Expand Up @@ -85,9 +86,9 @@ def clone(self, url, sha):

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)
Expand Down
67 changes: 67 additions & 0 deletions pre_commit/util.py
Expand Up @@ -7,10 +7,21 @@
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)
Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -43,7 +43,6 @@
'jsonschema',
'nodeenv>=0.11.1',
'ordereddict',
'plumbum',
'pyyaml',
'simplejson',
'virtualenv',
Expand Down
22 changes: 10 additions & 12 deletions testing/fixtures.py
Expand Up @@ -4,35 +4,33 @@
import io
import os.path
from aspy.yaml import ordered_dump
from plumbum import local

import pre_commit.constants as C
from pre_commit.clientlib.validate_manifest import load_manifest
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.ordereddict import OrderedDict
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.util import copy_tree_to_path
from testing.util import get_head_sha
from testing.util import get_resource_path


git = local['git']


def git_dir(tmpdir_factory):
path = tmpdir_factory.get()
with local.cwd(path):
git('init')
with cwd(path):
cmd_output('git', 'init')
return path


def make_repo(tmpdir_factory, repo_source):
path = git_dir(tmpdir_factory)
copy_tree_to_path(get_resource_path(repo_source), path)
with local.cwd(path):
git('add', '.')
git('commit', '-m', 'Add hooks')
with cwd(path):
cmd_output('git', 'add', '.')
cmd_output('git', 'commit', '-m', 'Add hooks')
return path


Expand Down Expand Up @@ -66,7 +64,7 @@ def make_consuming_repo(tmpdir_factory, repo_source):
config = make_config_from_repo(path)
git_path = git_dir(tmpdir_factory)
write_config(git_path, config)
with local.cwd(git_path):
git('add', C.CONFIG_FILE)
git('commit', '-m', 'Add hooks config')
with cwd(git_path):
cmd_output('git', 'add', C.CONFIG_FILE)
cmd_output('git', 'commit', '-m', 'Add hooks config')
return git_path
8 changes: 5 additions & 3 deletions testing/util.py
Expand Up @@ -5,7 +5,9 @@
import os.path
import pytest
import shutil
from plumbum import local

from pre_commit.util import cmd_output
from pre_commit.util import cwd


TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -34,8 +36,8 @@ def copy_tree_to_path(src_dir, dest_dir):


def get_head_sha(dir):
with local.cwd(dir):
return local['git']('rev-parse', 'HEAD').strip()
with cwd(dir):
return cmd_output('git', 'rev-parse', 'HEAD')[1].strip()


def is_valid_according_to_schema(obj, schema):
Expand Down

0 comments on commit bbd2572

Please sign in to comment.