#!/usr/bin/python # # $Id$ # $HeadURL$ # # Remove duplicate entries from a path as well as # user supplied entries. # # Usage: # # filter_path PATHVAR [p1 p2 p3 ...] # # The PATHVAR should be the name of an environment # variable like "PATH" (not "$PATH"). This will remove # duplicate entries or any entries specified by the # user. Order of the path is preserved. If a duplicate # is removed, it is always the one(s) occurring later. # Empty entries are also removed. # # This will not print a newline at the end of the new # path. This is to make it easy to set environment # variables in scripts to the output of this. # import sys import os if (len(sys.argv)>1): varname = sys.argv[1] var = os.environ.get(varname) filter_paths = sys.argv[2:] newpaths = [] if (var != 'none' ): paths = var.split(':') for path in paths: if len(path)>0 : # remove empty entries if path not in newpaths: # remove duplicates if path not in filter_paths: # remove user supplied paths newpaths.append(path) if len(newpaths)>0: newpath = newpaths[0] for path in newpaths[1:]: newpath = '%s:%s' % (newpath,path) sys.stdout.write(newpath) else: #exit -1 pass