Recently I wrote a simple Python utility called pathdiag, which stands as an abbreviation for "path diagnosis". The concept is simple, it parses environment variables that contain a path, or a list of paths and tries to identify possible problems.
The reasoning behing this is that environment variables are oftenly abused. For example, when you use brew on MacOS to install packages, you often have to add some new paths to your
PATH variable in order for them to work or make their executables available. Another example is that you often keep some of your dotfiles in a git repository in order to keep them synced across your systems. But those system usually have differences: different structures (based on your OS), different usernames, different packages installed etc. So sometimes you end up with environment variables that are unreadable and you can't verify whether all paths are needed or even exist.
Pathdiag is a simple Python 3 script that performs the following checks on all paths included in the environment variables of choice (PATH by default):
- the path exists
- the path is actually a directory
- the path is readable by the current user
It also provides you with two handy shell functions,
safe_append, that will put a path in the end of an environment variable only if the path successfully passes the above checks, and
safe_prepend which does the same but puts the path in the beggining (so it gets prioritized).
$ pathdiag -v ✓ /usr/local/bin ✓ /usr/bin ✓ /bin ✓ /usr/sbin ✓ /sbin
Automatically check PATH variable on session start
First, put the following lines in your
source /usr/local/bin/path-diag-functions.sh pathdiag
You can then add new paths safely:
safe_append "/usr/local/Cellar/node/7.4.0/bin" safe_prepend "/usr/local/opt/python/libexec/bin"
Some future extensions could be:
- Inform the user about the executables each path adds
- Warn the user about executables that overwrite existing ones
Contributions are more than welcomed. There are other parts that can be improved as well. Add tests, add Python 2 support, or move the whole functionality to a bash script in order to decouple it from Python.
Looking forward to your feedback.