Skip to content

Instantly share code, notes, and snippets.

@philopon
Last active December 26, 2015 16:49
Show Gist options
  • Save philopon/7183064 to your computer and use it in GitHub Desktop.
Save philopon/7183064 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
global_paths = "/etc/paths"
global_paths_d = "/etc/paths.d"
user_paths = os.path.join(os.environ['HOME'], ".paths")
user_paths_d = os.path.join(os.environ['HOME'], ".paths.d")
library_paths = os.path.join(os.environ['HOME'], ".library_paths")
include_paths = os.path.join(os.environ['HOME'], ".include_paths")
python_paths = os.path.join(os.environ['HOME'], ".python_paths")
############################################################
def read_file(path):
paths = []
if os.path.exists(path):
for line in open(path):
paths.append(line.strip())
return paths
def read_directory(dir):
paths = []
if os.path.exists(dir):
for file in os.listdir(dir):
paths += read_file(os.path.join(dir,file))
return paths
def print_path(target, paths, pp=False):
uniq = sorted(set(paths), key=paths.index)
if(len(uniq) > 0):
if pp:
print target + "=\"" + ":".join(uniq) + "\""
print "export " + target
else:
print target + "=\"" + ":".join(uniq) + "\"; export " + target + ";",
if __name__ == "__main__":
pp = False
### PATH
paths = read_file(user_paths) + read_directory(user_paths_d) \
+ read_file(global_paths) + read_directory(global_paths_d)
print_path("PATH", paths, pp)
### (DY)LD_LIBRARY_PATH
lib = read_file(library_paths)
print_path( "LIBRARY_PATH", lib, pp)
print_path( "LD_LIBRARY_PATH", lib, pp)
print_path("DYLD_LIBRARY_PATH", lib, pp)
## INCLUDE
inc = read_file(include_paths)
print_path("C_INCLUDE_PATH", inc, pp)
print_path("CPLUS_INCLUDE_PATH", inc, pp)
## PYTHON
py = read_file(python_paths)
print_path("PYTHONPATH", py, pp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment