|
4 | 4 | import os.path |
5 | 5 | import subprocess |
6 | 6 |
|
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 |
30 | 8 |
|
31 | 9 |
|
32 | 10 | def _replace_cmd(cmd, **kwargs): |
@@ -57,32 +35,10 @@ def _create_path_if_not_exists(self): |
57 | 35 | if not os.path.exists(self.prefix_dir): |
58 | 36 | self.__makedirs(self.prefix_dir) |
59 | 37 |
|
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): |
70 | 39 | self._create_path_if_not_exists() |
71 | 40 | 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) |
86 | 42 |
|
87 | 43 | def path(self, *parts): |
88 | 44 | path = os.path.join(self.prefix_dir, *parts) |
|
0 commit comments