text
stringlengths 0
828
|
|---|
opener = openers[suffix]
|
except KeyError:
|
raise UnknownArchive(suffix)
|
if options is None:
|
options = {}
|
if file is not None:
|
if mode in 'wa' and not hasattr(file, 'write'):
|
raise TypeError(""file.write() does not exist"", file)
|
if mode == 'r' and not hasattr(file, 'read'):
|
raise TypeError(""file.read() does not exist"", file)
|
if [filename, file].count(None) != 1:
|
raise ValueError(""either filename or file must be specified"")
|
if filename is not None:
|
file = builtins.open(filename, mode + 'b')
|
try:
|
return opener(file, mode, options)
|
except:
|
if filename is not None:
|
file.close()
|
raise"
|
529,"def extract(archive, directory, suffix=None, unpack_single_dir=False,
|
check_extract_file=None, progress_callback=None, default_mode='755'):
|
""""""
|
Extract the contents of *archive* to the specified *directory*. This
|
function ensures that no file is extracted outside of the target directory
|
(which can theoretically happen if the arcname is not relative or points
|
to a parent directory).
|
# Parameters
|
archive (str, archive-like): The filename of an archive or an already
|
opened archive.
|
directory (str): Path to the directory to unpack the contents to.
|
unpack_single_dir (bool): If this is True and if the archive contains only
|
a single top-level directory, its contents will be placed directly into
|
the target *directory*.
|
""""""
|
if isinstance(archive, str):
|
with open(archive, suffix=suffix) as archive:
|
return extract(archive, directory, None, unpack_single_dir,
|
check_extract_file, progress_callback, default_mode)
|
if isinstance(default_mode, str):
|
default_mode = int(default_mode, 8)
|
if progress_callback:
|
progress_callback(-1, 0, None)
|
names = archive.getnames()
|
# Find out if we have only one top-level directory.
|
toplevel_dirs = set()
|
for name in names:
|
parts = name.split('/')
|
if len(parts) > 1:
|
toplevel_dirs.add(parts[0])
|
if unpack_single_dir and len(toplevel_dirs) == 1:
|
stripdir = next(iter(toplevel_dirs)) + '/'
|
else:
|
stripdir = None
|
for index, name in enumerate(names):
|
if progress_callback:
|
progress_callback(index + 1, len(names), name)
|
if name.startswith('..') or name.startswith('/') or os.path.isabs(name):
|
continue
|
if check_extract_file and not check_extract_file(name):
|
continue
|
if name.endswith('/'):
|
continue
|
if stripdir:
|
filename = name[len(stripdir):]
|
if not filename:
|
continue
|
else:
|
filename = name
|
info = archive.getmember(name)
|
src = archive.extractfile(name)
|
if not src:
|
continue
|
try:
|
filename = os.path.join(directory, filename)
|
dirname = os.path.dirname(filename)
|
if not os.path.exists(dirname):
|
os.makedirs(dirname)
|
with builtins.open(filename, 'wb') as dst:
|
shutil.copyfileobj(src, dst)
|
os.chmod(filename, info.mode or default_mode)
|
os.utime(filename, (-1, info.mtime))
|
finally:
|
src.close()
|
if progress_callback:
|
progress_callback(len(names), len(names), None)"
|
530,"def transitions_to(self, dst):
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.