3 Pf@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z mZddlmZddlmZmZddlmZmZmZdd lmZmZmZmZmZmZmZd d d d dgZ ej!e"Z#dZ$dZ%deddde$dfZ&dZ'Gddde(Z)Gddde(Z*Gdd d e(Z+Gdd d e+Z,Gdd d e,Z-Gdd d e,Z.e-Z/e.Z0Gddde(Z1d)d!d"Z2d#d$Z3d%d&Z4d'd(Z5dS)*zPEP 376 implementation.)unicode_literalsN)DistlibException resources)StringIO) get_schemeUnsupportedVersionError)MetadataMETADATA_FILENAMEWHEEL_METADATA_FILENAME)parse_requirementcached_propertyparse_name_and_version read_exports write_exports CSVReader CSVWriter DistributionBaseInstalledDistributionInstalledDistributionEggInfoDistributionDistributionPathzpydist-exports.jsonzpydist-commands.jsonZ INSTALLERRECORD REQUESTED RESOURCESSHAREDz .dist-infoc@s(eZdZdZddZddZddZdS) _CachezL A simple cache mapping names and .dist-info paths to distributions cCsi|_i|_d|_dS)zZ Initialise an instance. There is normally one for each DistributionPath. FN)namepath generated)selfr!/usr/lib/python3.6/database.py__init__0sz_Cache.__init__cCs|jj|jjd|_dS)zC Clear the cache, setting it to its initial state. FN)rclearrr)r r!r!r"r$8s  z _Cache.clearcCs2|j|jkr.||j|j<|jj|jgj|dS)z` Add a distribution to the cache. :param dist: The distribution to add. N)rr setdefaultkeyappend)r distr!r!r"add@s  z _Cache.addN)__name__ __module__ __qualname____doc__r#r$r)r!r!r!r"r,src@seZdZdZdddZddZdd ZeeeZd d Z d d Z ddZ e ddZ ddZddZdddZddZdddZdS)rzU Represents a set of distributions installed on a path (typically sys.path). NFcCsD|dkrtj}||_d|_||_t|_t|_d|_td|_ dS)a Create an instance from a path, optionally including legacy (distutils/ setuptools/distribute) distributions. :param path: The path to use, as a list of directories. If not specified, sys.path is used. :param include_egg: If True, this instance will look for and return legacy distributions as well as those based on PEP 376. NTdefault) sysr _include_dist _include_eggr_cache _cache_egg_cache_enabledr_scheme)r rZ include_eggr!r!r"r#Ns zDistributionPath.__init__cCs|jS)N)r4)r r!r!r"_get_cache_enabledbsz#DistributionPath._get_cache_enabledcCs ||_dS)N)r4)r valuer!r!r"_set_cache_enabledesz#DistributionPath._set_cache_enabledcCs|jj|jjdS)z, Clears the internal cache. N)r2r$r3)r r!r!r" clear_cachejs zDistributionPath.clear_cachec csTt}xF|jD]:}tj|}|dkr*q|jd}| s|j rDqt|j}x|D]}|j|}| sV|j|krvqV|jo|jt rt t g}x*|D] }t j ||} |j| } | rPqWqVtj| j} t| dd} WdQRXtjd|j|j|jt|j| |dVqV|jrV|jd rVtjd|j|j|jt|j|VqVWqWdS) zD Yield .dist-info and/or .egg(-info) distributions. Nlegacy)fileobjschemezFound %s)metadataenv .egg-info.egg)r@rA)setrrfinder_for_pathfindZ is_containersortedr0endswith DISTINFO_EXTr r posixpathjoin contextlibclosing as_streamr loggerdebugr)new_dist_classr1old_dist_class) r seenrfinderrZrsetentryZpossible_filenamesZmetadata_filenameZ metadata_pathZpydiststreamr>r!r!r"_yield_distributionsrs@            z%DistributionPath._yield_distributionscCst|jj }|jo|jj }|s"|rpx4|jD](}t|trH|jj|q,|jj|q,W|rdd|j_|rpd|j_dS)zk Scan the path for distributions and populate the cache with those that are found. TN)r2rr1r3rV isinstancerr))r Zgen_distZgen_eggr(r!r!r"_generate_caches  z DistributionPath._generate_cachecCs|jdd}dj||gtS)ao The *name* and *version* parameters are converted into their filename-escaped form, i.e. any ``'-'`` characters are replaced with ``'_'`` other than the one in ``'dist-info'`` and the one separating the name from the version number. :parameter name: is converted to a standard distribution name by replacing any runs of non- alphanumeric characters with a single ``'-'``. :type name: string :parameter version: is converted to a standard version string. Spaces become dots, and all other non-alphanumeric characters (except dots) become dashes, with runs of multiple dashes condensed to a single dash. :type version: string :returns: directory name :rtype: string-_)replacerIrG)clsrversionr!r!r"distinfo_dirnames z!DistributionPath.distinfo_dirnameccsj|js x^|jD] }|VqWnF|jx|jjjD] }|Vq6W|jrfx|jjjD] }|VqXWdS)a5 Provides an iterator that looks for distributions and returns :class:`InstalledDistribution` or :class:`EggInfoDistribution` instances for each one of them. :rtype: iterator of :class:`InstalledDistribution` and :class:`EggInfoDistribution` instances N)r4rVrXr2rvaluesr1r3)r r(r!r!r"get_distributionss   z"DistributionPath.get_distributionscCsd}|j}|js6xj|jD]}|j|kr|}PqWnH|j||jjkr\|jj|d}n"|jr~||jjkr~|jj|d}|S)a= Looks for a named distribution on the path. This function only returns the first result found, as no more than one value is expected. If nothing is found, ``None`` is returned. :rtype: :class:`InstalledDistribution`, :class:`EggInfoDistribution` or ``None`` Nr) lowerr4rVr&rXr2rr1r3)r rresultr(r!r!r"get_distributions   z!DistributionPath.get_distributionc csd}|dk rJy|jjd||f}Wn$tk rHtd||fYnXxd|jD]X}|j}xL|D]D}t|\}}|dkr||kr|VPqd||krd|j|rd|VPqdWqTWdS)a Iterates over all distributions to find which distributions provide *name*. If a *version* is provided, it will be used to filter the results. This function only returns the first result found, since no more than one values are expected. If the directory is not found, returns ``None``. :parameter version: a version specifier that indicates the version required, conforming to the format in ``PEP-345`` :type name: string :type version: string Nz%s (%s)zinvalid name or version: %r, %r)r5matcher ValueErrorrr`providesrmatch) r rr]rdr(providedpp_namep_verr!r!r"provides_distributions$  z&DistributionPath.provides_distributioncCs(|j|}|dkrtd||j|S)z5 Return the path to a resource file. Nzno distribution named %r found)rc LookupErrorget_resource_path)r r relative_pathr(r!r!r" get_file_paths  zDistributionPath.get_file_pathccs`xZ|jD]N}|j}||kr ||}|dk r@||krX||Vq x|jD] }|VqJWq WdS)z Return all of the exported entries in a particular category. :param category: The category to search for entries. :param name: If specified, only entries with that name are returned. N)r`exportsr_)r categoryrr(rSdvr!r!r"get_exported_entries"s z%DistributionPath.get_exported_entries)NF)N)N)r*r+r,r-r#r6r8propertyZ cache_enabledr9rVrX classmethodr^r`rcrlrprur!r!r!r"rJs  *  $ c@seZdZdZdZdZddZeddZeZ eddZ ed d Z d d Z ed dZ eddZeddZeddZeddZddZddZddZddZdS) rz A base class for distributions, whether installed or from indexes. Either way, it must have some metadata, so that's all that's needed for construction. FcCsL||_|j|_|jj|_|j|_d|_d|_d|_d|_t |_ i|_ dS)z Initialise an instance. :param metadata: The instance of :class:`Metadata` describing this distribution. N) r>rrar&r]ZlocatordigestextrascontextrBZ download_urlsZdigests)r r>r!r!r"r#Gs zDistribution.__init__cCs|jjS)zH The source archive download URL for this distribution. )r> source_url)r r!r!r"r{XszDistribution.source_urlcCsd|j|jfS)zX A utility property which displays the name and version in parentheses. z%s (%s))rr])r r!r!r"name_and_versionaszDistribution.name_and_versioncCs.|jj}d|j|jf}||kr*|j||S)z A set of distribution names and versions provided by this distribution. :return: A set of "name (version)" strings. z%s (%s))r>rfrr]r')r Zplistsr!r!r"rfhs  zDistribution.providescCs8|j}tjd|jt||}t|j||j|jdS)Nz%Getting requirements from metadata %r)ryr?) r>rMrNZtodictgetattrrBZget_requirementsryrz)r Zreq_attrmdZreqtsr!r!r"_get_requirementsts   zDistribution._get_requirementscCs |jdS)N run_requires)r)r r!r!r"r{szDistribution.run_requirescCs |jdS)N meta_requires)r)r r!r!r"rszDistribution.meta_requirescCs |jdS)Nbuild_requires)r)r r!r!r"rszDistribution.build_requirescCs |jdS)N test_requires)r)r r!r!r"rszDistribution.test_requirescCs |jdS)N dev_requires)r)r r!r!r"rszDistribution.dev_requiresc Cst|}t|jj}y|j|j}Wn6tk rZtjd||j d}|j|}YnX|j }d}xJ|j D]@}t |\}} ||krqny|j | }PWqntk rYqnXqnW|S)z Say if this instance matches (fulfills) a requirement. :param req: The requirement to match. :rtype req: str :return: True if it matches, else False. z+could not read version %r - using name onlyrF)r rr>r=rd requirementrrMwarningsplitr&rfrrg) r reqrSr=rdrrbrirjrkr!r!r"matches_requirements*       z Distribution.matches_requirementcCs(|jrd|j}nd}d|j|j|fS)zC Return a textual representation of this instance, z [%s]r:z)r{rr])r suffixr!r!r"__repr__s zDistribution.__repr__cCs>t|t|k rd}n$|j|jko8|j|jko8|j|jk}|S)a< See if this distribution is the same as another. :param other: The distribution to compare with. To be equal to one another. distributions must have the same type, name, version and source_url. :return: True if it is the same, else False. F)typerr]r{)r otherrbr!r!r"__eq__s    zDistribution.__eq__cCst|jt|jt|jS)zH Compute hash in a way which matches the equality test. )hashrr]r{)r r!r!r"__hash__szDistribution.__hash__N)r*r+r,r-Zbuild_time_dependency requestedr#rvr{Z download_urlr|rfrrrrrrrrrrr!r!r!r"r5s$        " cs0eZdZdZdZdfdd ZdddZZS) rz] This is the base class for installed distributions (whether PEP 376 or legacy). Ncs tt|j|||_||_dS)a Initialise an instance. :param metadata: An instance of :class:`Metadata` which describes the distribution. This will normally have been initialised from a metadata file in the ``path``. :param path: The path of the ``.dist-info`` or ``.egg-info`` directory for the distribution. :param env: This is normally the :class:`DistributionPath` instance where this distribution was found. N)superrr#r dist_path)r r>rr?) __class__r!r"r#s z"BaseInstalledDistribution.__init__cCsd|dkr|j}|dkr"tj}d}ntt|}d|j}||j}tj|jdjd}d||fS)a Get the hash of some data, using a particular hash algorithm, if specified. :param data: The data to be hashed. :type data: bytes :param hasher: The name of a hash implementation, supported by hashlib, or ``None``. Examples of valid values are ``'sha1'``, ``'sha224'``, ``'sha384'``, '``sha256'``, ``'md5'`` and ``'sha512'``. If no hasher is specified, the ``hasher`` attribute of the :class:`InstalledDistribution` instance is used. If the hasher is determined to be ``None``, MD5 is used as the hashing algorithm. :returns: The hash of the data. If a hasher was explicitly specified, the returned hash will be prefixed with the specified hasher followed by '='. :rtype: str Nr:z%s==asciiz%s%s) hasherhashlibmd5r~rxbase64Zurlsafe_b64encoderstripdecode)r datarprefixrxr!r!r"get_hashs   z"BaseInstalledDistribution.get_hash)N)N)r*r+r,r-rr#r __classcell__r!r!)rr"rscseZdZdZdZd'fdd ZddZdd Zd d Ze d d Z ddZ ddZ ddZ ddZd(ddZddZe ddZd)ddZdd Zd!d"Zd#d$Zd%d&ZejZZS)*ra  Created with the *path* of the ``.dist-info`` directory provided to the constructor. It reads the metadata contained in ``pydist.json`` when it is instantiated., or uses a passed in Metadata instance (useful for when dry-run mode is being used). Zsha256Ncs0tj||_}|dkr(ddl}|j|rN|jrN||jjkrN|jj|j}nt|dkr|j t }|dkrr|j t }|dkr|j d}|dkrt dt |ft j|j}t|dd}WdQRXtt|j||||r|jr|jj|y|j d}Wn&tk r ddl}|jYnX|dk |_dS)NrZMETADATAzno %s found in %sr;)r<r=r)rrCrRpdbZ set_tracer4r2rr>rDr r rerJrKrLr rrr#r)AttributeErrorr)r rr>r?rRrrSrU)rr!r"r#s4      zInstalledDistribution.__init__cCsd|j|j|jfS)Nz#)rr]r)r r!r!r"r2szInstalledDistribution.__repr__cCsd|j|jfS)Nz%s %s)rr])r r!r!r"__str__6szInstalledDistribution.__str__c Csg}|jd}tj|j`}t|dJ}xB|D]:}ddtt|dD}||\}}} |j||| fq0WWdQRXWdQRX|S)a" Get the list of installed files for the distribution :return: A list of tuples of path, hash and size. Note that hash and size might be ``None`` for some entries. The path is exactly as stored in the file (which is as in PEP 376). r)rUcSsg|]}dqS)Nr!).0ir!r!r" Hsz6InstalledDistribution._get_records..N)get_distinfo_resourcerJrKrLrrangelenr') r resultsrSrUZ record_readerrowmissingrchecksumsizer!r!r" _get_records9s   (z"InstalledDistribution._get_recordscCsi}|jt}|r|j}|S)a Return the information exported by this distribution. :return: A dictionary of exports, mapping an export category to a dict of :class:`ExportEntry` instances describing the individual export entries, and keyed by name. )rEXPORTS_FILENAMEr)r rbrSr!r!r"rqPs  zInstalledDistribution.exportsc Cs8i}|jt}|r4tj|j}t|}WdQRX|S)z Read exports data from a file in .ini format. :return: A dictionary of exports, mapping an export category to a list of :class:`ExportEntry` instances describing the individual export entries. N)rrrJrKrLr)r rbrSrUr!r!r"r^s  z"InstalledDistribution.read_exportsc Cs.|jt}t|d}t||WdQRXdS)a Write a dictionary of exports to a file in .ini format. :param exports: A dictionary of exports, mapping an export category to a list of :class:`ExportEntry` instances describing the individual export entries. wN)get_distinfo_fileropenr)r rqZrffr!r!r"rms  z#InstalledDistribution.write_exportscCsh|jd}tj|j:}t|d$}x|D]\}}||kr,|Sq,WWdQRXWdQRXtd|dS)aW NOTE: This API may change in the future. Return the absolute path to a resource file with the given relative path. :param relative_path: The path, relative to .dist-info, of the resource of interest. :return: The absolute path where the resource is to be found. r)rUNz3no resource file with relative path %r is installed)rrJrKrLrKeyError)r rorSrUZresources_readerZrelativeZ destinationr!r!r"rnxs  z'InstalledDistribution.get_resource_pathccsx|jD] }|Vq WdS)z Iterates over the ``RECORD`` entries and returns a tuple ``(path, hash, size)`` for each line. :returns: iterator of (path, hash, size) N)r)r rbr!r!r"list_installed_filessz*InstalledDistribution.list_installed_filesFc Cs,tjj|d}tjj|j}|j|}tjj|d}|jd}tjd||rRdSt|}x|D]}tjj |s||j d rd} } n4dtjj |} t |d} |j | j} WdQRX|j|s|r|j|rtjj||}|j|| | fqbW|j|rtjj||}|j|ddfWdQRX|S) z Writes the ``RECORD`` file, using the ``paths`` iterable passed in. Any existing ``RECORD`` file is silently overwritten. prefix is used to determine when to write absolute paths. r:rz creating %sN.pyc.pyoz%drb)rr)osrrIdirname startswithrrMinforisdirrFgetsizerrreadrelpathZwriterow) r pathsrdry_runbaseZbase_under_prefix record_pathwriterr hash_valuerfpr!r!r"write_installed_filess.         z+InstalledDistribution.write_installed_filesc Csg}tjj|j}|jd}x|jD]\}}}tjj|sLtjj||}||krVq(tjj|sv|j|dddfq(tjj |r(t tjj |}|r||kr|j|d||fq(|r(d|kr|j ddd}nd }t |d 2} |j| j|} | |kr |j|d || fWd QRXq(W|S) a Checks that the hashes and sizes of the files in ``RECORD`` are matched by the files themselves. Returns a (possibly empty) list of mismatches. Each entry in the mismatch list will be a tuple consisting of the path, 'exists', 'size' or 'hash' according to what didn't match (existence is checked first, then size, then hash), the expected value and the actual value. rexistsTFr=rrNrr)rrrrrisabsrIrr'isfilestrrrrrr) r mismatchesrrrrrZ actual_sizerrZ actual_hashr!r!r"check_installed_filess.         z+InstalledDistribution.check_installed_filesc Csi}tjj|jd}tjj|rtj|ddd}|jj}WdQRXx@|D]8}|jdd\}}|dkr~|j |gj |qN|||<qNW|S) a A dictionary of shared locations whose keys are in the set 'prefix', 'purelib', 'platlib', 'scripts', 'headers', 'data' and 'namespace'. The corresponding value is the absolute path of that category for this distribution, and takes into account any paths selected by the user at installation time (e.g. via command-line arguments). In the case of the 'namespace' key, this would be a list of absolute paths for the roots of namespace packages in this distribution. The first time this property is accessed, the relevant information is read from the SHARED file in the .dist-info directory. rrSzutf-8)encodingNrr namespace) rrrIrcodecsrr splitlinesrr%r')r rb shared_pathrlinesliner&r7r!r!r"shared_locationss   z&InstalledDistribution.shared_locationsc Cstjj|jd}tjd||r$dSg}x6dD].}||}tjj||r.|jd ||fq.Wx"|jd fD]}|jd |qnWtj |d d d}|j dj|WdQRX|S)aa Write shared location information to the SHARED file in .dist-info. :param paths: A dictionary as described in the documentation for :meth:`shared_locations`. :param dry_run: If True, the action is logged but no file is actually written. :return: The path of the file written to. rz creating %sNrlibheadersscriptsrz%s=%srz namespace=%srzutf-8)r )rrrrr) rrrIrMrrr'getrrwrite) r rrrrr&rnsrr!r!r"write_shared_locationss   z,InstalledDistribution.write_shared_locationscCsF|tkrtd||jftj|j}|dkr.set_name_and_version) rrr4r3r>rr] _get_metadatar)rrr#)r rr?rr>)rr!r"r#Ws   zEggInfoDistribution.__init__c s2d}ddfdd}|jdrtjj|rdtjj|dd}t|dd }tjj|dd }||}n`tj|}t|j d j d }t|dd }y|j d} | j d}Wnt k rd}YnXnX|jdrtjj|rtjj|d }||}tjj|d}t|dd }n t d||r.|j ||S)NcSsg}|j}x|D]}|j}|jdr6tjd|Pt|}|sPtjd|q|jr`tjd|jst|j|j qdj dd|jD}|jd|j |fqW|S) zCreate a list of dependencies from a requires.txt file. *data*: the contents of a setuptools-produced requires.txt file. [z.Unexpected line: quitting requirement scan: %rz#Not recognised as a requirement: %rz4extra requirements in requires.txt are not supportedz, css|]}d|VqdS)z%s%sNr!)rcr!r!r" szQEggInfoDistribution._get_metadata..parse_requires_data..z%s (%s)) rstriprrMrr ryZ constraintsr'rrI)rreqsrrrSZconsr!r!r"parse_requires_dataos&    z>EggInfoDistribution._get_metadata..parse_requires_datacsHg}y*tj|dd}|j}WdQRXWntk rBYnX|S)zCreate a list of dependencies from a requires.txt file. *req_path*: the path to a setuptools-produced requires.txt file. rSzutf-8N)rrrIOError)req_pathrr)rr!r"parse_requires_pathsz>EggInfoDistribution._get_metadata..parse_requires_pathz.eggzEGG-INFOzPKG-INFOr;)rr=z requires.txtzEGG-INFO/PKG-INFOutf8)r<r=zEGG-INFO/requires.txtzutf-8z .egg-infoz,path must end with .egg-info or .egg, got %r)rFrrrrIr zipimport zipimporterrget_datarrrZadd_requirements) r rrequiresr meta_pathr>rZzipfr<rr!)rr"rls:           z!EggInfoDistribution._get_metadatacCsd|j|j|jfS)Nz!)rr]r)r r!r!r"rszEggInfoDistribution.__repr__cCsd|j|jfS)Nz%s %s)rr])r r!r!r"rszEggInfoDistribution.__str__cCsdg}tjj|jd}tjj|r`x>|jD]2\}}}||kr>q*tjj|s*|j|dddfq*W|S)a Checks that the hashes and sizes of the files in ``RECORD`` are matched by the files themselves. Returns a (possibly empty) list of mismatches. Each entry in the mismatch list will be a tuple consisting of the path, 'exists', 'size' or 'hash' according to what didn't match (existence is checked first, then size, then hash), the expected value and the actual value. zinstalled-files.txtrTF)rrrIrrr')r rrrrZr!r!r"rs   z)EggInfoDistribution.check_installed_filesc Csdd}dd}tjj|jd}g}tjj|rtj|ddd|}xt|D]l}|j}tjjtjj|j|}tjj|stj d ||j d rqHtjj |sH|j |||||fqHWWd QRX|j |d d f|S)z Iterates over the ``installed-files.txt`` entries and returns a tuple ``(path, hash, size)`` for each line. :returns: a list of (path, hash, size) c Ss0t|d}z |j}Wd|jXtj|jS)Nr)rrcloserrZ hexdigest)rrZcontentr!r!r"_md5s    z6EggInfoDistribution.list_installed_files.._md5cSs tj|jS)N)rstatst_size)rr!r!r"_sizesz7EggInfoDistribution.list_installed_files.._sizezinstalled-files.txtrSzutf-8)rzNon-existent file: %s.pyc.pyoN)rr) rrrIrrrrnormpathrMrrFrr')r rrrrbrrrir!r!r"rs"      &z(EggInfoDistribution.list_installed_filesFc cstjj|jd}d}tj|dddd}x\|D]T}|j}|dkrFd}q,|s,tjjtjj|j|}|j|jr,|rz|Vq,|Vq,WWdQRXdS) a  Iterates over the ``installed-files.txt`` entries and returns paths for each line if the path is pointing to a file located in the ``.egg-info`` directory or one of its subdirectories. :parameter absolute: If *absolute* is ``True``, each returned path is transformed into a local absolute path. Otherwise the raw value from ``installed-files.txt`` is returned. :type absolute: boolean :returns: iterator of paths zinstalled-files.txtTrSzutf-8)rz./FN)rrrIrrrrr)r Zabsoluterskiprrrir!r!r"rs   z'EggInfoDistribution.list_distinfo_filescCst|to|j|jkS)N)rWrr)r rr!r!r"rs zEggInfoDistribution.__eq__)N)F)r*r+r,r-rrr#rrrrrrrrrrr!r!)rr"rNsK& c@s^eZdZdZddZddZdddZd d Zd d ZdddZ dddZ ddZ ddZ dS)DependencyGrapha Represents a dependency graph between distributions. The dependency relationships are stored in an ``adjacency_list`` that maps distributions to a list of ``(other, label)`` tuples where ``other`` is a distribution and the edge is labeled with ``label`` (i.e. the version specifier, if such was provided). Also, for more efficient traversal, for every distribution ``x``, a list of predecessors is kept in ``reverse_list[x]``. An edge from distribution ``a`` to distribution ``b`` means that ``a`` depends on ``b``. If any missing dependencies are found, they are stored in ``missing``, which is a dictionary that maps distributions to a list of requirements that were not provided by any other distributions. cCsi|_i|_i|_dS)N)adjacency_list reverse_listr)r r!r!r"r#.szDependencyGraph.__init__cCsg|j|<g|j|<dS)zAdd the *distribution* to the graph. :type distribution: :class:`distutils2.database.InstalledDistribution` or :class:`distutils2.database.EggInfoDistribution` N)rr)r distributionr!r!r"add_distribution3s z DependencyGraph.add_distributionNcCs6|j|j||f||j|kr2|j|j|dS)aAdd an edge from distribution *x* to distribution *y* with the given *label*. :type x: :class:`distutils2.database.InstalledDistribution` or :class:`distutils2.database.EggInfoDistribution` :type y: :class:`distutils2.database.InstalledDistribution` or :class:`distutils2.database.EggInfoDistribution` :type label: ``str`` or ``None`` N)rr'r)r xylabelr!r!r"add_edge=s zDependencyGraph.add_edgecCs&tjd|||jj|gj|dS)a Add a missing *requirement* for the given *distribution*. :type distribution: :class:`distutils2.database.InstalledDistribution` or :class:`distutils2.database.EggInfoDistribution` :type requirement: ``str`` z %s missing %rN)rMrNrr%r')r rrr!r!r" add_missingLszDependencyGraph.add_missingcCsd|j|jfS)Nz%s %s)rr])r r(r!r!r" _repr_distWszDependencyGraph._repr_distrcCs|j|g}xv|j|D]h\}}|j|}|dk r>d||f}|jd|t||j||d}|jd}|j|ddqWdj|S)zPrints only a subgraphNz%s [%s]z rr)rrr'r repr_noderextendrI)r r(leveloutputrr Z suboutputZsubsr!r!r"rZs    zDependencyGraph.repr_nodeTcCsg}|jdx||jjD]n\}}t|dkr>| r>|j|xH|D]@\}}|dk rn|jd|j|j|fqD|jd|j|jfqDWqW| rt|dkr|jd|jd|jdx&|D]}|jd |j|jd qW|jd |jd dS) a9Writes a DOT output for the graph to the provided file *f*. If *skip_disconnected* is set to ``True``, then all distributions that are not dependent on any other distribution are skipped. :type f: has to support ``file``-like operations :type skip_disconnected: ``bool`` zdigraph dependencies { rNz"%s" -> "%s" [label="%s"] z "%s" -> "%s" zsubgraph disconnected { zlabel = "Disconnected" zbgcolor = red z"%s"rz} )rritemsrr'r)r rZskip_disconnectedZ disconnectedr(adjsrr r!r!r"to_dotgs&        zDependencyGraph.to_dotcsg}i}x&|jjD]\}}|dd||<qWxgx4t|jddD]\}}|sLj|||=qLWsrPx*|jD]\}}fdd|D||<q|WtjdddD|jq2W|t|jfS)aa Perform a topological sort of the graph. :return: A tuple, the first element of which is a topologically sorted list of distributions, and the second element of which is a list of distributions that cannot be sorted because they have circular dependencies and so form a cycle. Ncs g|]\}}|kr||fqSr!r!)rrsrS) to_remover!r"rsz4DependencyGraph.topological_sort..zMoving to result: %scSsg|]}d|j|jfqS)z%s (%s))rr])rrsr!r!r"rs)rrlistr'rMrNrkeys)r rbZalistkrtr!)rr"topological_sorts$  z DependencyGraph.topological_sortcCs6g}x&|jjD]\}}|j|j|qWdj|S)zRepresentation of the graphr)rrr'rrI)r rr(rr!r!r"rszDependencyGraph.__repr__)N)r)T) r*r+r,r-r#r r rrrrrrr!r!r!r"rs   rr.cCsft|}t}i}xX|D]P}|j|x@|jD]6}t|\}}tjd||||j|gj||fq.WqWx|D]}|j |j B|j B|j B}x|D]} y|j | } Wn6tk rtjd| | jd}|j |} YnX| j}d} ||krJxV||D]J\}} y| j|} Wntk r,d} YnX| r|j|| | d} PqW| s|j|| qWqrW|S)a6Makes a dependency graph from the given distributions. :parameter dists: a list of distributions :type dists: list of :class:`distutils2.database.InstalledDistribution` and :class:`distutils2.database.EggInfoDistribution` instances :rtype: a :class:`DependencyGraph` instance zAdd to provided: %s, %s, %sz+could not read version %r - using name onlyrFT)rrr rfrrMrNr%r'rrrrrdrrrr&rgr r)distsr=graphrhr(rirr]rrrdZmatchedZproviderrgr!r!r" make_graphsD         rcCs~||krtd|jt|}|g}|j|}x@|rn|j}|j|x$|j|D]}||krR|j|qRWq0W|jd|S)zRecursively generate a list of distributions from *dists* that are dependent on *dist*. :param dists: a list of distributions :param dist: a distribution, member of *dists* for which we are interested z1given distribution %r is not a member of the listr)rrrrpopr')rr(rZdeptodorsZsuccr!r!r"get_dependent_distss    r!cCsv||krtd|jt|}g}|j|}xD|rp|jd}|j|x$|j|D]}||krT|j|qTWq.W|S)zRecursively generate a list of distributions from *dists* that are required by *dist*. :param dists: a list of distributions :param dist: a distribution, member of *dists* for which we are interested z1given distribution %r is not a member of the listr)rrrrrr')rr(rrr rsZpredr!r!r"get_required_distss    r"cKs4|jdd}tf|}||_||_|p(d|_t|S)zO A convenience method for making a dist given just a name and version. summaryzPlaceholder for summary)rr rr]r#r)rr]kwargsr#rr!r!r" make_dists    r%)r.)6r-Z __future__rrrrJrZloggingrrHr/rr:rrcompatrr]rrr>r r r utilr r rrrrr__all__Z getLoggerr*rMrZCOMMANDS_FILENAMErrGrrrrrrrrOrPrrr!r"r%r!r!r!r"sV  $  l7GM 6