a +bd@sddlmZmZddlZddlZddlZddlmZddlm Z m Z mZddlm Z ej Z e j Z dZd Zed d ZGd d d eZGdddeZeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeeGdddeZeeGd d!d!eZeeGd"d#d#eZ ee Gd$d%d%eZ!ee!d&d'Z"e"#Z$dS)()absolute_importprint_functionN)getattr)errori18npycompat) stringutilsserversclientZcompenginewireprotosupport)nameZserverpriorityZclientpriorityc@s&eZdZddZdddZddZdS) propertycachecCs||_|j|_dSN)func__name__r )selfrrA/usr/lib64/python3.9/site-packages/mercurial/utils/compression.py__init__&szpropertycache.__init__NcCs||}||||Sr )r cachevalue)robjtyperesultrrr__get__*s  zpropertycache.__get__cCs||j|j<dSr )__dict__r )rrvaluerrrr/szpropertycache.cachevalue)N)r __module__ __qualname__rrrrrrrr %s r c@szeZdZdZddZddZddZdd Zd d Ze d d Z e ddZ ddZ ddZ dddZddZddZdS)compressormanageraGHolds registrations of various compression engines. This class essentially abstracts the differences between compression engines to allow new compression formats to be added easily, possibly from extensions. Compressors are registered against the global instance by calling its ``register()`` method. cCs"i|_i|_i|_i|_i|_dSr )_engines _bundlenames _bundletypes_revlogheaders _wiretypesrrrrr?s zcompressormanager.__init__cCs |j|Sr rrkeyrrr __getitem__Jszcompressormanager.__getitem__cCs ||jvSr r$r%rrr __contains__Mszcompressormanager.__contains__cCst|jSr )iterrkeysr#rrr__iter__Pszcompressormanager.__iter__c Cs>t|tsttd|}||jvr:ttd||}|r|\}}||j vrjttd|||j vrttd||j |f|r||j |<||j |<| }|r|j}||j vrttd||j |f||j |<| }|r ||jvr ttd||j|f|r0||j|<||j|<dS)zxRegister a compression engine with the manager. The argument must be a ``compressionengine`` instance. s$argument must be a compressionengines(compression engine %s already registereds!bundle name %s already registereds'bundle type %s already registered by %ss5wire protocol compression %s already registered by %ss)revlog header %s already registered by %sN) isinstancecompressionengine ValueError_r rrAbort bundletyperr wireprotosupportr" revlogheaderr!) renginer Z bundleinfo bundlenamer1Z wiresupportwiretyper3rrrregisterSsZ               zcompressormanager.registercCst|jSr )setrr*r#rrrsupportedbundlenamessz&compressormanager.supportedbundlenamescCst|jSr )r8r r*r#rrrsupportedbundletypessz&compressormanager.supportedbundletypescCs2|j|j|}|s.ttd||S)zObtain a compression engine registered to a bundle name. Will raise KeyError if the bundle type isn't registered. Will abort if the engine is known but not available. )compression engine %s could not be loaded)rr availablerr0r/r )rr5r4rrr forbundlenames zcompressormanager.forbundlenamecCs2|j|j|}|s.ttd||S)zObtain a compression engine registered to a bundle type. Will raise KeyError if the bundle type isn't registered. Will abort if the engine is known but not available. r;)rr r<rr0r/r )rr1r4rrr forbundletypes zcompressormanager.forbundletypeTcsV|tkr dndfddjD}|r:dd|D}fdd}tt||dS) zObtain compression engines that support the wire protocol. Returns a list of engines in prioritized order, most desired first. If ``onlyavailable`` is set, filter out engines that can't be loaded. sserverprioritysclientprioritycsg|]}j|qSrr$.0er#rr z:compressormanager.supportedwireengines..cSsg|]}|r|qSr)r<r?rrrrBrCcs|}dt||jfS)N)r2rr )rAw)attrrrgetkeysz6compressormanager.supportedwireengines..getkey)r&) SERVERROLEr"valueslistsorted)rZroleZ onlyavailableZenginesrGr)rFrrsupportedwireenginess  z&compressormanager.supportedwireenginescCs2|j|j|}|s.ttd||S)Nr;)rr"r<rr0r/r )rr6r4rrr forwiretypes zcompressormanager.forwiretypecCs|j|j|S)zObtain a compression engine registered to a revlog header. Will raise KeyError if the revlog header value isn't registered. )rr!)rheaderrrrforrevlogheadersz!compressormanager.forrevlogheaderN)T)rrr__doc__rr'r(r+r7propertyr9r:r=r>rLrMrOrrrrr4s  =   rc@sTeZdZdZddZddZddZdd Zd d Zdd dZ ddZ dddZ d S)r-zuBase class for compression engines. Compression engines must implement the interface defined by this class. cCs tdS)zReturns the name of the compression engine. This is the key the engine is registered under. This method must be implemented. NNotImplementedErrorr#rrrr szcompressionengine.namecCsdS)aWhether the compression engine is available. The intent of this method is to allow optional compression engines that may not be available in all installations (such as engines relying on C extensions that may not be present). Trr#rrrr<szcompressionengine.availablecCsdS)aDescribes bundle identifiers for this engine. If this compression engine isn't supported for bundles, returns None. If this engine can be used for bundles, returns a 2-tuple of strings of the user-facing "bundle spec" compression name and an internal identifier used to denote the compression format within bundles. To exclude the name from external usage, set the first element to ``None``. If bundle compression is supported, the class must also implement ``compressstream`` and `decompressorreader``. The docstring of this method is used in the help system to tell users about this engine. Nrr#rrrr1szcompressionengine.bundletypecCsdS)aDeclare support for this compression format on the wire protocol. If this compression engine isn't supported for compressing wire protocol payloads, returns None. Otherwise, returns ``compenginewireprotosupport`` with the following fields: * String format identifier * Integer priority for the server * Integer priority for the client The integer priorities are used to order the advertisement of format support by server and client. The highest integer is advertised first. Integers with non-positive values aren't advertised. The priority values are somewhat arbitrary and only used for default ordering. The relative order can be changed via config options. If wire protocol compression is supported, the class must also implement ``compressstream`` and ``decompressorreader``. Nrr#rrrr2 sz"compressionengine.wireprotosupportcCsdS)aPHeader added to revlog chunks that identifies this engine. If this engine can be used to compress revlogs, this method should return the bytes used to identify chunks compressed with this engine. Else, the method should return ``None`` to indicate it does not participate in revlog compression. Nrr#rrrr3#szcompressionengine.revlogheaderNcCs tdS)aCompress an iterator of chunks. The method receives an iterator (ideally a generator) of chunks of bytes to be compressed. It returns an iterator (ideally a generator) of bytes of chunks representing the compressed output. Optionally accepts an argument defining how to perform compression. Each engine treats this argument differently. NrRritoptsrrrcompressstream-s z compressionengine.compressstreamcCs tdS)zPerform decompression on a file object. Argument is an object with a ``read(size)`` method that returns compressed data. Return value is an object with a ``read(size)`` that returns uncompressed data. NrRrfhrrrdecompressorreader9sz$compressionengine.decompressorreadercCs tdS)aLObtain an object that can be used to compress revlog entries. The object has a ``compress(data)`` method that compresses binary data. This method returns compressed binary data or ``None`` if the data could not be compressed (too small, not compressible, etc). The returned data should have a header uniquely identifying this compression format so decompression can be routed to this engine. This header should be identified by the ``revlogheader()`` return value. The object has a ``decompress(data)`` method that decompresses data. The method will only be called if ``data`` begins with ``revlogheader()``. The method should return the raw, uncompressed data or raise a ``StorageError``. The object is reusable but is not thread safe. NrRrrVrrrrevlogcompressorBsz"compressionengine.revlogcompressor)N)N) rrrrPr r<r1r2r3rWrZr\rrrrr-s    r-c@s$eZdZddZddZddZdS)_CompressedStreamReadercCs2t|dr|j|_n|j|_g|_d|_d|_dS)NunbufferedreadrF) safehasattrr^_readerread_pending_pos_eofrXrrrrXs   z _CompressedStreamReader.__init__cCs tdSr rR)rchunkrrr _decompressasz#_CompressedStreamReader._decompresscCsg}|jrt|jd||jkr^|jd}|||j|j||j|7_d|S|jd}|jr|||jd|t||j8}n|||t|8}d|_q|jrd|S|d}|||s|js|jsd|SqdS)NrrCi) rblenrcappendjoinpoprdr`rf)rlbufnewbufrerrrrads(        z_CompressedStreamReader.readN)rrrrrfrarrrrr]Ws r]cs$eZdZfddZddZZS)_GzipCompressedStreamReadercstt||t|_dSr )superrnrzlib decompressobj _decompobjrX __class__rrrsz$_GzipCompressedStreamReader.__init__cCsf|j|}|r|j||j}z&|d||jdkrJd|_Wntj y`Yn0dS)NxT) rr decompressrbrhcopyflush unused_datardrpr)rrermdrrrrfs      z'_GzipCompressedStreamReader._decompressrrrrrf __classcell__rrrsrrns rncs$eZdZfddZddZZS)_BZ2CompressedStreamReadercstt||t|_dSr )ror}rbz2BZ2DecompressorrrrXrsrrrsz#_BZ2CompressedStreamReader.__init__cCs`|j|}|r|j|z&|jd}|r@|j|qq@qWntyZd|_Yn0dSNrCT)rrrvrbrhEOFErrorrdrrermrrrrfs    z&_BZ2CompressedStreamReader._decompressr{rrrsrr}s r}cseZdZfddZZS)#_TruncatedBZ2CompressedStreamReadercs0tt|||jd}|r,|j|dS)NBZ)rorrrrrvrbrh)rrYrmrsrrrs z,_TruncatedBZ2CompressedStreamReader.__init__)rrrrr|rrrsrrsrcs$eZdZfddZddZZS)_ZstdCompressedStreamReadercs(tt||||_||_dSr )rorr_zstdZstdDecompressorrqrr)rrYzstdrsrrrsz$_ZstdCompressedStreamReader.__init__cCsd|j|}|r|j|z&|jd}|r@|j|qq@qWn|jjy^d|_Yn0dSr)rrrvrbrhrZ ZstdErrorrdrrrrrfs   z'_ZstdCompressedStreamReader._decompressr{rrrsrrs rc@sXeZdZddZddZddZddZdd d Zd d ZGddde Z dddZ d S) _zlibenginecCsdS)Nzlibrr#rrrr sz_zlibengine.namecCsdS)zzlib compression using the DEFLATE algorithm. All Mercurial clients should support this format. The compression algorithm strikes a reasonable balance between compression ratio and size. )sgzipsGZrr#rrrr1sz_zlibengine.bundletypecCs tdddS)Nrcompewireprotosupportr#rrrr2sz_zlibengine.wireprotosupportcCsdS)Nrurr#rrrr3sz_zlibengine.revlogheaderNccsF|pi}t|dd}|D]}||}|r|Vq|VdS)NlevelrD)rp compressobjgetcompressrxrrUrVzredatarrrrWs z_zlibengine.compressstreamcCst|Sr )rnrXrrrrZsz_zlibengine.decompressorreaderc@s&eZdZdddZddZddZdS) z _zlibengine.zlibrevlogcompressorNcCs ||_dSr )_level)rlevelrrrrsz)_zlibengine.zlibrevlogcompressor.__init__cCst|}|dkrdS|dkrT|jdur2t|}nt||j}t||krP|SdS|jdurht}ntj|jd}g}d}||kr|d}||||||}q~||ttt||krd |SdSdS)N,@BrrirC) rgrrprrrhrxsummapri)rrinsize compressedrpartspospos2rrrrs.      z)_zlibengine.zlibrevlogcompressor.compressc CsPz t|WStjyJ}z$ttdt|WYd}~n d}~00dS)Nrevlog decompress error: %s)rprvr StorageErrorr/r forcebytestr)rrrArrrrvs z+_zlibengine.zlibrevlogcompressor.decompress)Nrrrrrrvrrrrzlibrevlogcompressors %rcCs d}|dur|d}||S)Ns zlib.level)rrrrVrrrrr\s z_zlibengine.revlogcompressor)N)N) rrrr r1r2r3rWrZobjectrr\rrrrrs  2rc@s6eZdZddZddZddZd dd Zd d ZdS) _bz2enginecCsdS)Nsbz2rr#rrrr $sz_bz2engine.namecCsdS)aAn algorithm that produces smaller bundles than ``gzip``. All Mercurial clients should support this format. This engine will likely produce smaller bundles than ``gzip`` but will be significantly slower, both during compression and decompression. If available, the ``zstd`` engine can yield similar or better compression at much higher speeds. )bzip2rrr#rrrr1's z_bz2engine.bundletypecCs tdddS)Nrrrr#rrrr27sz_bz2engine.wireprotosupportNccsF|pi}t|dd}|D]}||}|r|Vq|VdS)Nr )r~ BZ2CompressorrrrxrrrrrW:s z_bz2engine.compressstreamcCst|Sr )r}rXrrrrZDsz_bz2engine.decompressorreader)N)rrrr r1r2rWrZrrrrr#s  rc@s$eZdZddZddZddZdS)_truncatedbz2enginecCsdS)Ns bz2truncatedrr#rrrr Lsz_truncatedbz2engine.namecCsdS)N)Ns _truncatedBZrr#rrrr1Osz_truncatedbz2engine.bundletypecCst|Sr )rrXrrrrZTsz&_truncatedbz2engine.decompressorreaderN)rrrr r1rZrrrrrKsrc@sXeZdZddZddZddZddZdd d Zd d ZGddde Z dddZ d S) _noopenginecCsdS)Nnonerr#rrrr \sz_noopengine.namecCsdS)zmNo compression is performed. Use this compression engine to explicitly disable compression. )rsUNrr#rrrr1_sz_noopengine.bundletypecCs tdddS)Nrr rr#rrrr2isz_noopengine.wireprotosupportcCsdS)Nrr#rrrr3nsz_noopengine.revlogheaderNcCs|Sr rrTrrrrWqsz_noopengine.compressstreamcCs|Sr rrXrrrrZtsz_noopengine.decompressorreaderc@seZdZddZdS)z _noopengine.nooprevlogcompressorcCsdSr r)rrrrrrxsz)_noopengine.nooprevlogcompressor.compressN)rrrrrrrrnooprevlogcompressorwsrcCs|Sr )rr[rrrr\{sz_noopengine.revlogcompressor)N)N) rrrr r1r2r3rWrZrrr\rrrrr[s  rc@sleZdZddZeddZddZddZd d Zd d Z dddZ ddZ Gddde Z dddZd S) _zstdenginecCsdS)Nzstdrr#rrrr sz_zstdengine.namecCs2zddlm}|j|WSty,YdS0dS)Nr)r)r __version__ ImportError)rrrrr_modules   z_zstdengine._modulecCs t|jSr )boolrr#rrrr<sz_zstdengine.availablecCsdS)aA modern compression algorithm that is fast and highly flexible. Only supported by Mercurial 4.1 and newer clients. With the default settings, zstd compression is both faster and yields better compression than ``gzip``. It also frequently yields better compression than ``bzip2`` while operating at much higher speeds. If this engine is available and backwards compatibility is not a concern, it is likely the best available engine. )rsZSrr#rrrr1s z_zstdengine.bundletypecCs tdddS)Nr2rr#rrrr2sz_zstdengine.wireprotosupportcCsdS)N(rr#rrrr3sz_zstdengine.revlogheaderNc csd|pi}|dd}|dd}|j}|j||d}|D]}||}|r<|Vq<|VdS)Nrsthreadsr)rthreads)rrZstdCompressorrrrx) rrUrVrrrrrerrrrrWs   z_zstdengine.compressstreamcCs t||jSr )rrrXrrrrZsz_zstdengine.decompressorreaderc@s&eZdZd ddZddZddZdS) z _zstdengine.zstdrevlogcompressorrcCs,|j|d|_||_|j|_|j|_dS)Nr)r_cctxr_dctxZ"COMPRESSION_RECOMMENDED_INPUT_SIZE _compinsizeZ$DECOMPRESSION_RECOMMENDED_INPUT_SIZE _decompinsize)rrrrrrrs z)_zstdengine.zstdrevlogcompressor.__init__c Cst|}|dkrdS|dkr<|j|}t||kr8|SdS|j}g}d}||kr||j}||||}|r|||}qN||ttt||krd |SdSdS)NrrrrC) rgrrrrrhrxrrri) rrrrrchunksrrrerrrrs*      z)_zstdengine.zstdrevlogcompressor.compressc Cst|}zV|j}g}d}||krT||j}||||}|rN|||}qd|WSty}z$t t dt |WYd}~n d}~00dS)NrrCr) rgrrqrrvrhri Exceptionrrr/r r) rrrZdobjrrrrerArrrrvs$    z+_zstdengine.zstdrevlogcompressor.decompressN)rrrrrrzstdrevlogcompressors rcCs@|pi}|d}|dur$|d}|dur0d}|j|j|dS)Ns zstd.levelrrr)rrrrrrrr\s  z_zstdengine.revlogcompressor)N)N)rrrr r rr<r1r2r3rWrZrrr\rrrrrs  @rcCsi}Gdddt}tD]t}t|}|s.q|}|r|dsDqd|dt|jf}|}t||_|jj|_|j|_ |||d<q|S)z@Obtains a list of available bundle compressions for use in help.c@s eZdZdS)z*bundlecompressiontopics..docobjectN)rrrrrrr docobjectsrrs ``%s`` %s) r compenginesr<r1rZgetdocZsysstrrPZ_origdocZ _origfunc)itemsrr r4Zbtdocrrrrbundlecompressiontopics s    r)%Z __future__rrr~ collectionsrprrrrrr r_r/rHZ CLIENTROLE namedtuplerrr rrr-r]rnr}rrrr7rrrrrrIZ i18nfunctionsrrrrsJ  *w)\ %  $