Last active
August 29, 2015 14:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reload_path() { | |
export PATH=$(python ~/dynamic_path.py ~/bin); | |
} | |
reload_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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