Skip to content

Commit bbd2572

Browse files
committed
Remove plumbum
1 parent 5d9ba14 commit bbd2572

24 files changed

+236
-203
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
from aspy.yaml import ordered_dump
77
from aspy.yaml import ordered_load
8-
from plumbum import local
98

109
import pre_commit.constants as C
1110
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1211
from pre_commit.clientlib.validate_config import load_config
1312
from pre_commit.jsonschema_extensions import remove_defaults
1413
from pre_commit.ordereddict import OrderedDict
1514
from pre_commit.repository import Repository
15+
from pre_commit.util import cwd
16+
from pre_commit.util import cmd_output
1617

1718

1819
class RepositoryCannotBeUpdatedError(RuntimeError):
@@ -29,9 +30,9 @@ def _update_repository(repo_config, runner):
2930
"""
3031
repo = Repository.create(repo_config, runner.store)
3132

32-
with local.cwd(repo.repo_path_getter.repo_path):
33-
local['git']('fetch')
34-
head_sha = local['git']('rev-parse', 'origin/master').strip()
33+
with cwd(repo.repo_path_getter.repo_path):
34+
cmd_output('git', 'fetch')
35+
head_sha = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
3536

3637
# Don't bother trying to update if our sha is the same
3738
if head_sha == repo_config['sha']:

pre_commit/git.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import os
66
import os.path
77
import re
8-
from plumbum import local
98

109
from pre_commit.errors import FatalError
10+
from pre_commit.util import cmd_output
1111
from pre_commit.util import memoize_by_cwd
1212

1313

@@ -54,21 +54,21 @@ def get_conflicted_files():
5454
# This will get the rest of the changes made after the merge.
5555
# If they resolved the merge conflict by choosing a mesh of both sides
5656
# this will also include the conflicted files
57-
tree_hash = local['git']('write-tree').strip()
58-
merge_diff_filenames = local['git'](
59-
'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
60-
).splitlines()
57+
tree_hash = cmd_output('git', 'write-tree')[1].strip()
58+
merge_diff_filenames = cmd_output(
59+
'git', 'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
60+
)[1].splitlines()
6161
return set(merge_conflict_filenames) | set(merge_diff_filenames)
6262

6363

6464
@memoize_by_cwd
6565
def get_staged_files():
66-
return local['git']('diff', '--staged', '--name-only').splitlines()
66+
return cmd_output('git', 'diff', '--staged', '--name-only')[1].splitlines()
6767

6868

6969
@memoize_by_cwd
7070
def get_all_files():
71-
return local['git']('ls-files').splitlines()
71+
return cmd_output('git', 'ls-files')[1].splitlines()
7272

7373

7474
def get_files_matching(all_file_list_strategy):

pre_commit/languages/ruby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io
55

66
from pre_commit.languages import helpers
7-
from pre_commit.prefixed_command_runner import CalledProcessError
7+
from pre_commit.util import CalledProcessError
88
from pre_commit.util import clean_path_on_failure
99
from pre_commit.util import resource_filename
1010
from pre_commit.util import tarfile_open

pre_commit/make_archives.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import os.path
66
import shutil
7-
from plumbum import local
87

8+
from pre_commit.util import cmd_output
9+
from pre_commit.util import cwd
910
from pre_commit.util import tarfile_open
1011
from pre_commit.util import tmpdir
1112

@@ -42,9 +43,9 @@ def make_archive(name, repo, ref, destdir):
4243
output_path = os.path.join(destdir, name + '.tar.gz')
4344
with tmpdir() as tempdir:
4445
# Clone the repository to the temporary directory
45-
local['git']('clone', repo, tempdir)
46-
with local.cwd(tempdir):
47-
local['git']('checkout', ref)
46+
cmd_output('git', 'clone', repo, tempdir)
47+
with cwd(tempdir):
48+
cmd_output('git', 'checkout', ref)
4849

4950
# We don't want the '.git' directory
5051
# It adds a bunch of size to the archive and we don't use it at

pre_commit/prefixed_command_runner.py

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,7 @@
44
import os.path
55
import subprocess
66

7-
8-
class CalledProcessError(RuntimeError):
9-
def __init__(self, returncode, cmd, expected_returncode, output=None):
10-
super(CalledProcessError, self).__init__(
11-
returncode, cmd, expected_returncode, output,
12-
)
13-
self.returncode = returncode
14-
self.cmd = cmd
15-
self.expected_returncode = expected_returncode
16-
self.output = output
17-
18-
def __str__(self):
19-
return (
20-
'Command: {0!r}\n'
21-
'Return code: {1}\n'
22-
'Expected return code: {2}\n'
23-
'Output: {3!r}\n'.format(
24-
self.cmd,
25-
self.returncode,
26-
self.expected_returncode,
27-
self.output,
28-
)
29-
)
7+
from pre_commit.util import cmd_output
308

319

3210
def _replace_cmd(cmd, **kwargs):
@@ -57,32 +35,10 @@ def _create_path_if_not_exists(self):
5735
if not os.path.exists(self.prefix_dir):
5836
self.__makedirs(self.prefix_dir)
5937

60-
def run(self, cmd, retcode=0, stdin=None, encoding='UTF-8', **kwargs):
61-
popen_kwargs = {
62-
'stdin': subprocess.PIPE,
63-
'stdout': subprocess.PIPE,
64-
'stderr': subprocess.PIPE,
65-
}
66-
if stdin is not None:
67-
stdin = stdin.encode('UTF-8')
68-
69-
popen_kwargs.update(kwargs)
38+
def run(self, cmd, **kwargs):
7039
self._create_path_if_not_exists()
7140
replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir)
72-
proc = self.__popen(replaced_cmd, **popen_kwargs)
73-
stdout, stderr = proc.communicate(stdin)
74-
if encoding is not None:
75-
stdout = stdout.decode(encoding)
76-
if encoding is not None:
77-
stderr = stderr.decode(encoding)
78-
returncode = proc.returncode
79-
80-
if retcode is not None and retcode != returncode:
81-
raise CalledProcessError(
82-
returncode, replaced_cmd, retcode, output=(stdout, stderr),
83-
)
84-
85-
return proc.returncode, stdout, stderr
41+
return cmd_output(*replaced_cmd, __popen=self.__popen, **kwargs)
8642

8743
def path(self, *parts):
8844
path = os.path.join(self.prefix_dir, *parts)

pre_commit/staged_files_only.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import time
77

8-
from pre_commit.prefixed_command_runner import CalledProcessError
8+
from pre_commit.util import CalledProcessError
99

1010

1111
logger = logging.getLogger('pre_commit')

pre_commit/store.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import os.path
77
import tempfile
88
from cached_property import cached_property
9-
from plumbum import local
109

1110
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
1211
from pre_commit.util import clean_path_on_failure
12+
from pre_commit.util import cmd_output
13+
from pre_commit.util import cwd
1314
from pre_commit.util import hex_md5
1415

1516

@@ -85,9 +86,9 @@ def clone(self, url, sha):
8586

8687
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
8788
with clean_path_on_failure(dir):
88-
local['git']('clone', '--no-checkout', url, dir)
89-
with local.cwd(dir):
90-
local['git']('checkout', sha)
89+
cmd_output('git', 'clone', '--no-checkout', url, dir)
90+
with cwd(dir):
91+
cmd_output('git', 'checkout', sha)
9192

9293
# Make a symlink from sha->repo
9394
os.symlink(dir, sha_path)

pre_commit/util.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
import os.path
88
import pkg_resources
99
import shutil
10+
import subprocess
1011
import tarfile
1112
import tempfile
1213

1314

15+
@contextlib.contextmanager
16+
def cwd(path):
17+
original_cwd = os.getcwd()
18+
os.chdir(path)
19+
try:
20+
yield
21+
finally:
22+
os.chdir(original_cwd)
23+
24+
1425
def memoize_by_cwd(func):
1526
"""Memoize a function call based on os.getcwd()."""
1627
@functools.wraps(func)
@@ -83,3 +94,59 @@ def resource_filename(filename):
8394
'pre_commit',
8495
os.path.join('resources', filename),
8596
)
97+
98+
99+
class CalledProcessError(RuntimeError):
100+
def __init__(self, returncode, cmd, expected_returncode, output=None):
101+
super(CalledProcessError, self).__init__(
102+
returncode, cmd, expected_returncode, output,
103+
)
104+
self.returncode = returncode
105+
self.cmd = cmd
106+
self.expected_returncode = expected_returncode
107+
self.output = output
108+
109+
def __str__(self):
110+
return (
111+
'Command: {0!r}\n'
112+
'Return code: {1}\n'
113+
'Expected return code: {2}\n'
114+
'Output: {3!r}\n'.format(
115+
self.cmd,
116+
self.returncode,
117+
self.expected_returncode,
118+
self.output,
119+
)
120+
)
121+
122+
123+
def cmd_output(*cmd, **kwargs):
124+
retcode = kwargs.pop('retcode', 0)
125+
stdin = kwargs.pop('stdin', None)
126+
encoding = kwargs.pop('encoding', 'UTF-8')
127+
__popen = kwargs.pop('__popen', subprocess.Popen)
128+
129+
popen_kwargs = {
130+
'stdin': subprocess.PIPE,
131+
'stdout': subprocess.PIPE,
132+
'stderr': subprocess.PIPE,
133+
}
134+
135+
if stdin is not None:
136+
stdin = stdin.encode('UTF-8')
137+
138+
popen_kwargs.update(kwargs)
139+
proc = __popen(cmd, **popen_kwargs)
140+
stdout, stderr = proc.communicate(stdin)
141+
if encoding is not None and stdout is not None:
142+
stdout = stdout.decode(encoding)
143+
if encoding is not None and stderr is not None:
144+
stderr = stderr.decode(encoding)
145+
returncode = proc.returncode
146+
147+
if retcode is not None and retcode != returncode:
148+
raise CalledProcessError(
149+
returncode, cmd, retcode, output=(stdout, stderr),
150+
)
151+
152+
return proc.returncode, stdout, stderr

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
'jsonschema',
4444
'nodeenv>=0.11.1',
4545
'ordereddict',
46-
'plumbum',
4746
'pyyaml',
4847
'simplejson',
4948
'virtualenv',

testing/fixtures.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,33 @@
44
import io
55
import os.path
66
from aspy.yaml import ordered_dump
7-
from plumbum import local
87

98
import pre_commit.constants as C
109
from pre_commit.clientlib.validate_manifest import load_manifest
1110
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1211
from pre_commit.clientlib.validate_config import validate_config_extra
1312
from pre_commit.jsonschema_extensions import apply_defaults
1413
from pre_commit.ordereddict import OrderedDict
14+
from pre_commit.util import cmd_output
15+
from pre_commit.util import cwd
1516
from testing.util import copy_tree_to_path
1617
from testing.util import get_head_sha
1718
from testing.util import get_resource_path
1819

1920

20-
git = local['git']
21-
22-
2321
def git_dir(tmpdir_factory):
2422
path = tmpdir_factory.get()
25-
with local.cwd(path):
26-
git('init')
23+
with cwd(path):
24+
cmd_output('git', 'init')
2725
return path
2826

2927

3028
def make_repo(tmpdir_factory, repo_source):
3129
path = git_dir(tmpdir_factory)
3230
copy_tree_to_path(get_resource_path(repo_source), path)
33-
with local.cwd(path):
34-
git('add', '.')
35-
git('commit', '-m', 'Add hooks')
31+
with cwd(path):
32+
cmd_output('git', 'add', '.')
33+
cmd_output('git', 'commit', '-m', 'Add hooks')
3634
return path
3735

3836

@@ -66,7 +64,7 @@ def make_consuming_repo(tmpdir_factory, repo_source):
6664
config = make_config_from_repo(path)
6765
git_path = git_dir(tmpdir_factory)
6866
write_config(git_path, config)
69-
with local.cwd(git_path):
70-
git('add', C.CONFIG_FILE)
71-
git('commit', '-m', 'Add hooks config')
67+
with cwd(git_path):
68+
cmd_output('git', 'add', C.CONFIG_FILE)
69+
cmd_output('git', 'commit', '-m', 'Add hooks config')
7270
return git_path

0 commit comments

Comments
 (0)