"""Package resource API--------------------A resource is a logical file contained within a package, or a logicalsubdirectory thereof. The package resource API expects resource namesto have their path parts separated with ``/``, *not* whatever the localpath separator is. Do not use os.path operations to manipulate resourcenames being passed into the API.The package resource API is designed to work with normal filesystem packages,.egg files, and unpacked .egg files. It can also work in a limited way with.zip files and with custom PEP 302 loaders that support the ``get_data()``method."""importsys,os,zipimport,time,re,imp,typesfromurlparseimporturlparse,urlunparsetry:frozensetexceptNameError:fromsetsimportImmutableSetasfrozenset# capture these to bypass sandboxingfromosimportutimetry:fromosimportmkdir,rename,unlinkWRITE_SUPPORT=TrueexceptImportError:# no write support, probably under GAEWRITE_SUPPORT=Falsefromosimportopenasos_openfromos.pathimportisdir,split# This marker is used to simplify the process that checks is the# setuptools package was installed by the Setuptools project# or by the Distribute project, in case Setuptools creates# a distribution with the same version.## The bootstrapping script for instance, will check if this# attribute is present to decide wether to reinstall the package_distribute=Truedef_bypass_ensure_directory(name,mode=0777):# Sandbox-bypassing version of ensure_directory()ifnotWRITE_SUPPORT:raiseIOError('"os.mkdir" not supported on this platform.')dirname,filename=split(name)ifdirnameandfilenameandnotisdir(dirname):_bypass_ensure_directory(dirname)mkdir(dirname,mode)defget_supported_platform():"""Return this platform's maximum compatible version. distutils.util.get_platform() normally reports the minimum version of Mac OS X that would be required to *use* extensions produced by distutils. But what we want when checking compatibility is to know the version of Mac OS X that we are *running*. To allow usage of packages that explicitly require a newer version of Mac OS X, we must also know the current version of the OS. If this condition occurs for any other platform with a version in its platform strings, this function should be extended accordingly. """plat=get_build_platform();m=macosVersionString.match(plat)ifmisnotNoneandsys.platform=="darwin":try:plat='macosx-%s-%s'%('.'.join(_macosx_vers()[:2]),m.group(3))exceptValueError:pass# not Mac OS Xreturnplat__all__=[# Basic resource access and distribution/entry point discovery'require','run_script','get_provider','get_distribution','load_entry_point','get_entry_map','get_entry_info','iter_entry_points','resource_string','resource_stream','resource_filename','resource_listdir','resource_exists','resource_isdir',# Environmental control'declare_namespace','working_set','add_activation_listener','find_distributions','set_extraction_path','cleanup_resources','get_default_cache',# Primary implementation classes'Environment','WorkingSet','ResourceManager','Distribution','Requirement','EntryPoint',# Exceptions'ResolutionError','VersionConflict','DistributionNotFound','UnknownExtra','ExtractionError',# Parsing functions and string utilities'parse_requirements','parse_version','safe_name','safe_version','get_platform','compatible_platforms','yield_lines','split_sections','safe_extra','to_filename',# filesystem utilities'ensure_directory','normalize_path',# Distribution "precedence" constants'EGG_DIST','BINARY_DIST','SOURCE_DIST','CHECKOUT_DIST','DEVELOP_DIST',# "Provider" interfaces, implementations, and registration/lookup APIs'IMetadataProvider','IResourceProvider','FileMetadata','PathMetadata','EggMetadata','EmptyProvider','empty_provider','NullProvider','EggProvider','DefaultProvider','ZipProvider','register_finder','register_namespace_handler','register_loader_type','fixup_namespace_packages','get_importer',# Deprecated/backward compatibility only'run_main','AvailableDistributions',]classResolutionError(Exception):"""Abstract base for dependency resolution errors"""def__repr__(self):returnself.__class__.__name__+repr(self.args)classVersionConflict(ResolutionError):"""An already-installed version conflicts with the requested version"""classDistributionNotFound(ResolutionError):"""A requested distribution was not found"""classUnknownExtra(ResolutionError):"""Distribution doesn't have an "extra feature" of the given name"""_provider_factories={}PY_MAJOR=sys.version[:3]EGG_DIST=3BINARY_DIST=2SOURCE_DIST=1CHECKOUT_DIST=0DEVELOP_DIST=-1defregister_loader_type(loader_type,provider_factory):"""Register `provider_factory` to make providers for `loader_type` `loader_type` is the type or class of a PEP 302 ``module.__loader__``, and `provider_factory` is a function that, passed a *module* object, returns an ``IResourceProvider`` for that module. """_provider_factories[loader_type]=provider_factorydefget_provider(moduleOrReq):"""Return an IResourceProvider for the named module or requirement"""ifisinstance(moduleOrReq,Requirement):returnworking_set.find(moduleOrReq)orrequire(str(moduleOrReq))[0]try:module=sys.modules[moduleOrReq]exceptKeyError:__import__(moduleOrReq)module=sys.modules[moduleOrReq]loader=getattr(module,'__loader__',None)return_find_adapter(_provider_factories,loader)(module)def_macosx_vers(_cache=[]):ifnot_cache:importplatformversion=platform.mac_ver()[0]# fallback for MacPortsifversion=='':importplistlibplist='/System/Library/CoreServices/SystemVersion.plist'ifos.path.exists(plist):ifhasattr(plistlib,'readPlist'):plist_content=plistlib.readPlist(plist)if'ProductVersion'inplist_content:version=plist_content['ProductVersion']_cache.append(version.split('.'))return_cache[0]def_macosx_arch(machine):return{'PowerPC':'ppc','Power_Macintosh':'ppc'}.get(machine,machine)defget_build_platform():"""Return this platform's string for platform-specific distributions XXX Currently this is the same as ``distutils.util.get_platform()``, but it needs some hacks for Linux and Mac OS X. """try:fromdistutils.utilimportget_platformexceptImportError:fromsysconfigimportget_platformplat=get_platform()ifsys.platform=="darwin"andnotplat.startswith('macosx-'):try:version=_macosx_vers()machine=os.uname()[4].replace(" ","_")return"macosx-%d.%d-%s"%(int(version[0]),int(version[1]),_macosx_arch(machine))exceptValueError:# if someone is running a non-Mac darwin system, this will fall# through to the default implementationpassreturnplatmacosVersionString=re.compile(r"macosx-(\d+)\.(\d+)-(.*)")darwinVersionString=re.compile(r"darwin-(\d+)\.(\d+)\.(\d+)-(.*)")get_platform=get_build_platform# XXX backward compatdefcompatible_platforms(provided,required):"""Can code for the `provided` platform run on the `required` platform? Returns true if either platform is ``None``, or the platforms are equal. XXX Needs compatibility checks for Linux and other unixy OSes. """ifprovidedisNoneorrequiredisNoneorprovided==required:returnTrue# easy case# Mac OS X special casesreqMac=macosVersionString.match(required)ifreqMac:provMac=macosVersionString.match(provided)# is this a Mac package?ifnotprovMac:# this is backwards compatibility for packages built before# setuptools 0.6. All packages built after this point will# use the new macosx designation.provDarwin=darwinVersionString.match(provided)ifprovDarwin:dversion=int(provDarwin.group(1))macosversion="%s.%s"%(reqMac.group(1),reqMac.group(2))ifdversion==7andmacosversion>="10.3"or \
dversion==8andmacosversion>="10.4":#import warnings#warnings.warn("Mac eggs should be rebuilt to "# "use the macosx designation instead of darwin.",# category=DeprecationWarning)returnTruereturnFalse# egg isn't macosx or legacy darwin# are they the same major version and machine type?ifprovMac.group(1)!=reqMac.group(1)or \
provMac.group(3)!=reqMac.group(3):returnFalse# is the required OS major update >= the provided one?ifint(provMac.group(2))>int(reqMac.group(2)):returnFalsereturnTrue# XXX Linux and other platforms' special cases should go herereturnFalsedefrun_script(dist_spec,script_name):"""Locate distribution `dist_spec` and run its `script_name` script"""ns=sys._getframe(1).f_globalsname=ns['__name__']ns.clear()ns['__name__']=namerequire(dist_spec)[0].run_script(script_name,ns)run_main=run_script# backward compatibilitydefget_distribution(dist):"""Return a current distribution object for a Requirement or string"""ifisinstance(dist,basestring):dist=Requirement.parse(dist)ifisinstance(dist,Requirement):dist=get_provider(dist)ifnotisinstance(dist,Distribution):raiseTypeError("Expected string, Requirement, or Distribution",dist)returndistdefload_entry_point(dist,group,name):"""Return `name` entry point of `group` for `dist` or raise ImportError"""returnget_distribution(dist).load_entry_point(group,name)defget_entry_map(dist,group=None):"""Return the entry point map for `group`, or the full entry map"""returnget_distribution(dist).get_entry_map(group)defget_entry_info(dist,group,name):"""Return the EntryPoint object for `group`+`name`, or ``None``"""returnget_distribution(dist).get_entry_info(group,name)classIMetadataProvider:defhas_metadata(name):"""Does the package's distribution contain the named metadata?"""defget_metadata(name):"""The named metadata resource as a string"""defget_metadata_lines(name):"""Yield named metadata resource as list of non-blank non-comment lines Leading and trailing whitespace is stripped from each line, and lines with ``#`` as the first non-blank character are omitted."""defmetadata_isdir(name):"""Is the named metadata a directory? (like ``os.path.isdir()``)"""defmetadata_listdir(name):"""List of metadata names in the directory (like ``os.listdir()``)"""defrun_script(script_name,namespace):"""Execute the named script in the supplied namespace dictionary"""classIResourceProvider(IMetadataProvider):"""An object that provides access to package resources"""defget_resource_filename(manager,resource_name):"""Return a true filesystem path for `resource_name` `manager` must be an ``IResourceManager``"""defget_resource_stream(manager,resource_name):"""Return a readable file-like object for `resource_name` `manager` must be an ``IResourceManager``"""defget_resource_string(manager,resource_name):"""Return a string containing the contents of `resource_name` `manager` must be an ``IResourceManager``"""defhas_resource(resource_name):"""Does the package contain the named resource?"""defresource_isdir(resource_name):"""Is the named resource a directory? (like ``os.path.isdir()``)"""defresource_listdir(resource_name):"""List of resource names in the directory (like ``os.listdir()``)"""classWorkingSet(object):"""A collection of active distributions on sys.path (or a similar list)"""def__init__(self,entries=None):"""Create working set from list of path entries (default=sys.path)"""self.entries=[]self.entry_keys={}self.by_key={}self.callbacks=[]ifentriesisNone:entries=sys.pathforentryinentries:self.add_entry(entry)defadd_entry(self,entry):"""Add a path item to ``.entries``, finding any distributions on it ``find_distributions(entry,True)`` is used to find distributions corresponding to the path entry, and they are added. `entry` is always appended to ``.entries``, even if it is already present. (This is because ``sys.path`` can contain the same value more than once, and the ``.entries`` of the ``sys.path`` WorkingSet should always equal ``sys.path``.) """self.entry_keys.setdefault(entry,[])self.entries.append(entry)fordistinfind_distributions(entry,True):self.add(dist,entry,False)def__contains__(self,dist):"""True if `dist` is the active distribution for its project"""returnself.by_key.get(dist.key)==distdeffind(self,req):"""Find a distribution matching requirement `req` If there is an active distribution for the requested project, this returns it as long as it meets the version requirement specified by `req`. But, if there is an active distribution for the project and it does *not* meet the `req` requirement, ``VersionConflict`` is raised. If there is no active distribution for the requested project, ``None`` is returned. """dist=self.by_key.get(req.key)ifdistisnotNoneanddistnotinreq:raiseVersionConflict(dist,req)# XXX add more infoelse:returndistdefiter_entry_points(self,group,name=None):"""Yield entry point objects from `group` matching `name` If `name` is None, yields all entry points in `group` from all distributions in the working set, otherwise only ones matching both `group` and `name` are yielded (in distribution order). """fordistinself:entries=dist.get_entry_map(group)ifnameisNone:forepinentries.values():yieldepelifnameinentries:yieldentries[name]defrun_script(self,requires,script_name):"""Locate distribution for `requires` and run `script_name` script"""ns=sys._getframe(1).f_globalsname=ns['__name__']ns.clear()ns['__name__']=nameself.require(requires)[0].run_script(script_name,ns)def__iter__(self):"""Yield distributions for non-duplicate projects in the working set The yield order is the order in which the items' path entries were added to the working set. """seen={}foriteminself.entries:forkeyinself.entry_keys[item]:ifkeynotinseen:seen[key]=1yieldself.by_key[key]defadd(self,dist,entry=None,insert=True):"""Add `dist` to working set, associated with `entry` If `entry` is unspecified, it defaults to the ``.location`` of `dist`. On exit from this routine, `entry` is added to the end of the working set's ``.entries`` (if it wasn't already present). `dist` is only added to the working set if it's for a project that doesn't already have a distribution in the set. If it's added, any callbacks registered with the ``subscribe()`` method will be called. """ifinsert:dist.insert_on(self.entries,entry)ifentryisNone:entry=dist.locationkeys=self.entry_keys.setdefault(entry,[])keys2=self.entry_keys.setdefault(dist.location,[])ifdist.keyinself.by_key:return# ignore hidden distrosself.by_key[dist.key]=distifdist.keynotinkeys:keys.append(dist.key)ifdist.keynotinkeys2:keys2.append(dist.key)self._added_new(dist)defresolve(self,requirements,env=None,installer=None,replacement=True):"""List all distributions needed to (recursively) meet `requirements` `requirements` must be a sequence of ``Requirement`` objects. `env`, if supplied, should be an ``Environment`` instance. If not supplied, it defaults to all distributions available within any entry or distribution in the working set. `installer`, if supplied, will be invoked with each requirement that cannot be met by an already-installed distribution; it should return a ``Distribution`` or ``None``. """requirements=list(requirements)[::-1]# set up the stackprocessed={}# set of processed requirementsbest={}# key -> distto_activate=[]whilerequirements:req=requirements.pop(0)# process dependencies breadth-firstif_override_setuptools(req)andreplacement:req=Requirement.parse('distribute')ifreqinprocessed:# Ignore cyclic or redundant dependenciescontinuedist=best.get(req.key)ifdistisNone:# Find the best distribution and add it to the mapdist=self.by_key.get(req.key)ifdistisNone:ifenvisNone:env=Environment(self.entries)dist=best[req.key]=env.best_match(req,self,installer)ifdistisNone:#msg = ("The '%s' distribution was not found on this "# "system, and is required by this application.")#raise DistributionNotFound(msg % req)# unfortunately, zc.buildout uses a str(err)# to get the name of the distribution here..raiseDistributionNotFound(req)to_activate.append(dist)ifdistnotinreq:# Oops, the "best" so far conflicts with a dependencyraiseVersionConflict(dist,req)# XXX put more info hererequirements.extend(dist.requires(req.extras)[::-1])processed[req]=Truereturnto_activate# return list of distros to activatedeffind_plugins(self,plugin_env,full_env=None,installer=None,fallback=True):"""Find all activatable distributions in `plugin_env` Example usage:: distributions, errors = working_set.find_plugins( Environment(plugin_dirlist) ) map(working_set.add, distributions) # add plugins+libs to sys.path print 'Could not load', errors # display errors The `plugin_env` should be an ``Environment`` instance that contains only distributions that are in the project's "plugin directory" or directories. The `full_env`, if supplied, should be an ``Environment`` contains all currently-available distributions. If `full_env` is not supplied, one is created automatically from the ``WorkingSet`` this method is called on, which will typically mean that every directory on ``sys.path`` will be scanned for distributions. `installer` is a standard installer callback as used by the ``resolve()`` method. The `fallback` flag indicates whether we should attempt to resolve older versions of a plugin if the newest version cannot be resolved. This method returns a 2-tuple: (`distributions`, `error_info`), where `distributions` is a list of the distributions found in `plugin_env` that were loadable, along with any other distributions that are needed to resolve their dependencies. `error_info` is a dictionary mapping unloadable plugin distributions to an exception instance describing the error that occurred. Usually this will be a ``DistributionNotFound`` or ``VersionConflict`` instance. """plugin_projects=list(plugin_env)plugin_projects.sort()# scan project names in alphabetic ordererror_info={}distributions={}iffull_envisNone:env=Environment(self.entries)env+=plugin_envelse:env=full_env+plugin_envshadow_set=self.__class__([])map(shadow_set.add,self)# put all our entries in shadow_setforproject_nameinplugin_projects:fordistinplugin_env[project_name]:req=[dist.as_requirement()]try:resolvees=shadow_set.resolve(req,env,installer)exceptResolutionError,v:error_info[dist]=v# save error infoiffallback:continue# try the next older version of projectelse:break# give up on this project, keep goingelse:map(shadow_set.add,resolvees)distributions.update(dict.fromkeys(resolvees))# success, no need to try any more versions of this projectbreakdistributions=list(distributions)distributions.sort()returndistributions,error_infodefrequire(self,*requirements):"""Ensure that distributions matching `requirements` are activated `requirements` must be a string or a (possibly-nested) sequence thereof, specifying the distributions and versions required. The return value is a sequence of the distributions that needed to be activated to fulfill the requirements; all relevant distributions are included, even if they were already activated in this working set. """needed=self.resolve(parse_requirements(requirements))fordistinneeded:self.add(dist)returnneededdefsubscribe(self,callback):"""Invoke `callback` for all distributions (including existing ones)"""ifcallbackinself.callbacks:returnself.callbacks.append(callback)fordistinself:callback(dist)def_added_new(self,dist):forcallbackinself.callbacks:callback(dist)classEnvironment(object):"""Searchable snapshot of distributions on a search path"""def__init__(self,search_path=None,platform=get_supported_platform(),python=PY_MAJOR):"""Snapshot distributions available on a search path Any distributions found on `search_path` are added to the environment. `search_path` should be a sequence of ``sys.path`` items. If not supplied, ``sys.path`` is used. `platform` is an optional string specifying the name of the platform that platform-specific distributions must be compatible with. If unspecified, it defaults to the current platform. `python` is an optional string naming the desired version of Python (e.g. ``'2.4'``); it defaults to the current version. You may explicitly set `platform` (and/or `python`) to ``None`` if you wish to map *all* distributions, not just those compatible with the running platform or Python version. """self._distmap={}self._cache={}self.platform=platformself.python=pythonself.scan(search_path)defcan_add(self,dist):"""Is distribution `dist` acceptable for this environment? The distribution must match the platform and python version requirements specified when this environment was created, or False is returned. """return(self.pythonisNoneordist.py_versionisNoneordist.py_version==self.python) \
andcompatible_platforms(dist.platform,self.platform)defremove(self,dist):"""Remove `dist` from the environment"""self._distmap[dist.key].remove(dist)defscan(self,search_path=None):"""Scan `search_path` for distributions usable in this environment Any distributions found are added to the environment. `search_path` should be a sequence of ``sys.path`` items. If not supplied, ``sys.path`` is used. Only distributions conforming to the platform/python version defined at initialization are added. """ifsearch_pathisNone:search_path=sys.pathforiteminsearch_path:fordistinfind_distributions(item):self.add(dist)def__getitem__(self,project_name):"""Return a newest-to-oldest list of distributions for `project_name` """try:returnself._cache[project_name]exceptKeyError:project_name=project_name.lower()ifproject_namenotinself._distmap:return[]ifproject_namenotinself._cache:dists=self._cache[project_name]=self._distmap[project_name]_sort_dists(dists)returnself._cache[project_name]defadd(self,dist):"""Add `dist` if we ``can_add()`` it and it isn't already added"""ifself.can_add(dist)anddist.has_version():dists=self._distmap.setdefault(dist.key,[])ifdistnotindists:dists.append(dist)ifdist.keyinself._cache:_sort_dists(self._cache[dist.key])defbest_match(self,req,working_set,installer=None):"""Find distribution best matching `req` and usable on `working_set` This calls the ``find(req)`` method of the `working_set` to see if a suitable distribution is already active. (This may raise ``VersionConflict`` if an unsuitable version of the project is already active in the specified `working_set`.) If a suitable distribution isn't active, this method returns the newest distribution in the environment that meets the ``Requirement`` in `req`. If no suitable distribution is found, and `installer` is supplied, then the result of calling the environment's ``obtain(req, installer)`` method will be returned. """dist=working_set.find(req)ifdistisnotNone:returndistfordistinself[req.key]:ifdistinreq:returndistreturnself.obtain(req,installer)# try and download/installdefobtain(self,requirement,installer=None):"""Obtain a distribution matching `requirement` (e.g. via download) Obtain a distro that matches requirement (e.g. via download). In the base ``Environment`` class, this routine just returns ``installer(requirement)``, unless `installer` is None, in which case None is returned instead. This method is a hook that allows subclasses to attempt other ways of obtaining a distribution before falling back to the `installer` argument."""ifinstallerisnotNone:returninstaller(requirement)def__iter__(self):"""Yield the unique project names of the available distributions"""forkeyinself._distmap.keys():ifself[key]:yieldkeydef__iadd__(self,other):"""In-place addition of a distribution or environment"""ifisinstance(other,Distribution):self.add(other)elifisinstance(other,Environment):forprojectinother:fordistinother[project]:self.add(dist)else:raiseTypeError("Can't add %r to environment"%(other,))returnselfdef__add__(self,other):"""Add an environment or distribution to an environment"""new=self.__class__([],platform=None,python=None)forenvinself,other:new+=envreturnnewAvailableDistributions=Environment# XXX backward compatibilityclassExtractionError(RuntimeError):"""An error occurred extracting a resource The following attributes are available from instances of this exception: manager The resource manager that raised this exception cache_path The base directory for resource extraction original_error The exception instance that caused extraction to fail """classResourceManager:"""Manage resource extraction and packages"""extraction_path=Nonedef__init__(self):self.cached_files={}defresource_exists(self,package_or_requirement,resource_name):"""Does the named resource exist?"""returnget_provider(package_or_requirement).has_resource(resource_name)defresource_isdir(self,package_or_requirement,resource_name):"""Is the named resource an existing directory?"""returnget_provider(package_or_requirement).resource_isdir(resource_name)defresource_filename(self,package_or_requirement,resource_name):"""Return a true filesystem path for specified resource"""returnget_provider(package_or_requirement).get_resource_filename(self,resource_name)defresource_stream(self,package_or_requirement,resource_name):"""Return a readable file-like object for specified resource"""returnget_provider(package_or_requirement).get_resource_stream(self,resource_name)defresource_string(self,package_or_requirement,resource_name):"""Return specified resource as a string"""returnget_provider(package_or_requirement).get_resource_string(self,resource_name)defresource_listdir(self,package_or_requirement,resource_name):"""List the contents of the named resource directory"""returnget_provider(package_or_requirement).resource_listdir(resource_name)defextraction_error(self):"""Give an error message for problems extracting file(s)"""old_exc=sys.exc_info()[1]cache_path=self.extraction_pathorget_default_cache()err=ExtractionError("""Can't extract file(s) to egg cacheThe following error occurred while trying to extract file(s) to the Python eggcache:%sThe Python egg cache directory is currently set to:%sPerhaps your account does not have write access to this directory? You canchange the cache directory by setting the PYTHON_EGG_CACHE environmentvariable to point to an accessible directory."""%(old_exc,cache_path))err.manager=selferr.cache_path=cache_patherr.original_error=old_excraiseerrdefget_cache_path(self,archive_name,names=()):"""Return absolute location in cache for `archive_name` and `names` The parent directory of the resulting path will be created if it does not already exist. `archive_name` should be the base filename of the enclosing egg (which may not be the name of the enclosing zipfile!), including its ".egg" extension. `names`, if provided, should be a sequence of path name parts "under" the egg's extraction location. This method should only be called by resource providers that need to obtain an extraction location, and only for names they intend to extract, as it tracks the generated names for possible cleanup later. """extract_path=self.extraction_pathorget_default_cache()target_path=os.path.join(extract_path,archive_name+'-tmp',*names)try:_bypass_ensure_directory(target_path)except:self.extraction_error()self.cached_files[target_path]=1returntarget_pathdefpostprocess(self,tempname,filename):"""Perform any platform-specific postprocessing of `tempname` This is where Mac header rewrites should be done; other platforms don't have anything special they should do. Resource providers should call this method ONLY after successfully extracting a compressed resource. They must NOT call it on resources that are already in the filesystem. `tempname` is the current (temporary) name of the file, and `filename` is the name it will be renamed to by the caller after this routine returns. """ifos.name=='posix':# Make the resource executablemode=((os.stat(tempname).st_mode)|0555)&07777os.chmod(tempname,mode)defset_extraction_path(self,path):"""Set the base path where resources will be extracted to, if needed. If you do not call this routine before any extractions take place, the path defaults to the return value of ``get_default_cache()``. (Which is based on the ``PYTHON_EGG_CACHE`` environment variable, with various platform-specific fallbacks. See that routine's documentation for more details.) Resources are extracted to subdirectories of this path based upon information given by the ``IResourceProvider``. You may set this to a temporary directory, but then you must call ``cleanup_resources()`` to delete the extracted files when done. There is no guarantee that ``cleanup_resources()`` will be able to remove all extracted files. (Note: you may not change the extraction path for a given resource manager once resources have been extracted, unless you first call ``cleanup_resources()``.) """ifself.cached_files:raiseValueError("Can't change extraction path, files already extracted")self.extraction_path=pathdefcleanup_resources(self,force=False):""" Delete all extracted resource files and directories, returning a list of the file and directory names that could not be successfully removed. This function does not have any concurrency protection, so it should generally only be called when the extraction path is a temporary directory exclusive to a single process. This method is not automatically called; you must call it explicitly or register it as an ``atexit`` function if you wish to ensure cleanup of a temporary directory used for extractions. """# XXXdefget_default_cache():"""Determine the default cache location This returns the ``PYTHON_EGG_CACHE`` environment variable, if set. Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of the "Application Data" directory. On all other systems, it's "~/.python-eggs". """try:returnos.environ['PYTHON_EGG_CACHE']exceptKeyError:passifos.name!='nt':returnos.path.expanduser('~/.python-eggs')app_data='Application Data'# XXX this may be locale-specific!app_homes=[(('APPDATA',),None),# best option, should be locale-safe(('USERPROFILE',),app_data),(('HOMEDRIVE','HOMEPATH'),app_data),(('HOMEPATH',),app_data),(('HOME',),None),(('WINDIR',),app_data),# 95/98/ME]forkeys,subdirinapp_homes:dirname=''forkeyinkeys:ifkeyinos.environ:dirname=os.path.join(dirname,os.environ[key])else:breakelse:ifsubdir:dirname=os.path.join(dirname,subdir)returnos.path.join(dirname,'Python-Eggs')else:raiseRuntimeError("Please set the PYTHON_EGG_CACHE enviroment variable")defsafe_name(name):"""Convert an arbitrary string to a standard distribution name Any runs of non-alphanumeric/. characters are replaced with a single '-'. """returnre.sub('[^A-Za-z0-9.]+','-',name)defsafe_version(version):"""Convert an arbitrary string to a standard version string Spaces become dots, and all other non-alphanumeric characters become dashes, with runs of multiple dashes condensed to a single dash. """version=version.replace(' ','.')returnre.sub('[^A-Za-z0-9.]+','-',version)defsafe_extra(extra):"""Convert an arbitrary string to a standard 'extra' name Any runs of non-alphanumeric characters are replaced with a single '_', and the result is always lowercased. """returnre.sub('[^A-Za-z0-9.]+','_',extra).lower()defto_filename(name):"""Convert a project or version name to its filename-escaped form Any '-' characters are currently replaced with '_'. """returnname.replace('-','_')classNullProvider:"""Try to implement resources and metadata for arbitrary PEP 302 loaders"""egg_name=Noneegg_info=Noneloader=Nonedef__init__(self,module):self.loader=getattr(module,'__loader__',None)self.module_path=os.path.dirname(getattr(module,'__file__',''))defget_resource_filename(self,manager,resource_name):returnself._fn(self.module_path,resource_name)defget_resource_stream(self,manager,resource_name):returnStringIO(self.get_resource_string(manager,resource_name))defget_resource_string(self,manager,resource_name):returnself._get(self._fn(self.module_path,resource_name))defhas_resource(self,resource_name):returnself._has(self._fn(self.module_path,resource_name))defhas_metadata(self,name):returnself.egg_infoandself._has(self._fn(self.egg_info,name))ifsys.version_info<=(3,):defget_metadata(self,name):ifnotself.egg_info:return""returnself._get(self._fn(self.egg_info,name))else:defget_metadata(self,name):ifnotself.egg_info:return""returnself._get(self._fn(self.egg_info,name)).decode("utf-8")defget_metadata_lines(self,name):returnyield_lines(self.get_metadata(name))defresource_isdir(self,resource_name):returnself._isdir(self._fn(self.module_path,resource_name))defmetadata_isdir(self,name):returnself.egg_infoandself._isdir(self._fn(self.egg_info,name))defresource_listdir(self,resource_name):returnself._listdir(self._fn(self.module_path,resource_name))defmetadata_listdir(self,name):ifself.egg_info:returnself._listdir(self._fn(self.egg_info,name))return[]defrun_script(self,script_name,namespace):script='scripts/'+script_nameifnotself.has_metadata(script):raiseResolutionError("No script named %r"%script_name)script_text=self.get_metadata(script).replace('\r\n','\n')script_text=script_text.replace('\r','\n')script_filename=self._fn(self.egg_info,script)namespace['__file__']=script_filenameifos.path.exists(script_filename):execfile(script_filename,namespace,namespace)else:fromlinecacheimportcachecache[script_filename]=(len(script_text),0,script_text.split('\n'),script_filename)script_code=compile(script_text,script_filename,'exec')execscript_codeinnamespace,namespacedef_has(self,path):raiseNotImplementedError("Can't perform this operation for unregistered loader type")def_isdir(self,path):raiseNotImplementedError("Can't perform this operation for unregistered loader type")def_listdir(self,path):raiseNotImplementedError("Can't perform this operation for unregistered loader type")def_fn(self,base,resource_name):ifresource_name:returnos.path.join(base,*resource_name.split('/'))returnbasedef_get(self,path):ifhasattr(self.loader,'get_data'):returnself.loader.get_data(path)raiseNotImplementedError("Can't perform this operation for loaders without 'get_data()'")register_loader_type(object,NullProvider)classEggProvider(NullProvider):"""Provider based on a virtual filesystem"""def__init__(self,module):NullProvider.__init__(self,module)self._setup_prefix()def_setup_prefix(self):# we assume here that our metadata may be nested inside a "basket"# of multiple eggs; that's why we use module_path instead of .archivepath=self.module_pathold=Nonewhilepath!=old:ifpath.lower().endswith('.egg'):self.egg_name=os.path.basename(path)self.egg_info=os.path.join(path,'EGG-INFO')self.egg_root=pathbreakold=pathpath,base=os.path.split(path)classDefaultProvider(EggProvider):"""Provides access to package resources in the filesystem"""def_has(self,path):returnos.path.exists(path)def_isdir(self,path):returnos.path.isdir(path)def_listdir(self,path):returnos.listdir(path)defget_resource_stream(self,manager,resource_name):returnopen(self._fn(self.module_path,resource_name),'rb')def_get(self,path):stream=open(path,'rb')try:returnstream.read()finally:stream.close()register_loader_type(type(None),DefaultProvider)classEmptyProvider(NullProvider):"""Provider that returns nothing for all requests"""_isdir=_has=lambdaself,path:False_get=lambdaself,path:''_listdir=lambdaself,path:[]module_path=Nonedef__init__(self):passempty_provider=EmptyProvider()classZipProvider(EggProvider):"""Resource support for zips and eggs"""eagers=Nonedef__init__(self,module):EggProvider.__init__(self,module)self.zipinfo=zipimport._zip_directory_cache[self.loader.archive]self.zip_pre=self.loader.archive+os.sepdef_zipinfo_name(self,fspath):# Convert a virtual filename (full path to file) into a zipfile subpath# usable with the zipimport directory cache for our target archiveiffspath.startswith(self.zip_pre):returnfspath[len(self.zip_pre):]raiseAssertionError("%s is not a subpath of %s"%(fspath,self.zip_pre))def_parts(self,zip_path):# Convert a zipfile subpath into an egg-relative path part listfspath=self.zip_pre+zip_path# pseudo-fs pathiffspath.startswith(self.egg_root+os.sep):returnfspath[len(self.egg_root)+1:].split(os.sep)raiseAssertionError("%s is not a subpath of %s"%(fspath,self.egg_root))defget_resource_filename(self,manager,resource_name):ifnotself.egg_name:raiseNotImplementedError("resource_filename() only supported for .egg, not .zip")# no need to lock for extraction, since we use temp nameszip_path=self._resource_to_zip(resource_name)eagers=self._get_eager_resources()if'/'.join(self._parts(zip_path))ineagers:fornameineagers:self._extract_resource(manager,self._eager_to_zip(name))returnself._extract_resource(manager,zip_path)def_extract_resource(self,manager,zip_path):ifzip_pathinself._index():fornameinself._index()[zip_path]:last=self._extract_resource(manager,os.path.join(zip_path,name))returnos.path.dirname(last)# return the extracted directory namezip_stat=self.zipinfo[zip_path]t,d,size=zip_stat[5],zip_stat[6],zip_stat[3]date_time=((d>>9)+1980,(d>>5)&0xF,d&0x1F,# ymd(t&0xFFFF)>>11,(t>>5)&0x3F,(t&0x1F)*2,0,0,-1# hms, etc.)timestamp=time.mktime(date_time)try:ifnotWRITE_SUPPORT:raiseIOError('"os.rename" and "os.unlink" are not supported ''on this platform')real_path=manager.get_cache_path(self.egg_name,self._parts(zip_path))ifos.path.isfile(real_path):stat=os.stat(real_path)ifstat.st_size==sizeandstat.st_mtime==timestamp:# size and stamp match, don't bother extractingreturnreal_pathoutf,tmpnam=_mkstemp(".$extract",dir=os.path.dirname(real_path))os.write(outf,self.loader.get_data(zip_path))os.close(outf)utime(tmpnam,(timestamp,timestamp))manager.postprocess(tmpnam,real_path)try:rename(tmpnam,real_path)exceptos.error:ifos.path.isfile(real_path):stat=os.stat(real_path)ifstat.st_size==sizeandstat.st_mtime==timestamp:# size and stamp match, somebody did it just ahead of# us, so we're donereturnreal_pathelifos.name=='nt':# Windows, del old file and retryunlink(real_path)rename(tmpnam,real_path)returnreal_pathraiseexceptos.error:manager.extraction_error()# report a user-friendly errorreturnreal_pathdef_get_eager_resources(self):ifself.eagersisNone:eagers=[]fornamein('native_libs.txt','eager_resources.txt'):ifself.has_metadata(name):eagers.extend(self.get_metadata_lines(name))self.eagers=eagersreturnself.eagersdef_index(self):try:returnself._dirindexexceptAttributeError:ind={}forpathinself.zipinfo:parts=path.split(os.sep)whileparts:parent=os.sep.join(parts[:-1])ifparentinind:ind[parent].append(parts[-1])breakelse:ind[parent]=[parts.pop()]self._dirindex=indreturninddef_has(self,fspath):zip_path=self._zipinfo_name(fspath)returnzip_pathinself.zipinfoorzip_pathinself._index()def_isdir(self,fspath):returnself._zipinfo_name(fspath)inself._index()def_listdir(self,fspath):returnlist(self._index().get(self._zipinfo_name(fspath),()))def_eager_to_zip(self,resource_name):returnself._zipinfo_name(self._fn(self.egg_root,resource_name))def_resource_to_zip(self,resource_name):returnself._zipinfo_name(self._fn(self.module_path,resource_name))register_loader_type(zipimport.zipimporter,ZipProvider)classFileMetadata(EmptyProvider):"""Metadata handler for standalone PKG-INFO files Usage:: metadata = FileMetadata("/path/to/PKG-INFO") This provider rejects all data and metadata requests except for PKG-INFO, which is treated as existing, and will be the contents of the file at the provided location. """def__init__(self,path):self.path=pathdefhas_metadata(self,name):returnname=='PKG-INFO'defget_metadata(self,name):ifname=='PKG-INFO':f=open(self.path,'rU')metadata=f.read()f.close()returnmetadataraiseKeyError("No metadata except PKG-INFO is available")defget_metadata_lines(self,name):returnyield_lines(self.get_metadata(name))classPathMetadata(DefaultProvider):"""Metadata provider for egg directories Usage:: # Development eggs: egg_info = "/path/to/PackageName.egg-info" base_dir = os.path.dirname(egg_info) metadata = PathMetadata(base_dir, egg_info) dist_name = os.path.splitext(os.path.basename(egg_info))[0] dist = Distribution(basedir,project_name=dist_name,metadata=metadata) # Unpacked egg directories: egg_path = "/path/to/PackageName-ver-pyver-etc.egg" metadata = PathMetadata(egg_path, os.path.join(egg_path,'EGG-INFO')) dist = Distribution.from_filename(egg_path, metadata=metadata) """def__init__(self,path,egg_info):self.module_path=pathself.egg_info=egg_infoclassEggMetadata(ZipProvider):"""Metadata provider for .egg files"""def__init__(self,importer):"""Create a metadata provider from a zipimporter"""self.zipinfo=zipimport._zip_directory_cache[importer.archive]self.zip_pre=importer.archive+os.sepself.loader=importerifimporter.prefix:self.module_path=os.path.join(importer.archive,importer.prefix)else:self.module_path=importer.archiveself._setup_prefix()classImpWrapper:"""PEP 302 Importer that wraps Python's "normal" import algorithm"""def__init__(self,path=None):self.path=pathdeffind_module(self,fullname,path=None):subname=fullname.split(".")[-1]ifsubname!=fullnameandself.pathisNone:returnNoneifself.pathisNone:path=Noneelse:path=[self.path]try:file,filename,etc=imp.find_module(subname,path)exceptImportError:returnNonereturnImpLoader(file,filename,etc)classImpLoader:"""PEP 302 Loader that wraps Python's "normal" import algorithm"""def__init__(self,file,filename,etc):self.file=fileself.filename=filenameself.etc=etcdefload_module(self,fullname):try:mod=imp.load_module(fullname,self.file,self.filename,self.etc)finally:ifself.file:self.file.close()# Note: we don't set __loader__ because we want the module to look# normal; i.e. this is just a wrapper for standard import machineryreturnmoddefget_importer(path_item):"""Retrieve a PEP 302 "importer" for the given path item If there is no importer, this returns a wrapper around the builtin import machinery. The returned importer is only cached if it was created by a path hook. """try:importer=sys.path_importer_cache[path_item]exceptKeyError:forhookinsys.path_hooks:try:importer=hook(path_item)exceptImportError:passelse:breakelse:importer=Nonesys.path_importer_cache.setdefault(path_item,importer)ifimporterisNone:try:importer=ImpWrapper(path_item)exceptImportError:passreturnimportertry:frompkgutilimportget_importer,ImpImporterexceptImportError:pass# Python 2.3 or 2.4, use our own implementationelse:ImpWrapper=ImpImporter# Python 2.5, use pkgutil's implementationdelImpLoader,ImpImporter_distribution_finders={}defregister_finder(importer_type,distribution_finder):"""Register `distribution_finder` to find distributions in sys.path items `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item handler), and `distribution_finder` is a callable that, passed a path item and the importer instance, yields ``Distribution`` instances found on that path item. See ``pkg_resources.find_on_path`` for an example."""_distribution_finders[importer_type]=distribution_finderdeffind_distributions(path_item,only=False):"""Yield distributions accessible via `path_item`"""importer=get_importer(path_item)finder=_find_adapter(_distribution_finders,importer)returnfinder(importer,path_item,only)deffind_in_zip(importer,path_item,only=False):metadata=EggMetadata(importer)ifmetadata.has_metadata('PKG-INFO'):yieldDistribution.from_filename(path_item,metadata=metadata)ifonly:return# don't yield nested distrosforsubiteminmetadata.resource_listdir('/'):ifsubitem.endswith('.egg'):subpath=os.path.join(path_item,subitem)fordistinfind_in_zip(zipimport.zipimporter(subpath),subpath):yielddistregister_finder(zipimport.zipimporter,find_in_zip)defStringIO(*args,**kw):"""Thunk to load the real StringIO on demand"""globalStringIOtry:fromcStringIOimportStringIOexceptImportError:fromStringIOimportStringIOreturnStringIO(*args,**kw)deffind_nothing(importer,path_item,only=False):return()register_finder(object,find_nothing)deffind_on_path(importer,path_item,only=False):"""Yield distributions accessible on a sys.path directory"""path_item=_normalize_cached(path_item)ifos.path.isdir(path_item)andos.access(path_item,os.R_OK):ifpath_item.lower().endswith('.egg'):# unpacked eggyieldDistribution.from_filename(path_item,metadata=PathMetadata(path_item,os.path.join(path_item,'EGG-INFO')))else:# scan for .egg and .egg-info in directoryforentryinos.listdir(path_item):lower=entry.lower()iflower.endswith('.egg-info'):fullpath=os.path.join(path_item,entry)ifos.path.isdir(fullpath):# egg-info directory, allow getting metadatametadata=PathMetadata(path_item,fullpath)else:metadata=FileMetadata(fullpath)yieldDistribution.from_location(path_item,entry,metadata,precedence=DEVELOP_DIST)elifnotonlyandlower.endswith('.egg'):fordistinfind_distributions(os.path.join(path_item,entry)):yielddistelifnotonlyandlower.endswith('.egg-link'):forlineinopen(os.path.join(path_item,entry)):ifnotline.strip():continueforiteminfind_distributions(os.path.join(path_item,line.rstrip())):yielditembreakregister_finder(ImpWrapper,find_on_path)_namespace_handlers={}_namespace_packages={}defregister_namespace_handler(importer_type,namespace_handler):"""Register `namespace_handler` to declare namespace packages `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item handler), and `namespace_handler` is a callable like this:: def namespace_handler(importer,path_entry,moduleName,module): # return a path_entry to use for child packages Namespace handlers are only called if the importer object has already agreed that it can handle the relevant path item, and they should only return a subpath if the module __path__ does not already contain an equivalent subpath. For an example namespace handler, see ``pkg_resources.file_ns_handler``. """_namespace_handlers[importer_type]=namespace_handlerdef_handle_ns(packageName,path_item):"""Ensure that named package includes a subpath of path_item (if needed)"""importer=get_importer(path_item)ifimporterisNone:returnNoneloader=importer.find_module(packageName)ifloaderisNone:returnNonemodule=sys.modules.get(packageName)ifmoduleisNone:module=sys.modules[packageName]=types.ModuleType(packageName)module.__path__=[];_set_parent_ns(packageName)elifnothasattr(module,'__path__'):raiseTypeError("Not a package:",packageName)handler=_find_adapter(_namespace_handlers,importer)subpath=handler(importer,path_item,packageName,module)ifsubpathisnotNone:path=module.__path__;path.append(subpath)loader.load_module(packageName);module.__path__=pathreturnsubpathdefdeclare_namespace(packageName):"""Declare that package 'packageName' is a namespace package"""imp.acquire_lock()try:ifpackageNamein_namespace_packages:returnpath,parent=sys.path,Noneif'.'inpackageName:parent='.'.join(packageName.split('.')[:-1])declare_namespace(parent)__import__(parent)try:path=sys.modules[parent].__path__exceptAttributeError:raiseTypeError("Not a package:",parent)# Track what packages are namespaces, so when new path items are added,# they can be updated_namespace_packages.setdefault(parent,[]).append(packageName)_namespace_packages.setdefault(packageName,[])forpath_iteminpath:# Ensure all the parent's path items are reflected in the child,# if they apply_handle_ns(packageName,path_item)finally:imp.release_lock()deffixup_namespace_packages(path_item,parent=None):"""Ensure that previously-declared namespace packages include path_item"""imp.acquire_lock()try:forpackagein_namespace_packages.get(parent,()):subpath=_handle_ns(package,path_item)ifsubpath:fixup_namespace_packages(subpath,package)finally:imp.release_lock()deffile_ns_handler(importer,path_item,packageName,module):"""Compute an ns-package subpath for a filesystem or zipfile importer"""subpath=os.path.join(path_item,packageName.split('.')[-1])normalized=_normalize_cached(subpath)foriteminmodule.__path__:if_normalize_cached(item)==normalized:breakelse:# Only return the path if it's not already therereturnsubpathregister_namespace_handler(ImpWrapper,file_ns_handler)register_namespace_handler(zipimport.zipimporter,file_ns_handler)defnull_ns_handler(importer,path_item,packageName,module):returnNoneregister_namespace_handler(object,null_ns_handler)defnormalize_path(filename):"""Normalize a file/dir name for comparison purposes"""returnos.path.normcase(os.path.realpath(filename))def_normalize_cached(filename,_cache={}):try:return_cache[filename]exceptKeyError:_cache[filename]=result=normalize_path(filename)returnresultdef_set_parent_ns(packageName):parts=packageName.split('.')name=parts.pop()ifparts:parent='.'.join(parts)setattr(sys.modules[parent],name,sys.modules[packageName])defyield_lines(strs):"""Yield non-empty/non-comment lines of a ``basestring`` or sequence"""ifisinstance(strs,basestring):forsinstrs.splitlines():s=s.strip()ifsandnots.startswith('#'):# skip blank lines/commentsyieldselse:forssinstrs:forsinyield_lines(ss):yieldsLINE_END=re.compile(r"\s*(#.*)?$").match# whitespace and commentCONTINUE=re.compile(r"\s*\\\s*(#.*)?$").match# line continuationDISTRO=re.compile(r"\s*((\w|[-.])+)").match# Distribution or extraVERSION=re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|[-.])+)").match# ver. infoCOMMA=re.compile(r"\s*,").match# comma between itemsOBRACKET=re.compile(r"\s*\[").matchCBRACKET=re.compile(r"\s*\]").matchMODULE=re.compile(r"\w+(\.\w+)*$").matchEGG_NAME=re.compile(r"(?P<name>[^-]+)"r"( -(?P<ver>[^-]+) (-py(?P<pyver>[^-]+) (-(?P<plat>.+))? )? )?",re.VERBOSE|re.IGNORECASE).matchcomponent_re=re.compile(r'(\d+ | [a-z]+ | \.| -)',re.VERBOSE)replace={'pre':'c','preview':'c','-':'final-','rc':'c','dev':'@'}.getdef_parse_version_parts(s):forpartincomponent_re.split(s):part=replace(part,part)ifnotpartorpart=='.':continueifpart[:1]in'0123456789':yieldpart.zfill(8)# pad for numeric comparisonelse:yield'*'+partyield'*final'# ensure that alpha/beta/candidate are before finaldefparse_version(s):"""Convert a version string to a chronologically-sortable key This is a rough cross between distutils' StrictVersion and LooseVersion; if you give it versions that would work with StrictVersion, then it behaves the same; otherwise it acts like a slightly-smarter LooseVersion. It is *possible* to create pathological version coding schemes that will fool this parser, but they should be very rare in practice. The returned value will be a tuple of strings. Numeric portions of the version are padded to 8 digits so they will compare numerically, but without relying on how numbers compare relative to strings. Dots are dropped, but dashes are retained. Trailing zeros between alpha segments or dashes are suppressed, so that e.g. "2.4.0" is considered the same as "2.4". Alphanumeric parts are lower-cased. The algorithm assumes that strings like "-" and any alpha string that alphabetically follows "final" represents a "patch level". So, "2.4-1" is assumed to be a branch or patch of "2.4", and therefore "2.4.1" is considered newer than "2.4-1", which in turn is newer than "2.4". Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that come before "final" alphabetically) are assumed to be pre-release versions, so that the version "2.4" is considered newer than "2.4a1". Finally, to handle miscellaneous cases, the strings "pre", "preview", and "rc" are treated as if they were "c", i.e. as though they were release candidates, and therefore are not as new as a version string that does not contain them, and "dev" is replaced with an '@' so that it sorts lower than than any other pre-release tag. """parts=[]forpartin_parse_version_parts(s.lower()):ifpart.startswith('*'):ifpart<'*final':# remove '-' before a prerelease tagwhilepartsandparts[-1]=='*final-':parts.pop()# remove trailing zeros from each series of numeric partswhilepartsandparts[-1]=='00000000':parts.pop()parts.append(part)returntuple(parts)classEntryPoint(object):"""Object representing an advertised importable object"""def__init__(self,name,module_name,attrs=(),extras=(),dist=None):ifnotMODULE(module_name):raiseValueError("Invalid module name",module_name)self.name=nameself.module_name=module_nameself.attrs=tuple(attrs)self.extras=Requirement.parse(("x[%s]"%','.join(extras))).extrasself.dist=distdef__str__(self):s="%s = %s"%(self.name,self.module_name)ifself.attrs:s+=':'+'.'.join(self.attrs)ifself.extras:s+=' [%s]'%','.join(self.extras)returnsdef__repr__(self):return"EntryPoint.parse(%r)"%str(self)defload(self,require=True,env=None,installer=None):ifrequire:self.require(env,installer)entry=__import__(self.module_name,globals(),globals(),['__name__'])forattrinself.attrs:try:entry=getattr(entry,attr)exceptAttributeError:raiseImportError("%r has no %r attribute"%(entry,attr))returnentrydefrequire(self,env=None,installer=None):ifself.extrasandnotself.dist:raiseUnknownExtra("Can't require() without a distribution",self)map(working_set.add,working_set.resolve(self.dist.requires(self.extras),env,installer))#@classmethoddefparse(cls,src,dist=None):"""Parse a single entry point from string `src` Entry point syntax follows the form:: name = some.module:some.attr [extra1,extra2] The entry name and module name are required, but the ``:attrs`` and ``[extras]`` parts are optional """try:attrs=extras=()name,value=src.split('=',1)if'['invalue:value,extras=value.split('[',1)req=Requirement.parse("x["+extras)ifreq.specs:raiseValueErrorextras=req.extrasif':'invalue:value,attrs=value.split(':',1)ifnotMODULE(attrs.rstrip()):raiseValueErrorattrs=attrs.rstrip().split('.')exceptValueError:raiseValueError("EntryPoint must be in 'name=module:attrs [extras]' format",src)else:returncls(name.strip(),value.strip(),attrs,extras,dist)parse=classmethod(parse)#@classmethoddefparse_group(cls,group,lines,dist=None):"""Parse an entry point group"""ifnotMODULE(group):raiseValueError("Invalid group name",group)this={}forlineinyield_lines(lines):ep=cls.parse(line,dist)ifep.nameinthis:raiseValueError("Duplicate entry point",group,ep.name)this[ep.name]=epreturnthisparse_group=classmethod(parse_group)#@classmethoddefparse_map(cls,data,dist=None):"""Parse a map of entry point groups"""ifisinstance(data,dict):data=data.items()else:data=split_sections(data)maps={}forgroup,linesindata:ifgroupisNone:ifnotlines:continueraiseValueError("Entry points must be listed in groups")group=group.strip()ifgroupinmaps:raiseValueError("Duplicate group name",group)maps[group]=cls.parse_group(group,lines,dist)returnmapsparse_map=classmethod(parse_map)def_remove_md5_fragment(location):ifnotlocation:return''parsed=urlparse(location)ifparsed[-1].startswith('md5='):returnurlunparse(parsed[:-1]+('',))returnlocationclassDistribution(object):"""Wrap an actual or potential sys.path entry w/metadata"""def__init__(self,location=None,metadata=None,project_name=None,version=None,py_version=PY_MAJOR,platform=None,precedence=EGG_DIST):self.project_name=safe_name(project_nameor'Unknown')ifversionisnotNone:self._version=safe_version(version)self.py_version=py_versionself.platform=platformself.location=locationself.precedence=precedenceself._provider=metadataorempty_provider#@classmethoddeffrom_location(cls,location,basename,metadata=None,**kw):project_name,version,py_version,platform=[None]*4basename,ext=os.path.splitext(basename)ifext.lower()in(".egg",".egg-info"):match=EGG_NAME(basename)ifmatch:project_name,version,py_version,platform=match.group('name','ver','pyver','plat')returncls(location,metadata,project_name=project_name,version=version,py_version=py_version,platform=platform,**kw)from_location=classmethod(from_location)hashcmp=property(lambdaself:(getattr(self,'parsed_version',()),self.precedence,self.key,_remove_md5_fragment(self.location),self.py_version,self.platform))def__hash__(self):returnhash(self.hashcmp)def__lt__(self,other):returnself.hashcmp<other.hashcmpdef__le__(self,other):returnself.hashcmp<=other.hashcmpdef__gt__(self,other):returnself.hashcmp>other.hashcmpdef__ge__(self,other):returnself.hashcmp>=other.hashcmpdef__eq__(self,other):ifnotisinstance(other,self.__class__):# It's not a Distribution, so they are not equalreturnFalsereturnself.hashcmp==other.hashcmpdef__ne__(self,other):returnnotself==other# These properties have to be lazy so that we don't have to load any# metadata until/unless it's actually needed. (i.e., some distributions# may not know their name or version without loading PKG-INFO)#@propertydefkey(self):try:returnself._keyexceptAttributeError:self._key=key=self.project_name.lower()returnkeykey=property(key)#@propertydefparsed_version(self):try:returnself._parsed_versionexceptAttributeError:self._parsed_version=pv=parse_version(self.version)returnpvparsed_version=property(parsed_version)#@propertydefversion(self):try:returnself._versionexceptAttributeError:forlineinself._get_metadata('PKG-INFO'):ifline.lower().startswith('version:'):self._version=safe_version(line.split(':',1)[1].strip())returnself._versionelse:raiseValueError("Missing 'Version:' header and/or PKG-INFO file",self)version=property(version)#@propertydef_dep_map(self):try:returnself.__dep_mapexceptAttributeError:dm=self.__dep_map={None:[]}fornamein'requires.txt','depends.txt':forextra,reqsinsplit_sections(self._get_metadata(name)):ifextra:extra=safe_extra(extra)dm.setdefault(extra,[]).extend(parse_requirements(reqs))returndm_dep_map=property(_dep_map)defrequires(self,extras=()):"""List of Requirements needed for this distro if `extras` are used"""dm=self._dep_mapdeps=[]deps.extend(dm.get(None,()))forextinextras:try:deps.extend(dm[safe_extra(ext)])exceptKeyError:raiseUnknownExtra("%s has no such extra feature %r"%(self,ext))returndepsdef_get_metadata(self,name):ifself.has_metadata(name):forlineinself.get_metadata_lines(name):yieldlinedefactivate(self,path=None):"""Ensure distribution is importable on `path` (default=sys.path)"""ifpathisNone:path=sys.pathself.insert_on(path)ifpathissys.path:fixup_namespace_packages(self.location)map(declare_namespace,self._get_metadata('namespace_packages.txt'))defegg_name(self):"""Return what this distribution's standard .egg filename should be"""filename="%s-%s-py%s"%(to_filename(self.project_name),to_filename(self.version),self.py_versionorPY_MAJOR)ifself.platform:filename+='-'+self.platformreturnfilenamedef__repr__(self):ifself.location:return"%s (%s)"%(self,self.location)else:returnstr(self)def__str__(self):try:version=getattr(self,'version',None)exceptValueError:version=Noneversion=versionor"[unknown version]"return"%s%s"%(self.project_name,version)def__getattr__(self,attr):"""Delegate all unrecognized public attributes to .metadata provider"""ifattr.startswith('_'):raiseAttributeError,attrreturngetattr(self._provider,attr)#@classmethoddeffrom_filename(cls,filename,metadata=None,**kw):returncls.from_location(_normalize_cached(filename),os.path.basename(filename),metadata,**kw)from_filename=classmethod(from_filename)defas_requirement(self):"""Return a ``Requirement`` that matches this distribution exactly"""returnRequirement.parse('%s==%s'%(self.project_name,self.version))defload_entry_point(self,group,name):"""Return the `name` entry point of `group` or raise ImportError"""ep=self.get_entry_info(group,name)ifepisNone:raiseImportError("Entry point %r not found"%((group,name),))returnep.load()defget_entry_map(self,group=None):"""Return the entry point map for `group`, or the full entry map"""try:ep_map=self._ep_mapexceptAttributeError:ep_map=self._ep_map=EntryPoint.parse_map(self._get_metadata('entry_points.txt'),self)ifgroupisnotNone:returnep_map.get(group,{})returnep_mapdefget_entry_info(self,group,name):"""Return the EntryPoint object for `group`+`name`, or ``None``"""returnself.get_entry_map(group).get(name)definsert_on(self,path,loc=None):"""Insert self.location in path before its nearest parent directory"""loc=locorself.locationifself.project_name=='setuptools':try:version=self.versionexceptValueError:version=''if'0.7'inversion:raiseValueError("A 0.7-series setuptools cannot be installed ""with distribute. Found one at %s"%str(self.location))ifnotloc:returnifpathissys.path:self.check_version_conflict()nloc=_normalize_cached(loc)bdir=os.path.dirname(nloc)npath=map(_normalize_cached,path)bp=Noneforp,iteminenumerate(npath):ifitem==nloc:breakelifitem==bdirandself.precedence==EGG_DIST:# if it's an .egg, give it precedence over its directorypath.insert(p,loc)npath.insert(p,nloc)breakelse:path.append(loc)return# p is the spot where we found or inserted loc; now remove duplicateswhile1:try:np=npath.index(nloc,p+1)exceptValueError:breakelse:delnpath[np],path[np]p=np# ha!returndefcheck_version_conflict(self):ifself.key=='distribute':return# ignore the inevitable setuptools self-conflicts :(nsp=dict.fromkeys(self._get_metadata('namespace_packages.txt'))loc=normalize_path(self.location)formodnameinself._get_metadata('top_level.txt'):if(modnamenotinsys.modulesormodnameinnspormodnamein_namespace_packages):continueifmodnamein('pkg_resources','setuptools','site'):continuefn=getattr(sys.modules[modname],'__file__',None)iffnand(normalize_path(fn).startswith(loc)orfn.startswith(self.location)):continueissue_warning("Module %s was already imported from %s, but %s is being added"" to sys.path"%(modname,fn,self.location),)defhas_version(self):try:self.versionexceptValueError:issue_warning("Unbuilt egg for "+repr(self))returnFalsereturnTruedefclone(self,**kw):"""Copy this distribution, substituting in any changed keyword args"""forattrin('project_name','version','py_version','platform','location','precedence'):kw.setdefault(attr,getattr(self,attr,None))kw.setdefault('metadata',self._provider)returnself.__class__(**kw)#@propertydefextras(self):return[depfordepinself._dep_mapifdep]extras=property(extras)defissue_warning(*args,**kw):level=1g=globals()try:# find the first stack frame that is *not* code in# the pkg_resources module, to use for the warningwhilesys._getframe(level).f_globalsisg:level+=1exceptValueError:passfromwarningsimportwarnwarn(stacklevel=level+1,*args,**kw)defparse_requirements(strs):"""Yield ``Requirement`` objects for each specification in `strs` `strs` must be an instance of ``basestring``, or a (possibly-nested) iterable thereof. """# create a steppable iterator, so we can handle \-continuationslines=iter(yield_lines(strs))defscan_list(ITEM,TERMINATOR,line,p,groups,item_name):items=[]whilenotTERMINATOR(line,p):ifCONTINUE(line,p):try:line=lines.next();p=0exceptStopIteration:raiseValueError("\\ must not appear on the last nonblank line")match=ITEM(line,p)ifnotmatch:raiseValueError("Expected "+item_name+" in",line,"at",line[p:])items.append(match.group(*groups))p=match.end()match=COMMA(line,p)ifmatch:p=match.end()# skip the commaelifnotTERMINATOR(line,p):raiseValueError("Expected ',' or end-of-list in",line,"at",line[p:])match=TERMINATOR(line,p)ifmatch:p=match.end()# skip the terminator, if anyreturnline,p,itemsforlineinlines:match=DISTRO(line)ifnotmatch:raiseValueError("Missing distribution spec",line)project_name=match.group(1)p=match.end()extras=[]match=OBRACKET(line,p)ifmatch:p=match.end()line,p,extras=scan_list(DISTRO,CBRACKET,line,p,(1,),"'extra' name")line,p,specs=scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")specs=[(op,safe_version(val))forop,valinspecs]yieldRequirement(project_name,specs,extras)def_sort_dists(dists):tmp=[(dist.hashcmp,dist)fordistindists]tmp.sort()dists[::-1]=[dforhc,dintmp]classRequirement:def__init__(self,project_name,specs,extras):"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""self.unsafe_name,project_name=project_name,safe_name(project_name)self.project_name,self.key=project_name,project_name.lower()index=[(parse_version(v),state_machine[op],op,v)forop,vinspecs]index.sort()self.specs=[(op,ver)forparsed,trans,op,verinindex]self.index,self.extras=index,tuple(map(safe_extra,extras))self.hashCmp=(self.key,tuple([(op,parsed)forparsed,trans,op,verinindex]),frozenset(self.extras))self.__hash=hash(self.hashCmp)def__str__(self):specs=','.join([''.join(s)forsinself.specs])extras=','.join(self.extras)ifextras:extras='[%s]'%extrasreturn'%s%s%s'%(self.project_name,extras,specs)def__eq__(self,other):returnisinstance(other,Requirement)andself.hashCmp==other.hashCmpdef__contains__(self,item):ifisinstance(item,Distribution):ifitem.key<>self.key:returnFalseifself.index:item=item.parsed_version# only get if we need itelifisinstance(item,basestring):item=parse_version(item)last=Nonecompare=lambdaa,b:(a>b)-(a<b)# -1, 0, 1forparsed,trans,op,verinself.index:action=trans[compare(item,parsed)]# Indexing: 0, 1, -1ifaction=='F':returnFalseelifaction=='T':returnTrueelifaction=='+':last=Trueelifaction=='-'orlastisNone:last=FalseiflastisNone:last=True# no rules encounteredreturnlastdef__hash__(self):returnself.__hashdef__repr__(self):return"Requirement.parse(%r)"%str(self)#@staticmethoddefparse(s,replacement=True):reqs=list(parse_requirements(s))ifreqs:iflen(reqs)==1:founded_req=reqs[0]# if asked for setuptools distribution# and if distribute is installed, we want to give# distribute insteadif_override_setuptools(founded_req)andreplacement:distribute=list(parse_requirements('distribute'))iflen(distribute)==1:returndistribute[0]returnfounded_reqelse:returnfounded_reqraiseValueError("Expected only one requirement",s)raiseValueError("No requirements found",s)parse=staticmethod(parse)state_machine={# =><'<':'--T','<=':'T-T','>':'F+F','>=':'T+F','==':'T..','!=':'F++',}def_override_setuptools(req):"""Return True when distribute wants to override a setuptools dependency. We want to override when the requirement is setuptools and the version is a variant of 0.6. """ifreq.project_name=='setuptools':ifnotlen(req.specs):# Just setuptools: okreturnTrueforcomparator,versioninreq.specs:ifcomparatorin['==','>=','>']:if'0.7'inversion:# We want some setuptools not from the 0.6 series.returnFalsereturnTruereturnFalsedef_get_mro(cls):"""Get an mro for a type or classic class"""ifnotisinstance(cls,type):classcls(cls,object):passreturncls.__mro__[1:]returncls.__mro__def_find_adapter(registry,ob):"""Return an adapter factory for `ob` from `registry`"""fortin_get_mro(getattr(ob,'__class__',type(ob))):iftinregistry:returnregistry[t]defensure_directory(path):"""Ensure that the parent directory of `path` exists"""dirname=os.path.dirname(path)ifnotos.path.isdir(dirname):os.makedirs(dirname)defsplit_sections(s):"""Split a string or iterable thereof into (section,content) pairs Each ``section`` is a stripped version of the section header ("[section]") and each ``content`` is a list of stripped lines excluding blank lines and comment-only lines. If there are any such lines before the first section header, they're returned in a first ``section`` of ``None``. """section=Nonecontent=[]forlineinyield_lines(s):ifline.startswith("["):ifline.endswith("]"):ifsectionorcontent:yieldsection,contentsection=line[1:-1].strip()content=[]else:raiseValueError("Invalid section heading",line)else:content.append(line)# wrap up last segmentyieldsection,contentdef_mkstemp(*args,**kw):fromtempfileimportmkstempold_open=os.opentry:os.open=os_open# temporarily bypass sandboxingreturnmkstemp(*args,**kw)finally:os.open=old_open# and then put it back# Set up global resource manager_manager=ResourceManager()def_initialize(g):fornameindir(_manager):ifnotname.startswith('_'):g[name]=getattr(_manager,name)_initialize(globals())# Prepare the master working set and make the ``require()`` API availableworking_set=WorkingSet()try:# Does the main program list any requirements?from__main__import__requires__exceptImportError:pass# No: just use the default working set based on sys.pathelse:# Yes: ensure the requirements are met, by prefixing sys.path if necessarytry:working_set.require(__requires__)exceptVersionConflict:# try it without defaults already on sys.pathworking_set=WorkingSet([])# by starting with an empty pathfordistinworking_set.resolve(parse_requirements(__requires__),Environment()):working_set.add(dist)forentryinsys.path:# add any missing entries from sys.pathifentrynotinworking_set.entries:working_set.add_entry(entry)sys.path[:]=working_set.entries# then copy back to sys.pathrequire=working_set.requireiter_entry_points=working_set.iter_entry_pointsadd_activation_listener=working_set.subscriberun_script=working_set.run_scriptrun_main=run_script# backward compatibility# Activate all distributions already on sys.path, and ensure that# all distributions added to the working set in the future (e.g. by# calling ``require()``) will get activated as well.add_activation_listener(lambdadist:dist.activate())working_set.entries=[];map(working_set.add_entry,sys.path)# match order