Skip to content

Instantly share code, notes, and snippets.

@isle2983
Created October 30, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isle2983/79535a443caf5cce26d5b56eb0bf56ee to your computer and use it in GitHub Desktop.
Save isle2983/79535a443caf5cce26d5b56eb0bf56ee to your computer and use it in GitHub Desktop.
invoke a command for every directory in a git repo
#!/usr/bin/python3
import os
import sys
USAGE = """
$ ./walk.py <repo> <outputdir>
"""
if len(sys.argv) != 3:
print(USAGE)
sys.exit()
if not (os.path.isdir(sys.argv[1])):
print(USAGE)
sys.exit()
repo = os.path.abspath(sys.argv[1])
if not (os.path.isdir(sys.argv[2])):
print(USAGE)
sys.exit()
outputDir = os.path.abspath(sys.argv[2])
os.chdir(repo)
files = [line.rstrip() for line in os.popen("git ls-files").readlines()]
paths = sorted(list(set([os.path.dirname(f) for f in files])))
def writeOutput(path, output):
filename = os.path.join(outputDir, path.replace("/", "-") + '.txt')
outputfile = open(filename, "w")
outputfile.write(output)
outputfile.close()
def doFame(path):
print(path)
output = os.popen('find .').read()
print(output)
writeOutput(path, output)
for p in paths:
os.chdir(os.path.abspath(os.path.join(repo, p)))
doFame(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment