def makeScriptPathRelative( scriptFilepath ): ''' will attempt to transform the name of the given script into the shortest possible path relative to the python search paths defined in sys.path. For example, just say you have a package called "foo" this package contains the script: "bar.py" given the full path to bar.py this function will return: "foo/bar.py" ''' scriptFilepath = Path( scriptFilepath ) sysPaths = map( Path, sys.path ) bestFitPath = None for p in sysPaths: if scriptFilepath.isUnder( p ) or len( p ) > len( bestFitPath ): if bestFitPath is None: bestFitPath = p if bestFitPath is None: raise ValueError( "Cannot find a path under any of the paths in sys.path!" ) shortestPath = scriptFilepath - bestFitPath return shortestPath
def isScriptInSuperiorBranch( scriptPath ): ''' returns whether the given scriptPath can be found in a directory searched before the given script. Ie, if the following paths are in sys.path: sys.path = [ 'd:/somePath', 'd:/otherPath' ] isScriptInSuperiorBranch( 'd:/otherPath/someScript.py' ) if there is a someScript.py in d:/somePath, this function will return True ''' if not isinstance( scriptPath, Path ): scriptPath = Path( scriptPath ) originalPath = scriptPath for p in sys.path: if scriptPath.isUnder( p ): scriptPath = scriptPath - p break for p in sys.path: possibleSuperiorPath = p / scriptPath if possibleSuperiorPath.exists: if possibleSuperiorPath == originalPath: return None return possibleSuperiorPath
def getFiles( self ): files = [] for d in self._dirs: skipDir = False for dd in self._dirsExclude: if d.isUnder( dd ): skipDir = True break if skipDir: continue for dirPath, dirNames, fileNames in os.walk( d ): dirPath = Path( dirPath ) skipDir = False for d in self._dirsExclude: if dirPath.isUnder( d ): skipDir = True break if skipDir: continue for f in fileNames: f = dirPath / f if f.hasExtension( 'py' ): files.append( f ) #if the cmd script looks like its a python cmd script, then add it to the list - the DepFinder class knows how to deal with these files elif f.hasExtension( 'cmd' ): with file( f ) as fopen: line = fopen.readline().strip() if '@setlocal ' in line and '& python' in line: files.append( f ) return files