#!/usr/bin/env python

"""
A hook into setuptools for Git.
"""

import os
from subprocess import Popen, PIPE

def gitlsfiles(dirname=""):
    try:
        p = Popen(['git', 'ls-files', dirname], stdout=PIPE, stderr=PIPE)
        p.stderr.close()
        files = p.stdout.readlines()
    except:
        # 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.
        return []
    if p.wait():
        # git chocked
        return []
    return [f.strip() for f in files]

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(gitlsfiles(sys.argv[1]))

@@ -61,237 +61,858 @@
 %22%22%0A%0A
-import os%0Afrom subprocess import Popen, PIPE%0A%0Adef gitlsfiles(dirname=%22%22):%0A    try:%0A        p = Popen(%5B'git', 'ls-files', dirname%5D, stdout=PIPE, stderr=PIPE)%0A        p.stderr.close()%0A        files = p.stdout.readlines()%0A    except
+from subprocess import CalledProcessError%0Afrom subprocess import PIPE%0A%0Atry:%0A    from subprocess import check_output%0Aexcept ImportError:%0A    # BBB for python %3C2.7%0A    def check_output(*popenargs, **kwargs):%0A        from subprocess import Popen%0A        if 'stdout' in kwargs:%0A            raise ValueError('stdout argument not allowed, it will be overridden.')%0A        process = Popen(stdout=PIPE, *popenargs, **kwargs)%0A        output, unused_err = process.communicate()%0A        retcode = process.poll()%0A        if retcode:%0A            cmd = kwargs.get(%22args%22)%0A            if cmd is None:%0A                cmd = popenargs%5B0%5D%0A            raise CalledProcessError(retcode, cmd, output=output)%0A        return output%0A%0A%0Adef gitlsfiles(dirname=%22%22):%0A    try:%0A        output = check_output(%5B'git', 'ls-files', dirname%5D, stderr=PIPE)%0A    except CalledProcessError
 :%0A  
@@ -1093,102 +1093,40 @@
  %5B%5D%0A
-    if p.wait():%0A        # git chocked%0A        return %5B%5D%0A    return %5Bf.strip() for f in files%5D
+%0A    return output.splitlines()%0A
 %0A%0Aif

Use subprocess.check_output if available