-
Notifications
You must be signed in to change notification settings - Fork 16
/
setuptools_git.py
69 lines (59 loc) · 2.07 KB
/
setuptools_git.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
"""
A hook into setuptools for Git.
"""
import os
from subprocess import CalledProcessError
from subprocess import PIPE
from distutils.log import warn
try:
from subprocess import check_output
except ImportError:
# BBB for python <2.7
def check_output(*popenargs, **kwargs):
from subprocess import Popen
if 'stdout' in kwargs:
raise ValueError(
'stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd, output=output)
return output
def gitlsfiles(dirname=''):
try:
if dirname:
cwd = dirname
else:
cwd = None
dirname = '.'
dirname = os.path.realpath(dirname)
git_top = check_output(['git', 'rev-parse', '--show-toplevel'],
stderr=PIPE, cwd=cwd).strip()
git_files = check_output(['git', 'ls-files'], cwd=git_top, stderr=PIPE)
git_files = set([os.path.join(git_top, fn)
for fn in git_files.splitlines()])
except (CalledProcessError, OSError):
# Something went terribly wrong but the setuptools doc says we
# must be strong in the face of danger. We shall not run away
# in panic.
warn('Error running git')
raise StopIteration
prefix_length = len(dirname) + 1
for (root, dirs, files) in os.walk(dirname, followlinks=True):
for file in files:
filename = os.path.join(root, file)
realname = os.path.realpath(filename)
if realname in git_files:
yield filename[prefix_length:]
if __name__ == "__main__":
import sys
from pprint import pprint
if len(sys.argv) != 2:
print("USAGE: %s DIRNAME" % sys.argv[0])
sys.exit(1)
pprint(list(gitlsfiles(sys.argv[1])))