I’ve written a lot of little scripts over the years, and it’d be nice to be able to migrate to Kubuntu 20.04 LTS when it comes out, so I decided to write a little helper to find creations of mine that still depend on Python 2.x as quickly as possible.
The script I came up with produces output like this:
ERROR: NO PY3: /home/ssokolow/bin/k3b-rm.py ERROR: NO PY3: /home/ssokolow/bin/mergemove.py WARNING: NO #!: /home/ssokolow/bin/mpv.py ERROR: NOT UTF8: /home/ssokolow/src/Human Sort.py
Perfect for running with a command like
~/src/audit_python2.py --ignore read-only ~/bin ~/src 2>&1 | tee ~/python2_potentials.txt
It works by applying a few simple rules:
- If a folder is named
.toxor contains a
bin/activatescript, skip it to avoid flooding the results with virtualenv files.
- If the file has a
.pywextension but no shebang, print a
WARNING: NO #!.
- If the file has a shebang line containing
python3, print an
ERROR: NO PY3.
- If reading the first line of the file fails with a
UnicodeDecodeErrorand the file has one of the aforementioned extensions, print an
ERROR: NOT UTF8.
- If reading the file fails for any other reason than
UnicodeDecodeError, print a general
ERROR: READ ERR.
The logging level is configurable with repeated
-q arguments and there are liberal
DEBUG messages to detail how it’s traversing the filesystem. It also traverses in sorted order to ensure consistency between runs and folders or files can be ignored using the
--ignore option to skip stuff like read-only Git checkouts.
If this sounds useful to you, it’s up on GitHub Gist. For lack of a better name, it calls itself “Python 2.x Auditor”.