<s> """<STR_LIT>""" <EOL> import os <EOL> import atexit <EOL> from panda3d . core import Filename , VirtualFileSystem , get_model_path <EOL> from panda3d . core import VirtualFileMountRamdisk <EOL> from direct . stdpy . file import join , isdir , isfile <EOL> from rpcore . rpobject import RPObject <EOL> class MountManager ( RPObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pipeline ) : <EOL> """<STR_LIT>""" <EOL> RPObject . __init__ ( self ) <EOL> self . _pipeline = pipeline <EOL> self . _base_path = self . _find_basepath ( ) <EOL> self . _lock_file = "<STR_LIT>" <EOL> self . _model_paths = [ ] <EOL> self . _write_path = None <EOL> self . _mounted = False <EOL> self . _do_cleanup = True <EOL> self . _config_dir = None <EOL> self . debug ( "<STR_LIT>" , self . _base_path ) <EOL> atexit . register ( self . _on_exit_cleanup ) <EOL> @ property <EOL> def write_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _write_path <EOL> @ write_path . setter <EOL> def write_path ( self , pth ) : <EOL> """<STR_LIT>""" <EOL> if pth is None : <EOL> self . _write_path = None <EOL> self . _lock_file = "<STR_LIT>" <EOL> else : <EOL> self . _write_path = Filename . from_os_specific ( pth ) . get_fullpath ( ) <EOL> self . _lock_file = join ( self . _write_path , "<STR_LIT>" ) <EOL> @ property <EOL> def base_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _base_path <EOL> @ base_path . setter <EOL> def base_path ( self , pth ) : <EOL> """<STR_LIT>""" <EOL> self . debug ( "<STR_LIT>" + pth + "<STR_LIT:'>" ) <EOL> self . _base_path = Filename . from_os_specific ( pth ) . get_fullpath ( ) <EOL> @ property <EOL> def config_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> @ config_dir . setter <EOL> def config_dir ( self , pth ) : <EOL> """<STR_LIT>""" <EOL> self . _config_dir = Filename . from_os_specific ( pth ) . get_fullpath ( ) <EOL> @ property <EOL> def do_cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _do_cleanup <EOL> @ do_cleanup . setter <EOL> def do_cleanup ( self , cleanup ) : <EOL> """<STR_LIT>""" <EOL> self . _do_cleanup = cleanup <EOL> def get_lock ( self ) : <EOL> """<STR_LIT>""" <EOL> if isfile ( self . _lock_file ) : <EOL> try : <EOL> with open ( self . _lock_file , "<STR_LIT:r>" ) as handle : <EOL> pid = int ( handle . readline ( ) ) <EOL> except IOError as msg : <EOL> self . error ( "<STR_LIT>" , msg ) <EOL> return False <EOL> if self . _is_pid_running ( pid ) : <EOL> self . error ( "<STR_LIT>" ) <EOL> return False <EOL> self . _write_lock ( ) <EOL> return True <EOL> else : <EOL> self . _write_lock ( ) <EOL> return True <EOL> def _find_basepath ( self ) : <EOL> """<STR_LIT>""" <EOL> pth = os . path . abspath ( join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , "<STR_LIT:..>" ) ) <EOL> return Filename . from_os_specific ( pth ) . get_fullpath ( ) <EOL> def _is_pid_running ( self , pid ) : <EOL> """<STR_LIT>""" <EOL> if os . name == '<STR_LIT>' : <EOL> import errno <EOL> if pid < <NUM_LIT:0> : <EOL> return False <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> except OSError as err : <EOL> return err . errno == errno . EPERM <EOL> else : <EOL> return True <EOL> else : <EOL> import ctypes <EOL> kernel32 = ctypes . windll . kernel32 <EOL> process = kernel32 . OpenProcess ( <NUM_LIT> , <NUM_LIT:0> , pid ) <EOL> if process != <NUM_LIT:0> : <EOL> kernel32 . CloseHandle ( process ) <EOL> return True <EOL> else : <EOL> return False <EOL> def _write_lock ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( self . _lock_file , "<STR_LIT:w>" ) as handle : <EOL> handle . write ( str ( os . getpid ( ) ) ) <EOL> def _try_remove ( self , fname ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . remove ( fname ) <EOL> return True <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> return False <EOL> def _on_exit_cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _do_cleanup : <EOL> self . debug ( "<STR_LIT>" ) <EOL> if self . _write_path is not None : <EOL> self . _try_remove ( self . _lock_file ) <EOL> for fname in os . listdir ( self . _write_path ) : <EOL> pth = join ( self . _write_path , fname ) <EOL> if isfile ( pth ) and fname . startswith ( "<STR_LIT>" ) : <EOL> self . _try_remove ( pth ) <EOL> if len ( os . listdir ( self . _write_path ) ) < <NUM_LIT:1> : <EOL> try : <EOL> os . removedirs ( self . _write_path ) <EOL> except IOError : <EOL> pass <EOL> @ property <EOL> def is_mounted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _mounted <EOL> def mount ( self ) : <EOL> """<STR_LIT>""" <EOL> self . debug ( "<STR_LIT>" ) <EOL> self . _mounted = True <EOL> def convert_path ( pth ) : <EOL> return Filename . from_os_specific ( pth ) . get_fullpath ( ) <EOL> vfs = VirtualFileSystem . get_global_ptr ( ) <EOL> if self . _config_dir is None : <EOL> config_dir = convert_path ( join ( self . _base_path , "<STR_LIT>" ) ) <EOL> self . debug ( "<STR_LIT>" , config_dir ) <EOL> vfs . mount ( config_dir , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> else : <EOL> self . debug ( "<STR_LIT>" , self . _config_dir ) <EOL> vfs . mount ( convert_path ( self . _config_dir ) , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> vfs . mount ( convert_path ( self . _base_path ) , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> vfs . mount ( convert_path ( join ( self . _base_path , "<STR_LIT>" ) ) , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> vfs . mount ( convert_path ( join ( self . _base_path , "<STR_LIT>" ) ) , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> if self . _write_path is None : <EOL> self . debug ( "<STR_LIT>" ) <EOL> vfs . mount ( VirtualFileMountRamdisk ( ) , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> else : <EOL> if not isdir ( self . _write_path ) : <EOL> self . debug ( "<STR_LIT>" ) <EOL> try : <EOL> os . makedirs ( self . _write_path ) <EOL> except IOError as msg : <EOL> self . fatal ( "<STR_LIT>" , msg ) <EOL> self . debug ( "<STR_LIT>" , self . _write_path , "<STR_LIT>" ) <EOL> vfs . mount ( convert_path ( self . _write_path ) , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> get_model_path ( ) . prepend_directory ( "<STR_LIT>" ) <EOL> get_model_path ( ) . prepend_directory ( "<STR_LIT>" ) <EOL> get_model_path ( ) . prepend_directory ( "<STR_LIT>" ) <EOL> def unmount ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) </s>
