#NEWLINE# def compat_expanduser( path ) : #NEWLINE# #INDENT# 'Expand ~ and ~user constructions.  If user or $HOME is unknown,\n            do nothing.' #NEWLINE# if ( not path . startswith ( '~' ) ) : #NEWLINE# #INDENT# return path #NEWLINE# #UNINDENT# i = path . find ( '/' , 1 ) #NEWLINE# if ( i < 0 ) : #NEWLINE# #INDENT# i = len ( path ) #NEWLINE# #UNINDENT# if ( i == 1 ) : #NEWLINE# #INDENT# if ( 'HOME' not in os . environ ) : #NEWLINE# #INDENT# import pwd #NEWLINE# userhome = pwd . getpwuid ( os . getuid ( ) ) . pw_dir #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# userhome = compat_getenv ( 'HOME' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# import pwd #NEWLINE# try : #NEWLINE# #INDENT# pwent = pwd . getpwnam ( path [ 1 : i ] ) #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# return path #NEWLINE# #UNINDENT# userhome = pwent . pw_dir #NEWLINE# #UNINDENT# userhome = userhome . rstrip ( '/' ) #NEWLINE# return ( ( userhome + path [ i : ] ) or '/' )