""" """ import sys, os, re import distutils.sysconfig if sys.platform == 'win32': try: import subprocess except: sys.exit( '' ) svnBranch='trunk' def findCipresSource( branchToCheckout='trunk' ): cipresTop = None hadCIPRES = os.path.exists( 'cipres' ) if hadCIPRES: cipresTop = 'cipres' else: cipresTop = findHighestVersionedDir( 'cipres' ) return cipresTop def findHighestVersionedDir( prefix ): '''Looks for prefix-#.#.# pattern in current directory using the regex ^prefix-?[0-9.]*$ Returns the directory with the highest version numer (or None if there are no matches)''' dirTuples = findAllVersionedDir( prefix ) if not dirTuples: return None currHighest = dirTuples[0] for d in dirTuples[1:]: if versionTupleCmp( currHighest[1], d[1] ) < 0: currHighest = d return currHighest[0] def findAllVersionedDir( prefix ): '''Looks for prefix-#.#.# pattern in current directory using the regex ^prefix-?[0-9.]*$ Returns list tuples: [(dir-name, tuple of version numbers), ] of all version-numbered directories found.''' p = re.compile( '^%s' % prefix + r'-([0-9.]+)$' ) children = os.listdir( os.path.curdir ) matches = [] for c in children: m = p.match( c ) if m: matches.append( ( c, m.group( 1 ).split( '.' ) ) ) return matches def versionTupleCmp( f, s ): """__cmp__ function for version tuples. cannot use lexigraphic cmp because: 1.1 < 1.04 can't convert the whole thing to a decimal because 1.10 > 1.1.11""" if len( f ) == 0: return len( s ) != 0 and -1 or 0 for ind, v in enumerate( f ): if ind+1 > len( s ): return 1 r = cmp( v, s[ind] ) if r != 0: return r if len( s ) > len( f ): return -1 return 0 if __name__ == '__main__': cipresTop = findCipresSource( svnBranch ) if cipresTop: print cipresTop