Skip to content

Instantly share code, notes, and snippets.

@johnynek
Created January 24, 2018 17:17
Show Gist options
  • Save johnynek/a15c7a8270e816e8ee1a8c97c70cfe9b to your computer and use it in GitHub Desktop.
Save johnynek/a15c7a8270e816e8ee1a8c97c70cfe9b to your computer and use it in GitHub Desktop.
Tool to remove unused deps by brute force on bazel. Stolen shamelessly from Andrew Buss
#!/usr/bin/env python
import sys
# on osx you want brew install gnu-sed to get gsed
from sh import cp, rm, gsed, bazel, ErrorReturnCode
test_fn = lambda: bazel('build', sys.argv[1])
filename = sys.argv[2]
print "Checking that tests pass before starting"
print test_fn()
cp(filename, filename + '.good')
lines = lambda: open(filename).read().split('\n')
line_num = 0
while line_num < len(lines()):
line = lines()[line_num].strip()
# Heuristic to only touch lines that look like dependencies
if line and line[0] == '"' and line[1] in '@-/':
print "Trying to remove", line_num, line
gsed('-i', '%dd'%(line_num+1), filename)
try:
print test_fn()
success = True
except ErrorReturnCode as e:
print e
success = False
if success:
print "successful, saving"
cp(filename, filename + '.good')
else:
print "unsuccessful, skipping"
cp(filename + '.good', filename)
line_num += 1
else:
line_num += 1
rm(filename + '.good')
@plaird
Copy link

plaird commented Mar 27, 2020

Thanks, this is great. Simple and straightforward. I made a few tweaks for our purposes.

@michael-lefkowitz-techlabs

hmm, I'd like to use this, but it looks like the sh library (installed via pip install sh) doesn't include anything called gsed:

Traceback (most recent call last):
  File "../dep_clean.py", line 4, in <module>
    from sh import cp, rm, gsed, bazel, ErrorReturnCode
ImportError: cannot import name 'gsed' from 'sh' (/home/michael/work/techlabs/ve/lib/python3.8/site-packages/sh.py)

@johnynek
Copy link
Author

johnynek commented Jun 4, 2021

did you see: # on osx you want brew install gnu-sed to get gsed?

@johnynek
Copy link
Author

johnynek commented Jun 4, 2021

you could possible change the script to use sed on linux.

@michael-lefkowitz-techlabs

yup, gave that a try, but the sh python library doesn't have a sed method either unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment