<s> git_refnames = "<STR_LIT>" <EOL> git_full = "<STR_LIT>" <EOL> tag_prefix = "<STR_LIT>" <EOL> parentdir_prefix = "<STR_LIT>" <EOL> versionfile_source = "<STR_LIT>" <EOL> import os , sys , re , subprocess , errno <EOL> def run_command ( commands , args , cwd = None , verbose = False , hide_stderr = False ) : <EOL> assert isinstance ( commands , list ) <EOL> p = None <EOL> for c in commands : <EOL> try : <EOL> p = subprocess . Popen ( [ c ] + args , cwd = cwd , stdout = subprocess . PIPE , <EOL> stderr = ( subprocess . PIPE if hide_stderr <EOL> else None ) ) <EOL> break <EOL> except EnvironmentError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . errno == errno . ENOENT : <EOL> continue <EOL> if verbose : <EOL> print ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> print ( e ) <EOL> return None <EOL> else : <EOL> if verbose : <EOL> print ( "<STR_LIT>" % ( commands , ) ) <EOL> return None <EOL> stdout = p . communicate ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> if sys . version >= '<STR_LIT:3>' : <EOL> stdout = stdout . decode ( ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> if verbose : <EOL> print ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> return None <EOL> return stdout <EOL> def versions_from_parentdir ( parentdir_prefix , root , verbose = False ) : <EOL> dirname = os . path . basename ( root ) <EOL> if not dirname . startswith ( parentdir_prefix ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" % <EOL> ( root , dirname , parentdir_prefix ) ) <EOL> return None <EOL> return { "<STR_LIT:version>" : dirname [ len ( parentdir_prefix ) : ] , "<STR_LIT>" : "<STR_LIT>" } <EOL> def git_get_keywords ( versionfile_abs ) : <EOL> keywords = { } <EOL> try : <EOL> f = open ( versionfile_abs , "<STR_LIT:r>" ) <EOL> for line in f . readlines ( ) : <EOL> if line . strip ( ) . startswith ( "<STR_LIT>" ) : <EOL> mo = re . search ( r'<STR_LIT>' , line ) <EOL> if mo : <EOL> keywords [ "<STR_LIT>" ] = mo . group ( <NUM_LIT:1> ) <EOL> if line . strip ( ) . startswith ( "<STR_LIT>" ) : <EOL> mo = re . search ( r'<STR_LIT>' , line ) <EOL> if mo : <EOL> keywords [ "<STR_LIT>" ] = mo . group ( <NUM_LIT:1> ) <EOL> f . close ( ) <EOL> except EnvironmentError : <EOL> pass <EOL> return keywords <EOL> def git_versions_from_keywords ( keywords , tag_prefix , verbose = False ) : <EOL> if not keywords : <EOL> return { } <EOL> refnames = keywords [ "<STR_LIT>" ] . strip ( ) <EOL> if refnames . startswith ( "<STR_LIT>" ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" ) <EOL> return { } <EOL> refs = set ( [ r . strip ( ) for r in refnames . strip ( "<STR_LIT>" ) . split ( "<STR_LIT:U+002C>" ) ] ) <EOL> TAG = "<STR_LIT>" <EOL> tags = set ( [ r [ len ( TAG ) : ] for r in refs if r . startswith ( TAG ) ] ) <EOL> if not tags : <EOL> tags = set ( [ r for r in refs if re . search ( r'<STR_LIT>' , r ) ] ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( refs - tags ) ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( sorted ( tags ) ) ) <EOL> for ref in sorted ( tags ) : <EOL> if ref . startswith ( tag_prefix ) : <EOL> r = ref [ len ( tag_prefix ) : ] <EOL> if verbose : <EOL> print ( "<STR_LIT>" % r ) <EOL> return { "<STR_LIT:version>" : r , <EOL> "<STR_LIT>" : keywords [ "<STR_LIT>" ] . strip ( ) } <EOL> if verbose : <EOL> print ( "<STR_LIT>" ) <EOL> return { "<STR_LIT:version>" : keywords [ "<STR_LIT>" ] . strip ( ) , <EOL> "<STR_LIT>" : keywords [ "<STR_LIT>" ] . strip ( ) } <EOL> def git_versions_from_vcs ( tag_prefix , root , verbose = False ) : <EOL> if not os . path . exists ( os . path . join ( root , "<STR_LIT>" ) ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" % root ) <EOL> return { } <EOL> GITS = [ "<STR_LIT>" ] <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> GITS = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> stdout = run_command ( GITS , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> cwd = root ) <EOL> if stdout is None : <EOL> return { } <EOL> if not stdout . startswith ( tag_prefix ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" % ( stdout , tag_prefix ) ) <EOL> return { } <EOL> tag = stdout [ len ( tag_prefix ) : ] <EOL> stdout = run_command ( GITS , [ "<STR_LIT>" , "<STR_LIT>" ] , cwd = root ) <EOL> if stdout is None : <EOL> return { } <EOL> full = stdout . strip ( ) <EOL> if tag . endswith ( "<STR_LIT>" ) : <EOL> full += "<STR_LIT>" <EOL> return { "<STR_LIT:version>" : tag , "<STR_LIT>" : full } <EOL> def get_versions ( default = { "<STR_LIT:version>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , verbose = False ) : <EOL> keywords = { "<STR_LIT>" : git_refnames , "<STR_LIT>" : git_full } <EOL> ver = git_versions_from_keywords ( keywords , tag_prefix , verbose ) <EOL> if ver : <EOL> return ver <EOL> try : <EOL> root = os . path . abspath ( __file__ ) <EOL> for i in range ( len ( versionfile_source . split ( os . sep ) ) ) : <EOL> root = os . path . dirname ( root ) <EOL> except NameError : <EOL> return default <EOL> return ( git_versions_from_vcs ( tag_prefix , root , verbose ) <EOL> or versions_from_parentdir ( parentdir_prefix , root , verbose ) <EOL> or default ) </s>
