-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutil.py
More file actions
51 lines (38 loc) · 1.33 KB
/
util.py
File metadata and controls
51 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# From https://github.com/pre-commit/pre-commit-hooks
# License: MIT
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import subprocess
import os
devnull = open(os.devnull, 'w')
class CalledProcessError(RuntimeError):
pass
def added_files():
out, err, rc = cmd_output('git', 'diff', '--staged', '--name-only')
return set([f for f in out.splitlines() if f.endswith('py')])
def cmd_output(*cmd, **kwargs):
retcode = kwargs.pop('retcode', 0)
popen_kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
popen_kwargs.update(kwargs)
proc = subprocess.Popen(cmd, **popen_kwargs)
stdout, stderr = proc.communicate()
stdout = stdout.decode('UTF-8')
if stderr is not None:
stderr = stderr.decode('UTF-8')
if retcode is not None and proc.returncode != retcode:
raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
return stdout, stderr, proc.returncode
def execute(cmd, silent=False):
if silent:
params = {
'stdout': devnull,
'stderr': devnull,
}
else:
params = {}
retcode = subprocess.call(cmd.split(), **params)
return retcode
def exists(cmd):
return execute('which %s' % cmd, silent=True) == 0