Skip to content

Instantly share code, notes, and snippets.

@kobybum
Last active April 5, 2024 00:44
Show Gist options
  • Save kobybum/a162be138b2d19e3eac20547a7f10fa3 to your computer and use it in GitHub Desktop.
Save kobybum/a162be138b2d19e3eac20547a7f10fa3 to your computer and use it in GitHub Desktop.
Finding Unused Python Files
#!/bin/bash
# This is part of a Medium article about finding unused files:
# https://medium.com/@kobybum/finding-dead-python-files-with-snakefood-6c75a3e82294
# Generate a list of included dependencies
sfood -i example-project > /tmp/out.deps
# Get dependant filepath from each dependency, sort and get unique
cat /tmp/out.deps | \
grep -v test | \
cut -d"'" -f8 | \
sort | \
uniq > /tmp/required.txt
# Find all modules in our project
find example-project -name '*.py' | \
grep -v "__init__.py" | \
grep -v "test" | \
sort > /tmp/modules.txt
# Compare the two, and cleanup
diff tmp/modules.py tmp/required.py | grep '<' | cut -d' ' -f2
@drjasonharrison
Copy link

drjasonharrison commented Apr 5, 2024

Nice

Suggestions:

  • You can combine the two greps into grep -v -e "__init__.py" -e "test"
  • You can use CWD=$(pwd) to get the current working directoy and and use that instead of "example-project" as the starting point
  • You don't actually clean up, you may want to use TMP_DIR="/tmp/${CWD}/" as in "${TMP_DIR}/out.deps" and then add at the end of the code rm -r "${TMP_DIR}"

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