Created
January 24, 2018 17:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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') |
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)
Author
did you see: # on osx you want brew install gnu-sed to get gsed?
Author
you could possible change the script to use sed on linux.
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
Thanks, this is great. Simple and straightforward. I made a few tweaks for our purposes.