Helper for Finding Python 2.x Scripts Still To Be Ported

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 .tox or contains a bin/activate script, skip it to avoid flooding the results with virtualenv files.
  • If the file has a .py or .pyw extension but no shebang, print a WARNING: NO #!.
  • If the file has a shebang line containing python but not python3, print an ERROR: NO PY3.
  • If reading the first line of the file fails with a UnicodeDecodeError and 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 -v and -q arguments and there are liberal INFO and 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 -i/--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”.

CC BY-SA 4.0 Helper for Finding Python 2.x Scripts Still To Be Ported by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.