| 1 |
""" |
| 2 |
Based on "python-archive" -- https://pypi.org/project/python-archive/ |
| 3 |
|
| 4 |
Copyright (c) 2010 Gary Wilson Jr. <[email protected]> and contributors. |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
of this software and associated documentation files (the "Software"), to deal |
| 8 |
in the Software without restriction, including without limitation the rights |
| 9 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
copies of the Software, and to permit persons to whom the Software is |
| 11 |
furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be included in |
| 14 |
all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
THE SOFTWARE. |
| 23 |
""" |
| 24 |
|
| 25 |
import os |
| 26 |
import shutil |
| 27 |
import stat |
| 28 |
import tarfile |
| 29 |
import zipfile |
| 30 |
|
| 31 |
from django.core.exceptions import SuspiciousOperation |
| 32 |
|
| 33 |
|
| 34 |
class ArchiveException(Exception): |
| 35 |
""" |
| 36 |
Base exception class for all archive errors. |
| 37 |
""" |
| 38 |
|
| 39 |
|
| 40 |
class UnrecognizedArchiveFormat(ArchiveException): |
| 41 |
""" |
| 42 |
Error raised when passed file is not a recognized archive format. |
| 43 |
""" |
| 44 |
|
| 45 |
|
| 46 |
def extract(path, to_path): |
| 47 |
""" |
| 48 |
Unpack the tar or zip file at the specified path to the directory |
| 49 |
specified by to_path. |
| 50 |
""" |
| 51 |
with Archive(path) as archive: |
| 52 |
archive.extract(to_path) |
| 53 |
|
| 54 |
|
| 55 |
class Archive: |
| 56 |
""" |
| 57 |
The external API class that encapsulates an archive implementation. |
| 58 |
""" |
| 59 |
|
| 60 |
def __init__(self, file): |
| 61 |
self._archive = self._archive_cls(file)(file) |
| 62 |
|
| 63 |
@staticmethod |
| 64 |
def _archive_cls(file): |
| 65 |
cls = None |
| 66 |
if isinstance(file, str): |
| 67 |
filename = file |
| 68 |
else: |
| 69 |
try: |
| 70 |
filename = file.name |
| 71 |
except AttributeError: |
| 72 |
raise UnrecognizedArchiveFormat( |
| 73 |
"File object not a recognized archive format." |
| 74 |
) |
| 75 |
base, tail_ext = os.path.splitext(filename.lower()) |
| 76 |
cls = extension_map.get(tail_ext) |
| 77 |
if not cls: |
| 78 |
base, ext = os.path.splitext(base) |
| 79 |
cls = extension_map.get(ext) |
| 80 |
if not cls: |
| 81 |
raise UnrecognizedArchiveFormat( |
| 82 |
"Path not a recognized archive format: %s" % filename |
| 83 |
) |
| 84 |
return cls |
| 85 |
|
| 86 |
def __enter__(self): |
| 87 |
return self |
| 88 |
|
| 89 |
def __exit__(self, exc_type, exc_value, traceback): |
| 90 |
self.close() |
| 91 |
|
| 92 |
def extract(self, to_path): |
| 93 |
self._archive.extract(to_path) |
| 94 |
|
| 95 |
def list(self): |
| 96 |
self._archive.list() |
| 97 |
|
| 98 |
def close(self): |
| 99 |
self._archive.close() |
| 100 |
|
| 101 |
|
| 102 |
class BaseArchive: |
| 103 |
""" |
| 104 |
Base Archive class. Implementations should inherit this class. |
| 105 |
""" |
| 106 |
|
| 107 |
@staticmethod |
| 108 |
def _copy_permissions(mode, filename): |
| 109 |
""" |
| 110 |
If the file in the archive has some permissions (this assumes a file |
| 111 |
won't be writable/executable without being readable), apply those |
| 112 |
permissions to the unarchived file. |
| 113 |
""" |
| 114 |
if mode & stat.S_IROTH: |
| 115 |
os.chmod(filename, mode) |
| 116 |
|
| 117 |
def split_leading_dir(self, path): |
| 118 |
path = str(path) |
| 119 |
path = path.lstrip("/").lstrip("\\") |
| 120 |
if "/" in path and ( |
| 121 |
("\\" in path and path.find("/") < path.find("\\")) or "\\" not in path |
| 122 |
): |
| 123 |
return path.split("/", 1) |
| 124 |
elif "\\" in path: |
| 125 |
return path.split("\\", 1) |
| 126 |
else: |
| 127 |
return path, "" |
| 128 |
|
| 129 |
def has_leading_dir(self, paths): |
| 130 |
""" |
| 131 |
Return True if all the paths have the same leading path name |
| 132 |
(i.e., everything is in one subdirectory in an archive). |
| 133 |
""" |
| 134 |
common_prefix = None |
| 135 |
for path in paths: |
| 136 |
prefix, rest = self.split_leading_dir(path) |
| 137 |
if not prefix: |
| 138 |
return False |
| 139 |
elif common_prefix is None: |
| 140 |
common_prefix = prefix |
| 141 |
elif prefix != common_prefix: |
| 142 |
return False |
| 143 |
return True |
| 144 |
|
| 145 |
def target_filename(self, to_path, name): |
| 146 |
target_path = os.path.abspath(to_path) |
| 147 |
filename = os.path.abspath(os.path.join(target_path, name)) |
| 148 |
try: |
| 149 |
if os.path.commonpath([target_path, filename]) != target_path: |
| 150 |
raise SuspiciousOperation("Archive contains invalid path: '%s'" % name) |
| 151 |
except ValueError: |
| 152 |
# Different drives on Windows raises ValueError. |
| 153 |
raise SuspiciousOperation("Archive contains invalid path: '%s'" % name) |
| 154 |
return filename |
| 155 |
|
| 156 |
def extract(self): |
| 157 |
raise NotImplementedError( |
| 158 |
"subclasses of BaseArchive must provide an extract() method" |
| 159 |
) |
| 160 |
|
| 161 |
def list(self): |
| 162 |
raise NotImplementedError( |
| 163 |
"subclasses of BaseArchive must provide a list() method" |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
class TarArchive(BaseArchive): |
| 168 |
def __init__(self, file): |
| 169 |
self._archive = tarfile.open(file) |
| 170 |
|
| 171 |
def list(self, *args, **kwargs): |
| 172 |
self._archive.list(*args, **kwargs) |
| 173 |
|
| 174 |
def extract(self, to_path): |
| 175 |
members = self._archive.getmembers() |
| 176 |
leading = self.has_leading_dir(x.name for x in members) |
| 177 |
for member in members: |
| 178 |
name = member.name |
| 179 |
if leading: |
| 180 |
name = self.split_leading_dir(name)[1] |
| 181 |
filename = self.target_filename(to_path, name) |
| 182 |
if member.isdir(): |
| 183 |
if filename: |
| 184 |
os.makedirs(filename, exist_ok=True) |
| 185 |
else: |
| 186 |
try: |
| 187 |
extracted = self._archive.extractfile(member) |
| 188 |
except (KeyError, AttributeError) as exc: |
| 189 |
# Some corrupt tar files seem to produce this |
| 190 |
# (specifically bad symlinks) |
| 191 |
print( |
| 192 |
"In the tar file %s the member %s is invalid: %s" |
| 193 |
% (name, member.name, exc) |
| 194 |
) |
| 195 |
else: |
| 196 |
dirname = os.path.dirname(filename) |
| 197 |
if dirname: |
| 198 |
os.makedirs(dirname, exist_ok=True) |
| 199 |
with open(filename, "wb") as outfile: |
| 200 |
shutil.copyfileobj(extracted, outfile) |
| 201 |
self._copy_permissions(member.mode, filename) |
| 202 |
finally: |
| 203 |
if extracted: |
| 204 |
extracted.close() |
| 205 |
|
| 206 |
def close(self): |
| 207 |
self._archive.close() |
| 208 |
|
| 209 |
|
| 210 |
class ZipArchive(BaseArchive): |
| 211 |
def __init__(self, file): |
| 212 |
self._archive = zipfile.ZipFile(file) |
| 213 |
|
| 214 |
def list(self, *args, **kwargs): |
| 215 |
self._archive.printdir(*args, **kwargs) |
| 216 |
|
| 217 |
def extract(self, to_path): |
| 218 |
namelist = self._archive.namelist() |
| 219 |
leading = self.has_leading_dir(namelist) |
| 220 |
for name in namelist: |
| 221 |
data = self._archive.read(name) |
| 222 |
info = self._archive.getinfo(name) |
| 223 |
if leading: |
| 224 |
name = self.split_leading_dir(name)[1] |
| 225 |
if not name: |
| 226 |
continue |
| 227 |
filename = self.target_filename(to_path, name) |
| 228 |
if name.endswith(("/", "\\")): |
| 229 |
# A directory |
| 230 |
os.makedirs(filename, exist_ok=True) |
| 231 |
else: |
| 232 |
dirname = os.path.dirname(filename) |
| 233 |
if dirname: |
| 234 |
os.makedirs(dirname, exist_ok=True) |
| 235 |
with open(filename, "wb") as outfile: |
| 236 |
outfile.write(data) |
| 237 |
# Convert ZipInfo.external_attr to mode |
| 238 |
mode = info.external_attr >> 16 |
| 239 |
self._copy_permissions(mode, filename) |
| 240 |
|
| 241 |
def close(self): |
| 242 |
self._archive.close() |
| 243 |
|
| 244 |
|
| 245 |
extension_map = dict.fromkeys( |
| 246 |
( |
| 247 |
".tar", |
| 248 |
".tar.bz2", |
| 249 |
".tbz2", |
| 250 |
".tbz", |
| 251 |
".tz2", |
| 252 |
".tar.gz", |
| 253 |
".tgz", |
| 254 |
".taz", |
| 255 |
".tar.lzma", |
| 256 |
".tlz", |
| 257 |
".tar.xz", |
| 258 |
".txz", |
| 259 |
), |
| 260 |
TarArchive, |
| 261 |
) |
| 262 |
extension_map[".zip"] = ZipArchive |