r"""OS routines for Mac, NT, or Posix depending on what system we're on.This exports: - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc. - os.path is either posixpath or ntpath - os.name is either 'posix', 'nt', 'os2' or 'ce'. - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') - os.extsep is the extension separator (always '.') - os.altsep is the alternate pathname separator (None or '/') - os.pathsep is the component separator used in $PATH etc - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') - os.defpath is the default search path for executables - os.devnull is the file path of the null device ('/dev/null', etc.)Programs that import and use 'os' stand a better chance of beingportable between different platforms. Of course, they must thenonly use functions that are defined by all platforms (e.g., unlinkand opendir), and leave all pathname manipulation to os.path(e.g., split and join)."""#'importsys,errno_names=sys.builtin_module_names# Note: more names are added to __all__ later.__all__=["altsep","curdir","pardir","sep","pathsep","linesep","defpath","name","path","devnull","SEEK_SET","SEEK_CUR","SEEK_END"]def_get_exports_list(module):try:returnlist(module.__all__)exceptAttributeError:return[nfornindir(module)ifn[0]!='_']if'posix'in_names:name='posix'linesep='\n'fromposiximport*try:fromposiximport_exitexceptImportError:passimportposixpathaspathimportposix__all__.extend(_get_exports_list(posix))delposixelif'nt'in_names:name='nt'linesep='\r\n'fromntimport*try:fromntimport_exitexceptImportError:passimportntpathaspathimportnt__all__.extend(_get_exports_list(nt))delntelif'os2'in_names:name='os2'linesep='\r\n'fromos2import*try:fromos2import_exitexceptImportError:passifsys.version.find('EMX GCC')==-1:importntpathaspathelse:importos2emxpathaspathfrom_emx_linkimportlinkimportos2__all__.extend(_get_exports_list(os2))delos2elif'ce'in_names:name='ce'linesep='\r\n'fromceimport*try:fromceimport_exitexceptImportError:pass# We can use the standard Windows path.importntpathaspathimportce__all__.extend(_get_exports_list(ce))delceelse:raiseImportError('no os specific module found')sys.modules['os.path']=pathfromos.pathimport(curdir,pardir,sep,pathsep,defpath,extsep,altsep,devnull)del_names# Python uses fixed values for the SEEK_ constants; they are mapped# to native constants if necessary in posixmodule.cSEEK_SET=0SEEK_CUR=1SEEK_END=2#'# Super directory utilities.# (Inspired by Eric Raymond; the doc strings are mostly his)defmakedirs(name,mode=0o777):"""makedirs(path [, mode=0o777]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive. """head,tail=path.split(name)ifnottail:head,tail=path.split(head)ifheadandtailandnotpath.exists(head):try:makedirs(head,mode)exceptOSErrorase:# be happy if someone already created the pathife.errno!=errno.EEXIST:raiseiftail==curdir:# xxx/newdir/. exists if xxx/newdir existsreturnmkdir(name,mode)defremovedirs(name):"""removedirs(path) Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored -- they generally mean that a directory was not empty. """rmdir(name)head,tail=path.split(name)ifnottail:head,tail=path.split(head)whileheadandtail:try:rmdir(head)excepterror:breakhead,tail=path.split(head)defrenames(old,new):"""renames(old, new) Super-rename; create directories as necessary and delete any left empty. Works like rename, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned way until either the whole path is consumed or a nonempty directory is found. Note: this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file. """head,tail=path.split(new)ifheadandtailandnotpath.exists(head):makedirs(head)rename(old,new)head,tail=path.split(old)ifheadandtail:try:removedirs(head)excepterror:pass__all__.extend(["makedirs","removedirs","renames"])defwalk(top,topdown=True,onerror=None,followlinks=False):"""Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place (e.g., via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, or to impose a specific order of visiting. Modifying dirnames when topdown is false is ineffective, since the directories in dirnames have already been generated by the time dirnames itself is generated. By default errors from the os.listdir() call are ignored. If optional arg 'onerror' is specified, it should be a function; it will be called with one argument, an os.error instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on systems that support them. In order to get this functionality, set the optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't either. Example: import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end="") print(sum([getsize(join(root, name)) for name in files]), end="") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """fromos.pathimportjoin,isdir,islink# We may not have read permission for top, in which case we can't# get a list of the files the directory contains. os.walk# always suppressed the exception then, rather than blow up for a# minor reason when (say) a thousand readable directories are still# left to visit. That logic is copied here.try:# Note that listdir and error are globals in this module due# to earlier import-*.names=listdir(top)excepterroraserr:ifonerrorisnotNone:onerror(err)returndirs,nondirs=[],[]fornameinnames:ifisdir(join(top,name)):dirs.append(name)else:nondirs.append(name)iftopdown:yieldtop,dirs,nondirsfornameindirs:path=join(top,name)iffollowlinksornotislink(path):forxinwalk(path,topdown,onerror,followlinks):yieldxifnottopdown:yieldtop,dirs,nondirs__all__.append("walk")# Make sure os.environ exists, at leasttry:environexceptNameError:environ={}defexecl(file,*args):"""execl(file, *args) Execute the executable file with argument list args, replacing the current process. """execv(file,args)defexecle(file,*args):"""execle(file, *args, env) Execute the executable file with argument list args and environment env, replacing the current process. """env=args[-1]execve(file,args[:-1],env)defexeclp(file,*args):"""execlp(file, *args) Execute the executable file (which is searched for along $PATH) with argument list args, replacing the current process. """execvp(file,args)defexeclpe(file,*args):"""execlpe(file, *args, env) Execute the executable file (which is searched for along $PATH) with argument list args and environment env, replacing the current process. """env=args[-1]execvpe(file,args[:-1],env)defexecvp(file,args):"""execp(file, args) Execute the executable file (which is searched for along $PATH) with argument list args, replacing the current process. args may be a list or tuple of strings. """_execvpe(file,args)defexecvpe(file,args,env):"""execvpe(file, args, env) Execute the executable file (which is searched for along $PATH) with argument list args and environment env , replacing the current process. args may be a list or tuple of strings. """_execvpe(file,args,env)__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])def_execvpe(file,args,env=None):ifenvisnotNone:func=execveargrest=(args,env)else:func=execvargrest=(args,)env=environhead,tail=path.split(file)ifhead:func(file,*argrest)returnif'PATH'inenv:envpath=env['PATH']else:envpath=defpathPATH=envpath.split(pathsep)last_exc=saved_exc=Nonesaved_tb=NonefordirinPATH:fullname=path.join(dir,file)try:func(fullname,*argrest)excepterrorase:last_exc=etb=sys.exc_info()[2]if(e.errno!=errno.ENOENTande.errno!=errno.ENOTDIRandsaved_excisNone):saved_exc=esaved_tb=tbifsaved_exc:raisesaved_exc.with_traceback(saved_tb)raiselast_exc.with_traceback(tb)# Change environ to automatically call putenv(), unsetenv if they exist.from_abcollimportMutableMapping# Can't use collections (bootstrap)class_Environ(MutableMapping):def__init__(self,environ,keymap,putenv,unsetenv):self.keymap=keymapself.putenv=putenvself.unsetenv=unsetenvself.data=data={}forkey,valueinenviron.items():data[keymap(key)]=str(value)def__getitem__(self,key):returnself.data[self.keymap(key)]def__setitem__(self,key,value):value=str(value)self.putenv(key,value)self.data[self.keymap(key)]=valuedef__delitem__(self,key):self.unsetenv(key)delself.data[self.keymap(key)]def__iter__(self):forkeyinself.data:yieldkeydef__len__(self):returnlen(self.data)defcopy(self):returndict(self)defsetdefault(self,key,value):ifkeynotinself:self[key]=valuereturnself[key]try:_putenv=putenvexceptNameError:_putenv=lambdakey,value:Noneelse:__all__.append("putenv")try:_unsetenv=unsetenvexceptNameError:_unsetenv=lambdakey:_putenv(key,"")else:__all__.append("unsetenv")ifnamein('os2','nt'):# Where Env Var Names Must Be UPPERCASE_keymap=lambdakey:str(key.upper())else:# Where Env Var Names Can Be Mixed Case_keymap=lambdakey:str(key)environ=_Environ(environ,_keymap,_putenv,_unsetenv)defgetenv(key,default=None):"""Get an environment variable, return None if it doesn't exist. The optional second argument can specify an alternate default."""returnenviron.get(key,default)__all__.append("getenv")def_exists(name):returnnameinglobals()# Supply spawn*() (probably only for Unix)if_exists("fork")andnot_exists("spawnv")and_exists("execv"):P_WAIT=0P_NOWAIT=P_NOWAITO=1# XXX Should we support P_DETACH? I suppose it could fork()**2# and close the std I/O streams. Also, P_OVERLAY is the same# as execv*()?def_spawnvef(mode,file,args,env,func):# Internal helper; func is the exec*() function to usepid=fork()ifnotpid:# Childtry:ifenvisNone:func(file,args)else:func(file,args,env)except:_exit(127)else:# Parentifmode==P_NOWAIT:returnpid# Caller is responsible for waiting!while1:wpid,sts=waitpid(pid,0)ifWIFSTOPPED(sts):continueelifWIFSIGNALED(sts):return-WTERMSIG(sts)elifWIFEXITED(sts):returnWEXITSTATUS(sts)else:raiseerror("Not stopped, signaled or exited???")defspawnv(mode,file,args):"""spawnv(mode, file, args) -> integerExecute file with arguments from args in a subprocess.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """return_spawnvef(mode,file,args,None,execv)defspawnve(mode,file,args,env):"""spawnve(mode, file, args, env) -> integerExecute file with arguments from args in a subprocess with thespecified environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """return_spawnvef(mode,file,args,env,execve)# Note: spawnvp[e] is't currently supported on Windowsdefspawnvp(mode,file,args):"""spawnvp(mode, file, args) -> integerExecute file (which is looked for along $PATH) with arguments fromargs in a subprocess.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """return_spawnvef(mode,file,args,None,execvp)defspawnvpe(mode,file,args,env):"""spawnvpe(mode, file, args, env) -> integerExecute file (which is looked for along $PATH) with arguments fromargs in a subprocess with the supplied environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """return_spawnvef(mode,file,args,env,execvpe)if_exists("spawnv"):# These aren't supplied by the basic Windows code# but can be easily implemented in Pythondefspawnl(mode,file,*args):"""spawnl(mode, file, *args) -> integerExecute file with arguments from args in a subprocess.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """returnspawnv(mode,file,args)defspawnle(mode,file,*args):"""spawnle(mode, file, *args, env) -> integerExecute file with arguments from args in a subprocess with thesupplied environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """env=args[-1]returnspawnve(mode,file,args[:-1],env)__all__.extend(["spawnv","spawnve","spawnl","spawnle",])if_exists("spawnvp"):# At the moment, Windows doesn't implement spawnvp[e],# so it won't have spawnlp[e] either.defspawnlp(mode,file,*args):"""spawnlp(mode, file, *args) -> integerExecute file (which is looked for along $PATH) with arguments fromargs in a subprocess with the supplied environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """returnspawnvp(mode,file,args)defspawnlpe(mode,file,*args):"""spawnlpe(mode, file, *args, env) -> integerExecute file (which is looked for along $PATH) with arguments fromargs in a subprocess with the supplied environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """env=args[-1]returnspawnvpe(mode,file,args[:-1],env)__all__.extend(["spawnvp","spawnvpe","spawnlp","spawnlpe",])importcopyregas_copyregdef_make_stat_result(tup,dict):returnstat_result(tup,dict)def_pickle_stat_result(sr):(type,args)=sr.__reduce__()return(_make_stat_result,args)try:_copyreg.pickle(stat_result,_pickle_stat_result,_make_stat_result)exceptNameError:# stat_result may not existpassdef_make_statvfs_result(tup,dict):returnstatvfs_result(tup,dict)def_pickle_statvfs_result(sr):(type,args)=sr.__reduce__()return(_make_statvfs_result,args)try:_copyreg.pickle(statvfs_result,_pickle_statvfs_result,_make_statvfs_result)exceptNameError:# statvfs_result may not existpassifnot_exists("urandom"):defurandom(n):"""urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """try:_urandomfd=open("/dev/urandom",O_RDONLY)except(OSError,IOError):raiseNotImplementedError("/dev/urandom (or equivalent) not found")bs=b""whilelen(bs)<n:bs+=read(_urandomfd,n-len(bs))close(_urandomfd)returnbs# Supply os.popen()defpopen(cmd,mode="r",buffering=None):ifnotisinstance(cmd,str):raiseTypeError("invalid cmd type (%s, expected string)"%type(cmd))ifmodenotin("r","w"):raiseValueError("invalid mode %r"%mode)importsubprocess,ioifmode=="r":proc=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,bufsize=buffering)return_wrap_close(io.TextIOWrapper(proc.stdout),proc)else:proc=subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,bufsize=buffering)return_wrap_close(io.TextIOWrapper(proc.stdin),proc)# Helper for popen() -- a proxy for a file whose close waits for the processclass_wrap_close:def__init__(self,stream,proc):self._stream=streamself._proc=procdefclose(self):self._stream.close()returnself._proc.wait()<<8# Shift left to match old behaviordef__getattr__(self,name):returngetattr(self._stream,name)def__iter__(self):returniter(self._stream)# Supply os.fdopen()deffdopen(fd,*args,**kwargs):ifnotisinstance(fd,int):raiseTypeError("invalid fd type (%s, expected integer)"%type(fd))importioreturnio.open(fd,*args,**kwargs)