Skip to content

Instantly share code, notes, and snippets.

@nemisj
Last active August 29, 2015 14:20
Show Gist options
  • Save nemisj/64197e2e6386570dc1ba to your computer and use it in GitHub Desktop.
Save nemisj/64197e2e6386570dc1ba to your computer and use it in GitHub Desktop.
Python script which will generate PATH env variable based on the content of the folder
function reload_path() {
export PATH=$(python ~/dynamic_path.py ~/bin);
}
reload_path
#!/usr/bin/env python
import os
import sys
import re
old_path=os.getenv("PATH")
if len(sys.argv) == 1:
print old_path
sys.exit(1)
bin_folder = os.path.abspath(sys.argv[1])
paths = [ bin_folder ]
def clean_path(path):
cleaned = []
for name in path.split(':'):
if not re.search(bin_folder, name):
cleaned.append(name)
return cleaned
cleaned = clean_path(old_path)
for name in os.listdir(bin_folder):
full_path = os.path.join(bin_folder, name)
if os.path.isdir(full_path):
paths.append(full_path)
if (len(sys.argv) == 3) and (sys.argv[2] == "clean"):
# this might be clean
print ':'.join(cleaned)
else:
print ':'.join(paths + cleaned)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment