o a@sdZddlZddlZddlZddlZddlZddlmZGdddeZ Gddde Z dd Z Gd d d Z Gd d d Z Gddde ZGddde ZedeZeed<dS)a Based on "python-archive" -- https://pypi.org/project/python-archive/ Copyright (c) 2010 Gary Wilson Jr. and contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. N)SuspiciousOperationc@eZdZdZdS)ArchiveExceptionz6 Base exception class for all archive errors. N__name__ __module__ __qualname____doc__r r 6/usr/lib/python3/dist-packages/django/utils/archive.pyr!rc@r)UnrecognizedArchiveFormatzK Error raised when passed file is not a recognized archive format. Nrr r r r r 'r r cCs8t|}||WddS1swYdS)ze Unpack the tar or zip file at the specified path to the directory specified by to_path. N)Archiveextract)pathto_patharchiver r r r-s  "rc@sLeZdZdZddZeddZddZdd Zd d Z d d Z ddZ dS)rzM The external API class that encapsulates an archive implementation. cCs||||_dSN) _archive_cls_archiveselffiler r r __init__:szArchive.__init__cCsd}t|tr |}nz|j}Wn tytdwtj|\}}t |}|s9tj|\}}t |}|sAtd||S)Nz,File object not a recognized archive format.z(Path not a recognized archive format: %s) isinstancestrnameAttributeErrorr osrsplitextlower extension_mapget)rclsfilenamebasetail_extextr r r r=s(     zArchive._archive_clscCs|Srr rr r r __enter__RszArchive.__enter__cCs |dSr)close)rexc_type exc_value tracebackr r r __exit__Us zArchive.__exit__cCs|j|dSr)rr)rrr r r rXzArchive.extractcC|jdSrrlistr(r r r r2[z Archive.listcCr0rrr*r(r r r r*^r3z Archive.closeN) rrrr r staticmethodrr)r.rr2r*r r r r r6s  rc@sDeZdZdZeddZddZddZdd Zd d Z d d Z dS) BaseArchivezI Base Archive class. Implementations should inherit this class. cCs|tj@r t||dSdS)z If the file in the archive has some permissions (this assumes a file won't be writable/executable without being readable), apply those permissions to the unarchived file. N)statS_IROTHrchmod)moder$r r r _copy_permissionsfs zBaseArchive._copy_permissionscCslt|}|dd}d|vr(d|vr|d|dks"d|vr(|ddSd|vr2|ddS|dfS)N/\)rlstripfindsplit)rrr r r split_leading_dirps,  zBaseArchive.split_leading_dircCsFd}|D]}||\}}|sdS|dur|}q||kr dSqdS)z Return True if all the paths have the same leading path name (i.e., everything is in one subdirectory in an archive). NFT)rC)rpaths common_prefixrprefixrestr r r has_leading_dirzszBaseArchive.has_leading_dircCs<tj|}tjtj||}||std||S)Nz#Archive contains invalid path: '%s')rrabspathjoin startswithr)rrr target_pathr$r r r target_filenames   zBaseArchive.target_filenamecCtd)Nz:subclasses of BaseArchive must provide an extract() methodNotImplementedErrorr(r r r rzBaseArchive.extractcCrN)Nz6subclasses of BaseArchive must provide a list() methodrOr(r r r r2rQzBaseArchive.listN) rrrr r5r;rCrHrMrr2r r r r r6bs    r6c@,eZdZddZddZddZddZd S) TarArchivecCt||_dSr)tarfileopenrrr r r rr/zTarArchive.__init__cO|jj|i|dSrr1rargskwargsr r r r2zTarArchive.listc Cs<|j}|dd|D}|D]}|j}|r||d}|||}|r3|r2tj|ddqz`z|j |}Wnt t fyZ}zt d||j|fWYd}~n6d}~wwtj |} | rjtj| ddt|d} t|| ||j|Wdn1swYW|r|q|r|wwdS)Ncss|]}|jVqdSr)r).0xr r r sz%TarArchive.extract..r>Texist_okz/In the tar file %s the member %s is invalid: %swb)r getmembersrHrrCrMisdirrmakedirs extractfileKeyErrorrprintrdirnamerVshutil copyfileobjr;r:r*) rrmembersleadingmemberrr$ extractedexcrhoutfiler r r rsD       zTarArchive.extractcCr0rr4r(r r r r*r3zTarArchive.closeNrrrrr2rr*r r r r rSs  rSc@rR) ZipArchivecCrTr)zipfileZipFilerrr r r rr/zZipArchive.__init__cOrWr)rprintdirrXr r r r2r[zZipArchive.listc Cs|j}||}|D]a}|j|}|j|}|r#||d}|s&q |||}|dr9tj |ddq tj |}|rHtj |ddt |d } | |Wdn1s]wY|jd?} || |q dS)Nr>)r<r=Tr_ra)rnamelistrHreadgetinforCrMendswithrrdrrhrVwrite external_attrr;) rrrwrlrdatainfor$rhrpr:r r r rs*          zZipArchive.extractcCr0rr4r(r r r r*r3zZipArchive.closeNrqr r r r rrs  rr) z.tarz.tar.bz2z.tbz2z.tbzz.tz2z.tar.gzz.tgzz.tazz .tar.lzmaz.tlzz.tar.xzz.txzz.zip)r rrir7rUrsdjango.core.exceptionsr Exceptionrr rrr6rSrrdictfromkeysr!r r r r s$  ,6*$