Skip to content

Instantly share code, notes, and snippets.

@Mizzlr
Created October 15, 2018 05:20
Show Gist options
  • Save Mizzlr/be9f34da7d375b68f87e947b2e655c72 to your computer and use it in GitHub Desktop.
Save Mizzlr/be9f34da7d375b68f87e947b2e655c72 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dependencies:
# ripgrep https://github.com/BurntSushi/ripgrep#installation
# realpath from GNU coreutils
# pip3 install pylint isort dewildcard
# Usage: ./fix-python-wild-card-imports.sh "path/to/python/source/to/fix/"
# set -x # uncomment this line to see all commands
rm -rf fixed
mkdir -p fixed
cp -r $1 fixed
full=`realpath $1`
cd fixed
export PYTHONPATH=`pwd`
for file in `find . -name "*.py"`; do
echo "====> PASS 1: Fixing star imports in file $file"
all=`cat $file | rg "^(class|def) " | cut -d'(' -f 1 | cut -d ':' -f 1 | cut -d ' ' -f 2 | xargs | sed "s/ /', '/g"`
all="__all__ = ['$all']"
echo $all >> $file
done
for file in `find . -name "*.py"`; do
echo "====> PASS 2: Fixing star imports in file $file"
dewildcard -w $file
isort -y -ds -sl $file
regex="import (`pylint $file | grep "unused-import" | cut -d' ' -f 4 | xargs | tr ' ' '|'`)$"
noissue="ximport ()$"
if [ "x$regex" != "$noissue" ]; then
cat $file | rg -v "$regex" > "$file.tmp"
mv "$file.tmp" $file
isort -y -ds -m 5 $file
echo "========> Fixed file $file. See diff below."
diff $full/$file $file
else
echo "========> No issues found in $file"
fi
done
for file in `find . -name "*.py"`; do
lines=`cat $file | wc -l`
cat $file | head -n `expr $lines - 1` > "$file.tmp"
mv "$file.tmp" $file
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment