o ]Lbd@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) stringutilsserversclientcompenginewireprotosupport)nameserverpriorityclientpriorityc@s&eZdZddZdddZddZdS) propertycachecCs||_|j|_dSN)func__name__r )selfrr=/usr/lib/python3/dist-packages/mercurial/utils/compression.py__init__&s zpropertycache.__init__NcCs||}||||Sr)r cachevalue)robjtyperesultrrr__get__*s  zpropertycache.__get__cCs||j|j<dSr)__dict__r )rrvaluerrrr/szpropertycache.cachevaluer)r __module__ __qualname__rrrrrrrr%s  rc@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|Srr!rkeyrrr __getitem__J zcompressormanager.__getitem__cCs ||jvSrr'r(rrr __contains__Mr+zcompressormanager.__contains__cCt|jSr)iterr!keysr&rrr__iter__Pszcompressormanager.__iter__c Cs8t|ts ttd|}||jvrttd||}|rT|\}}||j vr5ttd|||j vrHttd||j |f|rO||j |<||j |<| }|ru|j}||j vrpttd||j |f||j |<| }|r||jvrttd||j|f|r||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 r!rAbort bundletyper"r#wireprotosupportr% revlogheaderr$) renginer bundleinfo bundlenamer6 wiresupportwiretyper8rrrregisterSsZ               zcompressormanager.registercCr-r)setr"r/r&rrrsupportedbundlenamesz&compressormanager.supportedbundlenamescCr-r)r?r#r/r&rrrsupportedbundletypesrAz&compressormanager.supportedbundletypescC2|j|j|}|sttd||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)r!r" availablerr5r4r )rr;r9rrr forbundlename zcompressormanager.forbundlenamecCrC)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. rD)r!r#rErr5r4r )rr6r9rrr forbundletyperGzcompressormanager.forbundletypeTcsf|ttfvsJ|tkrdndfddjD}|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 sz:compressormanager.supportedwireengines..cSsg|]}|r|qSr)rErIrrrrLscs|}dt||jfS)N)r7rr )rKw)attrrrgetkeysz6compressormanager.supportedwireengines..getkey)r)) SERVERROLE CLIENTROLEr%valueslistsorted)rrole onlyavailableenginesrPr)rOrrsupportedwireenginess z&compressormanager.supportedwireenginescCrC)NrD)r!r%rErr5r4r )rr=r9rrr 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. )r!r$)rheaderrrrforrevlogheadersz!compressormanager.forrevlogheaderN)T)rrr__doc__rr*r,r0r>propertyr@rBrFrHrYrZr\rrrrr 4s   =    r c@sTeZdZdZddZddZddZdd Zd d Zdd dZ ddZ dddZ d S)r2zuBase class for compression engines. Compression engines must implement the interface defined by this class. cCt)zReturns the name of the compression engine. This is the key the engine is registered under. This method must be implemented. NotImplementedErrorr&rrrr zcompressionengine.namecCdS)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&rrrrEzcompressionengine.availablecCrc)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&rrrr6szcompressionengine.bundletypecCrc)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&rrrr7 sz"compressionengine.wireprotosupportcCrc)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&rrrr8#szcompressionengine.revlogheaderNcCr_)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. r`ritoptsrrrcompressstream-s z compressionengine.compressstreamcCr_)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. r`rfhrrrdecompressorreader9rbz$compressionengine.decompressorreadercCr_)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. r`rrgrrrrevlogcompressorBsz"compressionengine.revlogcompressorr) rrrr]r rEr6r7r8rhrkrmrrrrr2s     r2c@$eZdZddZddZddZdS)_CompressedStreamReadercCs2t|dr |j|_n|j|_g|_d|_d|_dS)NunbufferedreadrF) safehasattrrp_readerread_pending_pos_eofrirrrrXs   z _CompressedStreamReader.__init__cCr_rr`)rchunkrrr _decompressasz#_CompressedStreamReader._decompresscCsg} |jr^t|jd||jkr0|jd}|||j|j||j|7_d|S|jd}|jrM|||jd|t||j8}n |||t|8}d|_|js|jrfd|S|d}|||s}|js}|js}d|Sq)NTri) rtlenruappendjoinpoprvrrrx)rlbufnewbufrwrrrrsds.         z_CompressedStreamReader.readN)rrrrrxrsrrrrroWs roc$eZdZfddZddZZS)_GzipCompressedStreamReaderctt||t|_dSr)superrrzlib decompressobj _decompobjri __class__rrrz$_GzipCompressedStreamReader.__init__cCsl|j|}|r|j||j}z|d||jdkr(d|_WdSWdStj y5YdSw)NxT) r decompressrtr{copyflush unused_datarvrr)rrwrdrrrrxs      z'_GzipCompressedStreamReader._decompressrrrrrx __classcell__rrrrr rcr)_BZ2CompressedStreamReadercrr)rrrbz2BZ2Decompressorrrirrrrrz#_BZ2CompressedStreamReader.__init__cCs`|j|}|r|j|z |jd}|r|j|nWdSqty/d|_YdSwNTry)rrrtr{EOFErrorrvrrwrrrrrxs     z&_BZ2CompressedStreamReader._decompressrrrrrrrrcseZdZfddZZS)#_TruncatedBZ2CompressedStreamReadercs4tt|||jd}|r|j|dSdS)NBZ)rrrrrrtr{)rrjrrrrrs  z,_TruncatedBZ2CompressedStreamReader.__init__)rrrrrrrrrrsrcr)_ZstdCompressedStreamReadercs(tt||||_||_dSr)rrr_zstdZstdDecompressorrr)rrjzstdrrrrsz$_ZstdCompressedStreamReader.__init__cCsd|j|}|r|j|z |jd}|r|j|nWdSq|jjy1d|_YdSwr)rrrtr{r ZstdErrorrvrrrrrxs    z'_ZstdCompressedStreamReader._decompressrrrrrrs rc@XeZdZddZddZddZddZdd d Zd d ZGddde Z dddZ d S) _zlibenginecCrc)Nzlibrr&rrrr z_zlibengine.namecCrc)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&rrrr6rdz_zlibengine.bundletypecC tdddS)Nrcompewireprotosupportr&rrrr7 z_zlibengine.wireprotosupportcCrc)Nrrr&rrrr8rz_zlibengine.revlogheaderNccH|pi}t|dd}|D] }||}|r|Vq|VdS)NlevelrM)r compressobjgetcompressrrrfrgzrwdatarrrrhs z_zlibengine.compressstreamcCt|Sr)rrirrrrkz_zlibengine.decompressorreaderc@s&eZdZdddZddZddZdS) z _zlibengine.zlibrevlogcompressorNcCs ||_dSr)_level)rlevelrrrrr+z)_zlibengine.zlibrevlogcompressor.__init__cCst|}|dks J|dkrdS|dkr0|jdurt|}nt||j}t||kr.|SdS|jdur:t}ntj|jd}g}d}||kr_|d}||||||}||ksI||ttt||krtd |SdS)Nr,@Briry) rzrrrrr{rsummapr|)rrinsize compressedrpartspospos2rrrrs2       z)_zlibengine.zlibrevlogcompressor.compressc Cs@zt|WStjy}z ttdt|d}~ww)Nrevlog decompress error: %s)rrr StorageErrorr4r forcebytestr)rrrKrrrrs z+_zlibengine.zlibrevlogcompressor.decompressrrrrrrrrrrrzlibrevlogcompressors  %rcCs d}|dur |d}||S)Ns zlib.level)rrrrgrrrrrms  z_zlibengine.revlogcompressorr) rrrr r6r7r8rhrkobjectrrmrrrrrs   2rc@s6eZdZddZddZddZd dd Zd d ZdS) _bz2enginecCrc)Nsbz2rr&rrrr $rz_bz2engine.namecCrc)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&rrrr6' z_bz2engine.bundletypecCr)Nrrrr&rrrr77rz_bz2engine.wireprotosupportNccr)Nr )r BZ2Compressorrrrrrrrrh:s z_bz2engine.compressstreamcCrr)rrirrrrkDrz_bz2engine.decompressorreaderr)rrrr r6r7rhrkrrrrr#s   rc@rn)_truncatedbz2enginecCrc)Ns bz2truncatedrr&rrrr Lrz_truncatedbz2engine.namecCrc)N)Ns _truncatedBZrr&rrrr6Orz_truncatedbz2engine.bundletypecCrr)rrirrrrkTrz&_truncatedbz2engine.decompressorreaderN)rrrr r6rkrrrrrKs rc@r) _noopenginecCrc)Nnonerr&rrrr \rz_noopengine.namecCrc)zmNo compression is performed. Use this compression engine to explicitly disable compression. )rsUNrr&rrrr6_sz_noopengine.bundletypecCs tdddS)Nrr rr&rrrr7irz_noopengine.wireprotosupportcCrc)Nrr&rrrr8nrz_noopengine.revlogheaderNcC|Srrrerrrrhqrz_noopengine.compressstreamcCrrrrirrrrktrz_noopengine.decompressorreaderc@seZdZddZdS)z _noopengine.nooprevlogcompressorcCsdSrr)rrrrrrxrz)_noopengine.nooprevlogcompressor.compressN)rrrrrrrrnooprevlogcompressorws rcCs|Sr)rrlrrrrm{rz_noopengine.revlogcompressorr) rrrr r6r7r8rhrkrrrmrrrrr[s  rc@sleZdZddZeddZddZddZd d Zd d Z dddZ ddZ Gddde Z dddZd S) _zstdenginecCrc)Nzstdrr&rrrr rz_zstdengine.namecCs.z ddlm}|j|WStyYdSw)Nr)r)r __version__ ImportError)rrrrr_modules  z_zstdengine._modulecCs t|jSr)boolrr&rrrrEr+z_zstdengine.availablecCrc)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&rrrr6rz_zstdengine.bundletypecCr)Nr2rr&rrrr7rz_zstdengine.wireprotosupportcCrc)N(rr&rrrr8rz_zstdengine.revlogheaderNc csf|pi}|dd}|dd}|j}|j||d}|D] }||}|r+|Vq|VdS)Nrsthreadsr)rthreads)rrZstdCompressorrrr) rrfrgrrrrrwrrrrrhs   z_zstdengine.compressstreamcCs t||jSr)rrrirrrrkrz_zstdengine.decompressorreaderc@s&eZdZd ddZddZddZdS) z _zstdengine.zstdrevlogcompressorrcCs,|j|d|_||_|j|_|j|_dS)Nr)r_cctxr_dctx"COMPRESSION_RECOMMENDED_INPUT_SIZE _compinsize$DECOMPRESSION_RECOMMENDED_INPUT_SIZE _decompinsize)rrrrrrrs  z)_zstdengine.zstdrevlogcompressor.__init__c Cst|}|dks J|dkrdS|dkr$|j|}t||kr"|SdS|j}g}d}||krL||j}||||}|rF|||}||ks1||ttt||krad |SdS)Nrrrry) rzrrrrr{rrrr|) rrrrrchunksrrrwrrrrs.       z)_zstdengine.zstdrevlogcompressor.compressc Cst|}z.|j}g}d}||kr-||j}||||}|r'|||}||ksd|WStyJ}z t t dt |d}~ww)Nrryr) rzrrrrr{r| Exceptionrrr4r r) rrrdobjrrrrwrKrrrrs*    z+_zstdengine.zstdrevlogcompressor.decompressN)rrrrrrzstdrevlogcompressors  rcCs@|pi}|d}|dur|d}|durd}|j|j|dS)Ns zstd.levelrrr)rrrrrrrrms  z_zstdengine.revlogcompressorr)rrrr rrrEr6r7r8rhrkrrrmrrrrrs   @rcCsi}Gdddt}tD]:}t|}|sq |}|r!|ds"q d|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 compenginesrEr6rgetdocsysstrr]_origdoc _origfunc)itemsrr r9btdocrrrrbundlecompressiontopics s    r)% __future__rrr collectionsrrrrrrr rqr4rQrR namedtuplerrrr rr2rorrrrrr>rrrrrrS i18nfunctionsrrrrsL  *w) \ %  $