<s> """<STR_LIT>""" <EOL> import pickle <EOL> import cPickle <EOL> import copy <EOL> import copy_reg <EOL> import datetime <EOL> import fnmatch <EOL> import inspect <EOL> import modulefinder <EOL> import os . path <EOL> import pkg_resources <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import types <EOL> from zope . interface . interface import InterfaceClass <EOL> from openmdao . util . log import NullLogger , LOG_DEBUG2 <EOL> from openmdao . util import eggobserver , eggwriter <EOL> from openmdao . util . fileutil import onerror <EOL> SAVE_PICKLE = <NUM_LIT:3> <EOL> SAVE_CPICKLE = <NUM_LIT:4> <EOL> def _pickle_method ( method ) : <EOL> """<STR_LIT>""" <EOL> name = method . __name__ <EOL> im_self = method . im_self <EOL> if im_self is not None : <EOL> im_class = None <EOL> else : <EOL> im_class = method . im_class <EOL> if im_class . __module__ == '<STR_LIT:__main__>' : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % ( method , im_self ) ) <EOL> return _unpickle_method , ( name , im_self , im_class ) <EOL> def _unpickle_method ( name , im_self , im_class ) : <EOL> """<STR_LIT>""" <EOL> if im_self is not None : <EOL> return getattr ( im_self , name ) <EOL> else : <EOL> return getattr ( im_class , name ) <EOL> copy_reg . pickle ( types . MethodType , _pickle_method , _unpickle_method ) <EOL> def save_to_egg ( entry_pts , version = None , py_dir = None , src_dir = None , <EOL> src_files = None , dst_dir = None , logger = None , observer = None , <EOL> need_requirements = True ) : <EOL> """<STR_LIT>""" <EOL> root , name , group = entry_pts [ <NUM_LIT:0> ] <EOL> logger = logger or NullLogger ( ) <EOL> observer = eggobserver . EggObserver ( observer , logger ) <EOL> orig_dir = os . getcwd ( ) <EOL> if not py_dir : <EOL> py_dir = orig_dir <EOL> else : <EOL> py_dir = os . path . realpath ( py_dir ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> py_dir = py_dir . lower ( ) <EOL> if src_dir : <EOL> src_dir = os . path . realpath ( src_dir ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> src_dir = src_dir . lower ( ) <EOL> src_files = src_files or set ( ) <EOL> if not version : <EOL> now = datetime . datetime . now ( ) <EOL> version = '<STR_LIT>' % ( now . year , now . month , now . day , now . hour , now . minute ) <EOL> dst_dir = dst_dir or orig_dir <EOL> if not os . access ( dst_dir , os . W_OK ) : <EOL> msg = "<STR_LIT>" % dst_dir <EOL> observer . exception ( msg ) <EOL> raise IOError ( msg ) <EOL> egg_name = eggwriter . egg_filename ( name , version ) <EOL> logger . debug ( '<STR_LIT>' , egg_name , orig_dir ) <EOL> _fix_main ( logger ) <EOL> objs = _get_objects ( root , logger ) <EOL> _check_objects ( objs , logger , observer ) <EOL> _verify_objects ( root , logger , observer ) <EOL> tmp_dir = None <EOL> try : <EOL> if need_requirements : <EOL> required_distributions , local_modules , orphan_modules = _get_distributions ( objs , py_dir , logger , observer ) <EOL> else : <EOL> required_distributions = set ( ) <EOL> local_modules = set ( ) <EOL> orphan_modules = set ( ) <EOL> for dirpath , dirnames , filenames in os . walk ( py_dir ) : <EOL> dirs = copy . copy ( dirnames ) <EOL> for path in dirs : <EOL> if not os . path . exists ( os . path . join ( dirpath , path , <EOL> '<STR_LIT>' ) ) : <EOL> dirnames . remove ( path ) <EOL> for path in filenames : <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> local_modules . add ( os . path . join ( dirpath , path ) ) <EOL> root_mod = root . __class__ . __module__ <EOL> root_mod = sys . modules [ root_mod ] . __file__ <EOL> if root_mod . endswith ( '<STR_LIT>' ) or root_mod . endswith ( '<STR_LIT>' ) : <EOL> root_mod = root_mod [ : - <NUM_LIT:1> ] <EOL> local_modules . add ( os . path . abspath ( root_mod ) ) <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' , py_dir ) <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' , src_dir ) <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' ) <EOL> for module in sorted ( local_modules ) : <EOL> mod = module <EOL> if mod . startswith ( py_dir ) : <EOL> mod = mod [ len ( py_dir ) + <NUM_LIT:1> : ] <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' , mod ) <EOL> tmp_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , dir = tmp_dir ) <EOL> os . chdir ( tmp_dir ) <EOL> os . mkdir ( name ) <EOL> cleanup_files = [ ] <EOL> try : <EOL> if src_dir : <EOL> for path in src_files : <EOL> subdir = os . path . dirname ( path ) <EOL> if subdir : <EOL> subdir = os . path . join ( name , subdir ) <EOL> if not os . path . exists ( subdir ) : <EOL> os . makedirs ( subdir ) <EOL> src = os . path . join ( src_dir , path ) <EOL> dst = os . path . join ( name , path ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> shutil . copy2 ( src , dst ) <EOL> else : <EOL> os . symlink ( src , dst ) <EOL> for path in local_modules : <EOL> if not os . path . exists ( <EOL> os . path . join ( name , os . path . basename ( path ) ) ) : <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . join ( py_dir , path ) <EOL> shutil . copy ( path , name ) <EOL> entry_info = [ ] <EOL> for obj , obj_name , obj_group in entry_pts : <EOL> clean_name = obj_name <EOL> if clean_name . startswith ( name + '<STR_LIT:.>' ) : <EOL> clean_name = clean_name [ len ( name ) + <NUM_LIT:1> : ] <EOL> clean_name = clean_name . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> state_name , state_path = _write_state_file ( name , obj , clean_name , logger , observer ) <EOL> src_files . add ( state_name ) <EOL> cleanup_files . append ( state_path ) <EOL> loader = '<STR_LIT>' % clean_name <EOL> loader_path = os . path . join ( name , loader + '<STR_LIT>' ) <EOL> cleanup_files . append ( loader_path ) <EOL> _write_loader_script ( loader_path , state_name , name , obj is root ) <EOL> entry_info . append ( ( obj_group , obj_name , loader ) ) <EOL> init_path = os . path . join ( name , '<STR_LIT>' ) <EOL> if not os . path . exists ( init_path ) : <EOL> cleanup_files . append ( init_path ) <EOL> out = open ( init_path , '<STR_LIT:w>' ) <EOL> out . close ( ) <EOL> doc = root . __doc__ or '<STR_LIT>' <EOL> entry_map = _create_entry_map ( entry_info ) <EOL> orphans = [ mod for mod , path in orphan_modules ] <EOL> eggwriter . write ( name , version , doc , entry_map , <EOL> src_files , required_distributions , <EOL> orphans , dst_dir , logger , observer . observer ) <EOL> finally : <EOL> for path in cleanup_files : <EOL> if os . path . exists ( path ) : <EOL> os . remove ( path ) <EOL> finally : <EOL> os . chdir ( orig_dir ) <EOL> if tmp_dir : <EOL> shutil . rmtree ( tmp_dir , onerror = onerror ) <EOL> return ( egg_name , required_distributions , orphan_modules ) <EOL> def _fix_main ( logger ) : <EOL> """<STR_LIT>""" <EOL> mod = sys . modules [ '<STR_LIT:__main__>' ] <EOL> mod_name , dot , ext = os . path . basename ( mod . __file__ ) . rpartition ( '<STR_LIT:.>' ) <EOL> if mod_name not in sys . modules : <EOL> logger . info ( '<STR_LIT>' , mod_name , mod . __file__ ) <EOL> sys . modules [ mod_name ] = mod <EOL> for name , value in inspect . getmembers ( mod ) : <EOL> if inspect . isclass ( value ) and value . __module__ == '<STR_LIT:__main__>' : <EOL> value . __module__ = mod_name <EOL> if inspect . isfunction ( value ) and hasattr ( value , '<STR_LIT>' ) and value . __module__ == '<STR_LIT:__main__>' : <EOL> value . __module__ = mod_name <EOL> def _get_objects ( root , logger ) : <EOL> """<STR_LIT>""" <EOL> def _recurse_get_objects ( obj , objs , visited , logger ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> state = obj . __getstate__ ( ) <EOL> except AttributeError : <EOL> try : <EOL> state = obj . __dict__ <EOL> except AttributeError : <EOL> if isinstance ( obj , ( dict , list , set , tuple ) ) : <EOL> state = obj <EOL> else : <EOL> return <EOL> container = state <EOL> except Exception as exc : <EOL> logger . error ( "<STR_LIT>" , <EOL> type ( obj ) , exc ) <EOL> return <EOL> else : <EOL> try : <EOL> container = obj . __dict__ <EOL> except AttributeError : <EOL> container = None <EOL> if isinstance ( state , dict ) : <EOL> indices = state . keys ( ) <EOL> state = state . values ( ) <EOL> else : <EOL> indices = range ( len ( state ) ) <EOL> for obj , index in zip ( state , indices ) : <EOL> if id ( obj ) in visited : <EOL> continue <EOL> visited . add ( id ( obj ) ) <EOL> objs . append ( ( obj , container , index ) ) <EOL> if not inspect . isclass ( obj ) and not isinstance ( obj , InterfaceClass ) : <EOL> _recurse_get_objects ( obj , objs , visited , logger ) <EOL> objs = [ ( root , None , None ) ] <EOL> visited = set ( ) <EOL> visited . add ( id ( root ) ) <EOL> _recurse_get_objects ( root , objs , visited , logger ) <EOL> return objs <EOL> def _check_objects ( objs , logger , observer ) : <EOL> """<STR_LIT>""" <EOL> errors = <NUM_LIT:0> <EOL> for obj , container , index in objs : <EOL> try : <EOL> cls = obj . __class__ <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> classname = cls . __name__ <EOL> if classname == '<STR_LIT>' and not hasattr ( sys . modules [ obj . __module__ ] , obj . __name__ ) : <EOL> logger . error ( <EOL> "<STR_LIT>" , <EOL> obj . __module__ , obj . __name__ ) <EOL> errors += <NUM_LIT:1> <EOL> if errors : <EOL> plural = '<STR_LIT:s>' if errors > <NUM_LIT:1> else '<STR_LIT>' <EOL> msg = "<STR_LIT>" % ( errors , plural ) <EOL> observer . exception ( msg ) <EOL> raise RuntimeError ( msg ) <EOL> def _verify_objects ( root , logger , observer ) : <EOL> """<STR_LIT>""" <EOL> objs = _get_objects ( root , logger ) <EOL> for obj , container , index in objs : <EOL> if obj . __class__ . __name__ == '<STR_LIT>' : <EOL> continue <EOL> try : <EOL> mod = obj . __module__ <EOL> except AttributeError : <EOL> continue <EOL> if mod == '<STR_LIT:__main__>' : <EOL> msg = "<STR_LIT>" "<STR_LIT>" % ( obj , container , index ) <EOL> observer . exception ( msg ) <EOL> raise RuntimeError ( msg ) <EOL> def _get_distributions ( objs , py_dir , logger , observer ) : <EOL> """<STR_LIT>""" <EOL> distributions = set ( ) <EOL> local_modules = set ( ) <EOL> orphans = set ( ) <EOL> modules = set ( [ '<STR_LIT>' ] ) <EOL> prefixes = set ( ) <EOL> not_found = set ( ) <EOL> if _get_distributions . excludes is None : <EOL> _get_distributions . excludes = _get_standard_modules ( ) <EOL> for obj , container , index in objs : <EOL> try : <EOL> name = obj . __module__ <EOL> except AttributeError : <EOL> continue <EOL> if name is None or name in modules : <EOL> continue <EOL> modules . add ( name ) <EOL> try : <EOL> path = os . path . realpath ( sys . modules [ name ] . __file__ ) <EOL> except AttributeError : <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' , name ) <EOL> continue <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> orig_path = path <EOL> path = path . lower ( ) <EOL> found = False <EOL> for prefix in prefixes : <EOL> if path . startswith ( prefix ) : <EOL> found = True <EOL> break <EOL> if found : <EOL> continue <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> path = orig_path <EOL> egg = path . find ( '<STR_LIT>' ) <EOL> if egg > <NUM_LIT:0> : <EOL> path = path [ : egg + <NUM_LIT:4> ] <EOL> _process_egg ( path , distributions , prefixes , logger ) <EOL> else : <EOL> if path . endswith ( '<STR_LIT>' ) or path . endswith ( '<STR_LIT>' ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> if not os . path . exists ( path ) : <EOL> logger . warning ( "<STR_LIT>" , path ) <EOL> continue <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . join ( os . getcwd ( ) , path ) <EOL> finder_info = None <EOL> try : <EOL> finder_info = _DistCache . lookup ( path ) <EOL> except KeyError : <EOL> observer . analyze ( path ) <EOL> finder = modulefinder . ModuleFinder ( <EOL> excludes = _get_distributions . excludes ) <EOL> try : <EOL> finder . run_script ( path ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" % path ) <EOL> name , dot , ext = os . path . basename ( path ) . partition ( '<STR_LIT:.>' ) <EOL> finder_info = [ ( name , path ) ] <EOL> else : <EOL> finder_info = [ ] <EOL> for name , module in finder . modules . items ( ) : <EOL> try : <EOL> filename = module . __file__ <EOL> except AttributeError : <EOL> filename = None <EOL> finder_info . append ( ( name , filename ) ) <EOL> _DistCache . record ( path , finder_info ) <EOL> else : <EOL> logger . log ( LOG_DEBUG2 , "<STR_LIT>" , path ) <EOL> if finder_info : <EOL> _process_found_modules ( py_dir , finder_info , modules , <EOL> distributions , prefixes , local_modules , <EOL> orphans , not_found , logger ) <EOL> _DistCache . save ( ) <EOL> distributions = sorted ( distributions , key = lambda dist : dist . project_name ) <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' ) <EOL> for dist in distributions : <EOL> logger . log ( LOG_DEBUG2 , '<STR_LIT>' , dist . project_name , dist . version ) <EOL> return ( distributions , local_modules , orphans ) <EOL> _get_distributions . excludes = None <EOL> def _get_standard_modules ( ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> lib_dir = os . path . join ( sys . prefix , '<STR_LIT>' ) <EOL> obj_dir = os . path . join ( sys . prefix , '<STR_LIT>' ) <EOL> obj_pattern = '<STR_LIT>' <EOL> roots = [ lib_dir , obj_dir ] <EOL> else : <EOL> lib_dir = os . path . join ( sys . prefix , '<STR_LIT>' , '<STR_LIT>' + sys . version [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> obj_dir = '<STR_LIT>' <EOL> obj_pattern = '<STR_LIT>' <EOL> roots = [ lib_dir ] <EOL> orig_prefix = None <EOL> try : <EOL> with open ( os . path . join ( lib_dir , '<STR_LIT>' ) , '<STR_LIT:r>' ) as inp : <EOL> orig_prefix = inp . read ( ) <EOL> except IOError as error : <EOL> pass <EOL> if orig_prefix : <EOL> for root in tuple ( roots ) : <EOL> roots . append ( orig_prefix + root [ len ( sys . prefix ) : ] ) <EOL> excludes = set ( sys . builtin_module_names ) <EOL> for root in roots : <EOL> for dirpath , dirnames , filenames in os . walk ( root ) : <EOL> if '<STR_LIT:test>' in dirnames : <EOL> dirnames . remove ( '<STR_LIT:test>' ) <EOL> if dirpath == root : <EOL> path = '<STR_LIT>' <EOL> if '<STR_LIT>' in dirnames : <EOL> dirnames . remove ( '<STR_LIT>' ) <EOL> else : <EOL> path = dirpath [ len ( root ) + <NUM_LIT:1> : ] <EOL> if path == obj_dir : <EOL> path = '<STR_LIT>' <EOL> for name in filenames : <EOL> if fnmatch . fnmatch ( name , '<STR_LIT>' ) or fnmatch . fnmatch ( name , obj_pattern ) : <EOL> name = os . path . join ( path , name [ : name . rfind ( '<STR_LIT:.>' ) ] ) <EOL> name = name . replace ( os . sep , '<STR_LIT:.>' ) <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : name . rfind ( '<STR_LIT:.>' ) ] <EOL> excludes . add ( name ) <EOL> return list ( excludes ) <EOL> def _process_egg ( path , distributions , prefixes , logger ) : <EOL> """<STR_LIT>""" <EOL> logger . log ( LOG_DEBUG2 , "<STR_LIT>" , path ) <EOL> dist = pkg_resources . Distribution . from_filename ( path ) <EOL> distributions . add ( dist ) <EOL> prefixes . add ( path ) <EOL> for req in dist . requires ( ) : <EOL> logger . log ( LOG_DEBUG2 , "<STR_LIT>" , req ) <EOL> dep = pkg_resources . get_distribution ( req ) <EOL> distributions . add ( dep ) <EOL> loc = dep . location <EOL> if loc . endswith ( '<STR_LIT>' ) and loc not in prefixes : <EOL> prefixes . add ( loc ) <EOL> def _process_found_modules ( py_dir , finder_info , modules , distributions , <EOL> prefixes , local_modules , orphans , not_found , logger ) : <EOL> """<STR_LIT>""" <EOL> working_set = pkg_resources . WorkingSet ( ) <EOL> py_version = '<STR_LIT>' % sys . version [ : <NUM_LIT:3> ] <EOL> for name , path in sorted ( finder_info , key = lambda item : item [ <NUM_LIT:0> ] ) : <EOL> if name in modules : <EOL> continue <EOL> if name != '<STR_LIT:__main__>' : <EOL> modules . add ( name ) <EOL> if not path : <EOL> continue <EOL> dirpath = os . path . realpath ( os . path . dirname ( path ) ) <EOL> if dirpath . startswith ( py_dir ) : <EOL> local_modules . add ( path ) <EOL> continue <EOL> if name not in sys . modules : <EOL> continue <EOL> found = False <EOL> for prefix in prefixes : <EOL> if dirpath . startswith ( prefix ) : <EOL> found = True <EOL> break <EOL> if found : <EOL> continue <EOL> for dist in working_set : <EOL> loc = dist . location <EOL> if loc . endswith ( '<STR_LIT>' ) or loc . endswith ( '<STR_LIT>' ) or loc . endswith ( py_version ) : <EOL> loc = os . path . join ( loc , dist . project_name ) <EOL> if dirpath . startswith ( loc ) : <EOL> distributions . add ( dist ) <EOL> if loc . endswith ( '<STR_LIT>' ) : <EOL> prefixes . add ( loc ) <EOL> break <EOL> else : <EOL> if dirpath not in not_found : <EOL> if not dirpath . endswith ( '<STR_LIT>' ) and not dirpath . endswith ( '<STR_LIT>' ) : <EOL> not_found . add ( dirpath ) <EOL> path = dirpath <EOL> if ( name , path ) not in orphans : <EOL> logger . warning ( '<STR_LIT>' , name ) <EOL> orphans . add ( ( name , path ) ) <EOL> def _write_state_file ( dst_dir , root , name , logger , observer ) : <EOL> """<STR_LIT>""" <EOL> state_name = name + '<STR_LIT>' <EOL> state_path = os . path . join ( dst_dir , state_name ) <EOL> try : <EOL> save ( root , state_path , SAVE_CPICKLE , - <NUM_LIT:1> , logger ) <EOL> except Exception as exc : <EOL> msg = "<STR_LIT>" % ( state_path , exc ) <EOL> observer . exception ( msg ) <EOL> raise type ( exc ) ( msg ) <EOL> return ( state_name , state_path ) <EOL> def _write_loader_script ( path , state_name , package , top ) : <EOL> """<STR_LIT>""" <EOL> if state_name . startswith ( package ) : <EOL> pkg_arg = '<STR_LIT>' <EOL> else : <EOL> pkg_arg = "<STR_LIT>" % package <EOL> if top : <EOL> top_arg = '<STR_LIT>' <EOL> else : <EOL> top_arg = '<STR_LIT>' <EOL> out = open ( path , '<STR_LIT:w>' ) <EOL> out . write ( """<STR_LIT>""" % { '<STR_LIT:name>' : state_name , '<STR_LIT>' : pkg_arg , '<STR_LIT>' : top_arg } ) <EOL> out . close ( ) <EOL> def _create_entry_map ( entry_pts ) : <EOL> """<STR_LIT>""" <EOL> pkg_name = entry_pts [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> pkg_loader = entry_pts [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> ldattr = [ '<STR_LIT>' ] <EOL> entry_map = { } <EOL> entry_group = { } <EOL> entry_group [ '<STR_LIT>' ] = pkg_resources . EntryPoint ( '<STR_LIT>' , pkg_loader , ldattr ) <EOL> entry_map [ '<STR_LIT>' ] = entry_group <EOL> groups = set ( ) <EOL> for group , name , loader in entry_pts : <EOL> groups . add ( group ) <EOL> for grp in sorted ( groups ) : <EOL> entry_group = { } <EOL> for group , name , loader in entry_pts : <EOL> if group == grp : <EOL> entry_group [ name ] = pkg_resources . EntryPoint ( name , pkg_name + '<STR_LIT:.>' + loader , ldattr ) <EOL> if entry_group : <EOL> entry_map [ grp ] = entry_group <EOL> return entry_map <EOL> def save ( root , outstream , fmt = SAVE_CPICKLE , proto = - <NUM_LIT:1> , logger = None ) : <EOL> """<STR_LIT>""" <EOL> logger = logger or NullLogger ( ) <EOL> if isinstance ( outstream , basestring ) : <EOL> if ( fmt is SAVE_CPICKLE or fmt is SAVE_PICKLE ) and proto != <NUM_LIT:0> : <EOL> mode = '<STR_LIT:wb>' <EOL> else : <EOL> mode = '<STR_LIT:w>' <EOL> try : <EOL> outstream = open ( outstream , mode ) <EOL> except IOError as exc : <EOL> raise type ( exc ) ( "<STR_LIT>" % <EOL> ( outstream , exc . strerror ) ) <EOL> if fmt is SAVE_CPICKLE : <EOL> cPickle . dump ( root , outstream , proto ) <EOL> elif fmt is SAVE_PICKLE : <EOL> pickle . dump ( root , outstream , proto ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % fmt ) <EOL> class _DistCache ( object ) : <EOL> """<STR_LIT>""" <EOL> _cache = None <EOL> _dirty = False <EOL> @ staticmethod <EOL> def lookup ( path ) : <EOL> """<STR_LIT>""" <EOL> if _DistCache . _cache is None : <EOL> _DistCache . _load ( ) <EOL> return _DistCache . _cache [ _DistCache . _key ( path ) ] <EOL> @ staticmethod <EOL> def record ( path , dists ) : <EOL> """<STR_LIT>""" <EOL> _DistCache . _cache [ _DistCache . _key ( path ) ] = dists <EOL> _DistCache . _dirty = True <EOL> @ staticmethod <EOL> def save ( ) : <EOL> """<STR_LIT>""" <EOL> if _DistCache . _dirty : <EOL> out = _DistCache . _open ( '<STR_LIT:wb>' ) <EOL> cPickle . dump ( _DistCache . _cache , out , cPickle . HIGHEST_PROTOCOL ) <EOL> out . close ( ) <EOL> _DistCache . _dirty = False <EOL> @ staticmethod <EOL> def _key ( path ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . realpath ( path ) <EOL> info = os . stat ( path ) <EOL> return ( path , info . st_mtime ) <EOL> @ staticmethod <EOL> def _load ( ) : <EOL> """<STR_LIT>""" <EOL> _DistCache . _cache = { } <EOL> try : <EOL> inp = _DistCache . _open ( '<STR_LIT:rb>' ) <EOL> except Exception : <EOL> return <EOL> try : <EOL> _DistCache . _cache = cPickle . load ( inp ) <EOL> except Exception : <EOL> return <EOL> finally : <EOL> inp . close ( ) <EOL> @ staticmethod <EOL> def _open ( mode ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . expanduser ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> dirname = os . path . dirname ( filename ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . mkdir ( dirname ) <EOL> return open ( filename , mode ) </s>
