<s> import os <EOL> from os . path import isabs , islink , abspath , normpath <EOL> from rpython . rlib . rpath import rabspath <EOL> from rpython . rlib . objectmodel import enforceargs <EOL> @ enforceargs ( str ) <EOL> def dirname ( p ) : <EOL> """<STR_LIT>""" <EOL> i = p . rfind ( '<STR_LIT:/>' ) + <NUM_LIT:1> <EOL> assert i >= <NUM_LIT:0> <EOL> head = p [ : i ] <EOL> if head and head != '<STR_LIT:/>' * len ( head ) : <EOL> head = head . rstrip ( '<STR_LIT:/>' ) <EOL> return head <EOL> @ enforceargs ( str ) <EOL> def basename ( p ) : <EOL> """<STR_LIT>""" <EOL> i = p . rfind ( '<STR_LIT:/>' ) + <NUM_LIT:1> <EOL> assert i >= <NUM_LIT:0> <EOL> return p [ i : ] <EOL> @ enforceargs ( str ) <EOL> def split ( p ) : <EOL> """<STR_LIT>""" <EOL> i = p . rfind ( '<STR_LIT:/>' ) + <NUM_LIT:1> <EOL> assert i >= <NUM_LIT:0> <EOL> head , tail = p [ : i ] , p [ i : ] <EOL> if head and head != '<STR_LIT:/>' * len ( head ) : <EOL> head = head . rstrip ( '<STR_LIT:/>' ) <EOL> return head , tail <EOL> @ enforceargs ( str ) <EOL> def exists ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> assert path is not None <EOL> os . stat ( path ) <EOL> except os . error : <EOL> return False <EOL> return True <EOL> @ enforceargs ( str , [ str ] ) <EOL> def join ( a , p ) : <EOL> """<STR_LIT>""" <EOL> path = a <EOL> for b in p : <EOL> if b . startswith ( '<STR_LIT:/>' ) : <EOL> path = b <EOL> elif path == '<STR_LIT>' or path . endswith ( '<STR_LIT:/>' ) : <EOL> path += b <EOL> else : <EOL> path += '<STR_LIT:/>' + b <EOL> return path <EOL> @ enforceargs ( str ) <EOL> def realpath ( filename ) : <EOL> """<STR_LIT>""" <EOL> if isabs ( filename ) : <EOL> bits = [ '<STR_LIT:/>' ] + filename . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> bits = [ '<STR_LIT>' ] + filename . split ( '<STR_LIT:/>' ) <EOL> for i in range ( <NUM_LIT:2> , len ( bits ) + <NUM_LIT:1> ) : <EOL> component = join ( bits [ <NUM_LIT:0> ] , bits [ <NUM_LIT:1> : i ] ) <EOL> if islink ( component ) : <EOL> resolved = _resolve_link ( component ) <EOL> if resolved is None : <EOL> return abspath ( join ( component , bits [ i : ] ) ) <EOL> else : <EOL> newpath = join ( resolved , bits [ i : ] ) <EOL> return realpath ( newpath ) <EOL> return abspath ( filename ) <EOL> @ enforceargs ( str ) <EOL> def _resolve_link ( path ) : <EOL> """<STR_LIT>""" <EOL> paths_seen = { } <EOL> while islink ( path ) : <EOL> if path in paths_seen : <EOL> return None <EOL> paths_seen [ path ] = None <EOL> resolved = os . readlink ( path ) <EOL> if not isabs ( resolved ) : <EOL> dir = dirname ( path ) <EOL> path = normpath ( join ( dir , [ resolved ] ) ) <EOL> else : <EOL> path = normpath ( resolved ) <EOL> return path </s>
