From 2e290c4c74ec5a490b0d3763abdd1509dd30787d Mon Sep 17 00:00:00 2001 From: "beville@gmail.com" Date: Fri, 8 Feb 2013 22:04:56 +0000 Subject: [PATCH] Used RE to make sure duplicate doesn't get added to path git-svn-id: http://comictagger.googlecode.com/svn/trunk@471 6c5673fe-1810-88d6-992b-cd32ca31540c --- comictaggerlib/utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/comictaggerlib/utils.py b/comictaggerlib/utils.py index 8936329..40da7e8 100644 --- a/comictaggerlib/utils.py +++ b/comictaggerlib/utils.py @@ -21,6 +21,7 @@ limitations under the License. """ import os +import re def listToString( l ): string = "" @@ -31,10 +32,16 @@ def listToString( l ): string += item return string -def addtopath( dir ): - # TODO only add if not there already - if dir is not None and dir != "": - os.environ['PATH'] = dir + os.pathsep + os.environ['PATH'] +def addtopath( dirname ): + if dirname is not None and dirname != "": + + # verify that path doesn't already contain the given dirname + tmpdirname = re.escape(dirname) + pattern = r"{sep}{dir}$|^{dir}{sep}|{sep}{dir}{sep}|^{dir}$".format( dir=tmpdirname, sep=os.pathsep) + + match = re.search(pattern, os.environ['PATH']) + if not match: + os.environ['PATH'] = dirname + os.pathsep + os.environ['PATH'] # returns executable path, if it exists def which(program):