Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: shutil.py
"""Utility functions for copying and archiving files and directory trees.
[0] Fix | Delete
[1] Fix | Delete
XXX The functions here don't copy the resource fork or other metadata on Mac.
[2] Fix | Delete
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
import os
[6] Fix | Delete
import sys
[7] Fix | Delete
import stat
[8] Fix | Delete
from os.path import abspath
[9] Fix | Delete
import fnmatch
[10] Fix | Delete
import collections
[11] Fix | Delete
import errno
[12] Fix | Delete
[13] Fix | Delete
try:
[14] Fix | Delete
import zlib
[15] Fix | Delete
del zlib
[16] Fix | Delete
_ZLIB_SUPPORTED = True
[17] Fix | Delete
except ImportError:
[18] Fix | Delete
_ZLIB_SUPPORTED = False
[19] Fix | Delete
[20] Fix | Delete
try:
[21] Fix | Delete
import bz2
[22] Fix | Delete
del bz2
[23] Fix | Delete
_BZ2_SUPPORTED = True
[24] Fix | Delete
except ImportError:
[25] Fix | Delete
_BZ2_SUPPORTED = False
[26] Fix | Delete
[27] Fix | Delete
try:
[28] Fix | Delete
from pwd import getpwnam
[29] Fix | Delete
except ImportError:
[30] Fix | Delete
getpwnam = None
[31] Fix | Delete
[32] Fix | Delete
try:
[33] Fix | Delete
from grp import getgrnam
[34] Fix | Delete
except ImportError:
[35] Fix | Delete
getgrnam = None
[36] Fix | Delete
[37] Fix | Delete
__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
[38] Fix | Delete
"copytree", "move", "rmtree", "Error", "SpecialFileError",
[39] Fix | Delete
"ExecError", "make_archive", "get_archive_formats",
[40] Fix | Delete
"register_archive_format", "unregister_archive_format",
[41] Fix | Delete
"ignore_patterns"]
[42] Fix | Delete
[43] Fix | Delete
class Error(EnvironmentError):
[44] Fix | Delete
pass
[45] Fix | Delete
[46] Fix | Delete
class SpecialFileError(EnvironmentError):
[47] Fix | Delete
"""Raised when trying to do a kind of operation (e.g. copying) which is
[48] Fix | Delete
not supported on a special file (e.g. a named pipe)"""
[49] Fix | Delete
[50] Fix | Delete
class ExecError(EnvironmentError):
[51] Fix | Delete
"""Raised when a command could not be executed"""
[52] Fix | Delete
[53] Fix | Delete
try:
[54] Fix | Delete
WindowsError
[55] Fix | Delete
except NameError:
[56] Fix | Delete
WindowsError = None
[57] Fix | Delete
[58] Fix | Delete
def copyfileobj(fsrc, fdst, length=16*1024):
[59] Fix | Delete
"""copy data from file-like object fsrc to file-like object fdst"""
[60] Fix | Delete
while 1:
[61] Fix | Delete
buf = fsrc.read(length)
[62] Fix | Delete
if not buf:
[63] Fix | Delete
break
[64] Fix | Delete
fdst.write(buf)
[65] Fix | Delete
[66] Fix | Delete
def _samefile(src, dst):
[67] Fix | Delete
# Macintosh, Unix.
[68] Fix | Delete
if hasattr(os.path, 'samefile'):
[69] Fix | Delete
try:
[70] Fix | Delete
return os.path.samefile(src, dst)
[71] Fix | Delete
except OSError:
[72] Fix | Delete
return False
[73] Fix | Delete
[74] Fix | Delete
# All other platforms: check for same pathname.
[75] Fix | Delete
return (os.path.normcase(os.path.abspath(src)) ==
[76] Fix | Delete
os.path.normcase(os.path.abspath(dst)))
[77] Fix | Delete
[78] Fix | Delete
def copyfile(src, dst):
[79] Fix | Delete
"""Copy data from src to dst"""
[80] Fix | Delete
if _samefile(src, dst):
[81] Fix | Delete
raise Error("`%s` and `%s` are the same file" % (src, dst))
[82] Fix | Delete
[83] Fix | Delete
for fn in [src, dst]:
[84] Fix | Delete
try:
[85] Fix | Delete
st = os.stat(fn)
[86] Fix | Delete
except OSError:
[87] Fix | Delete
# File most likely does not exist
[88] Fix | Delete
pass
[89] Fix | Delete
else:
[90] Fix | Delete
# XXX What about other special files? (sockets, devices...)
[91] Fix | Delete
if stat.S_ISFIFO(st.st_mode):
[92] Fix | Delete
raise SpecialFileError("`%s` is a named pipe" % fn)
[93] Fix | Delete
[94] Fix | Delete
with open(src, 'rb') as fsrc:
[95] Fix | Delete
with open(dst, 'wb') as fdst:
[96] Fix | Delete
copyfileobj(fsrc, fdst)
[97] Fix | Delete
[98] Fix | Delete
def copymode(src, dst):
[99] Fix | Delete
"""Copy mode bits from src to dst"""
[100] Fix | Delete
if hasattr(os, 'chmod'):
[101] Fix | Delete
st = os.stat(src)
[102] Fix | Delete
mode = stat.S_IMODE(st.st_mode)
[103] Fix | Delete
os.chmod(dst, mode)
[104] Fix | Delete
[105] Fix | Delete
def copystat(src, dst):
[106] Fix | Delete
"""Copy file metadata
[107] Fix | Delete
[108] Fix | Delete
Copy the permission bits, last access time, last modification time, and
[109] Fix | Delete
flags from `src` to `dst`. On Linux, copystat() also copies the "extended
[110] Fix | Delete
attributes" where possible. The file contents, owner, and group are
[111] Fix | Delete
unaffected. `src` and `dst` are path names given as strings.
[112] Fix | Delete
"""
[113] Fix | Delete
st = os.stat(src)
[114] Fix | Delete
mode = stat.S_IMODE(st.st_mode)
[115] Fix | Delete
if hasattr(os, 'utime'):
[116] Fix | Delete
os.utime(dst, (st.st_atime, st.st_mtime))
[117] Fix | Delete
if hasattr(os, 'chmod'):
[118] Fix | Delete
os.chmod(dst, mode)
[119] Fix | Delete
if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
[120] Fix | Delete
try:
[121] Fix | Delete
os.chflags(dst, st.st_flags)
[122] Fix | Delete
except OSError, why:
[123] Fix | Delete
for err in 'EOPNOTSUPP', 'ENOTSUP':
[124] Fix | Delete
if hasattr(errno, err) and why.errno == getattr(errno, err):
[125] Fix | Delete
break
[126] Fix | Delete
else:
[127] Fix | Delete
raise
[128] Fix | Delete
[129] Fix | Delete
def copy(src, dst):
[130] Fix | Delete
"""Copy data and mode bits ("cp src dst").
[131] Fix | Delete
[132] Fix | Delete
The destination may be a directory.
[133] Fix | Delete
[134] Fix | Delete
"""
[135] Fix | Delete
if os.path.isdir(dst):
[136] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[137] Fix | Delete
copyfile(src, dst)
[138] Fix | Delete
copymode(src, dst)
[139] Fix | Delete
[140] Fix | Delete
def copy2(src, dst):
[141] Fix | Delete
"""Copy data and metadata. Return the file's destination.
[142] Fix | Delete
[143] Fix | Delete
Metadata is copied with copystat(). Please see the copystat function
[144] Fix | Delete
for more information.
[145] Fix | Delete
[146] Fix | Delete
The destination may be a directory.
[147] Fix | Delete
[148] Fix | Delete
"""
[149] Fix | Delete
if os.path.isdir(dst):
[150] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[151] Fix | Delete
copyfile(src, dst)
[152] Fix | Delete
copystat(src, dst)
[153] Fix | Delete
[154] Fix | Delete
def ignore_patterns(*patterns):
[155] Fix | Delete
"""Function that can be used as copytree() ignore parameter.
[156] Fix | Delete
[157] Fix | Delete
Patterns is a sequence of glob-style patterns
[158] Fix | Delete
that are used to exclude files"""
[159] Fix | Delete
def _ignore_patterns(path, names):
[160] Fix | Delete
ignored_names = []
[161] Fix | Delete
for pattern in patterns:
[162] Fix | Delete
ignored_names.extend(fnmatch.filter(names, pattern))
[163] Fix | Delete
return set(ignored_names)
[164] Fix | Delete
return _ignore_patterns
[165] Fix | Delete
[166] Fix | Delete
def copytree(src, dst, symlinks=False, ignore=None):
[167] Fix | Delete
"""Recursively copy a directory tree using copy2().
[168] Fix | Delete
[169] Fix | Delete
The destination directory must not already exist.
[170] Fix | Delete
If exception(s) occur, an Error is raised with a list of reasons.
[171] Fix | Delete
[172] Fix | Delete
If the optional symlinks flag is true, symbolic links in the
[173] Fix | Delete
source tree result in symbolic links in the destination tree; if
[174] Fix | Delete
it is false, the contents of the files pointed to by symbolic
[175] Fix | Delete
links are copied.
[176] Fix | Delete
[177] Fix | Delete
The optional ignore argument is a callable. If given, it
[178] Fix | Delete
is called with the `src` parameter, which is the directory
[179] Fix | Delete
being visited by copytree(), and `names` which is the list of
[180] Fix | Delete
`src` contents, as returned by os.listdir():
[181] Fix | Delete
[182] Fix | Delete
callable(src, names) -> ignored_names
[183] Fix | Delete
[184] Fix | Delete
Since copytree() is called recursively, the callable will be
[185] Fix | Delete
called once for each directory that is copied. It returns a
[186] Fix | Delete
list of names relative to the `src` directory that should
[187] Fix | Delete
not be copied.
[188] Fix | Delete
[189] Fix | Delete
XXX Consider this example code rather than the ultimate tool.
[190] Fix | Delete
[191] Fix | Delete
"""
[192] Fix | Delete
names = os.listdir(src)
[193] Fix | Delete
if ignore is not None:
[194] Fix | Delete
ignored_names = ignore(src, names)
[195] Fix | Delete
else:
[196] Fix | Delete
ignored_names = set()
[197] Fix | Delete
[198] Fix | Delete
os.makedirs(dst)
[199] Fix | Delete
errors = []
[200] Fix | Delete
for name in names:
[201] Fix | Delete
if name in ignored_names:
[202] Fix | Delete
continue
[203] Fix | Delete
srcname = os.path.join(src, name)
[204] Fix | Delete
dstname = os.path.join(dst, name)
[205] Fix | Delete
try:
[206] Fix | Delete
if symlinks and os.path.islink(srcname):
[207] Fix | Delete
linkto = os.readlink(srcname)
[208] Fix | Delete
os.symlink(linkto, dstname)
[209] Fix | Delete
elif os.path.isdir(srcname):
[210] Fix | Delete
copytree(srcname, dstname, symlinks, ignore)
[211] Fix | Delete
else:
[212] Fix | Delete
# Will raise a SpecialFileError for unsupported file types
[213] Fix | Delete
copy2(srcname, dstname)
[214] Fix | Delete
# catch the Error from the recursive copytree so that we can
[215] Fix | Delete
# continue with other files
[216] Fix | Delete
except Error, err:
[217] Fix | Delete
errors.extend(err.args[0])
[218] Fix | Delete
except EnvironmentError, why:
[219] Fix | Delete
errors.append((srcname, dstname, str(why)))
[220] Fix | Delete
try:
[221] Fix | Delete
copystat(src, dst)
[222] Fix | Delete
except OSError, why:
[223] Fix | Delete
if WindowsError is not None and isinstance(why, WindowsError):
[224] Fix | Delete
# Copying file access times may fail on Windows
[225] Fix | Delete
pass
[226] Fix | Delete
else:
[227] Fix | Delete
errors.append((src, dst, str(why)))
[228] Fix | Delete
if errors:
[229] Fix | Delete
raise Error, errors
[230] Fix | Delete
[231] Fix | Delete
def rmtree(path, ignore_errors=False, onerror=None):
[232] Fix | Delete
"""Recursively delete a directory tree.
[233] Fix | Delete
[234] Fix | Delete
If ignore_errors is set, errors are ignored; otherwise, if onerror
[235] Fix | Delete
is set, it is called to handle the error with arguments (func,
[236] Fix | Delete
path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
[237] Fix | Delete
path is the argument to that function that caused it to fail; and
[238] Fix | Delete
exc_info is a tuple returned by sys.exc_info(). If ignore_errors
[239] Fix | Delete
is false and onerror is None, an exception is raised.
[240] Fix | Delete
[241] Fix | Delete
"""
[242] Fix | Delete
if ignore_errors:
[243] Fix | Delete
def onerror(*args):
[244] Fix | Delete
pass
[245] Fix | Delete
elif onerror is None:
[246] Fix | Delete
def onerror(*args):
[247] Fix | Delete
raise
[248] Fix | Delete
try:
[249] Fix | Delete
if os.path.islink(path):
[250] Fix | Delete
# symlinks to directories are forbidden, see bug #1669
[251] Fix | Delete
raise OSError("Cannot call rmtree on a symbolic link")
[252] Fix | Delete
except OSError:
[253] Fix | Delete
onerror(os.path.islink, path, sys.exc_info())
[254] Fix | Delete
# can't continue even if onerror hook returns
[255] Fix | Delete
return
[256] Fix | Delete
names = []
[257] Fix | Delete
try:
[258] Fix | Delete
names = os.listdir(path)
[259] Fix | Delete
except os.error, err:
[260] Fix | Delete
onerror(os.listdir, path, sys.exc_info())
[261] Fix | Delete
for name in names:
[262] Fix | Delete
fullname = os.path.join(path, name)
[263] Fix | Delete
try:
[264] Fix | Delete
mode = os.lstat(fullname).st_mode
[265] Fix | Delete
except os.error:
[266] Fix | Delete
mode = 0
[267] Fix | Delete
if stat.S_ISDIR(mode):
[268] Fix | Delete
rmtree(fullname, ignore_errors, onerror)
[269] Fix | Delete
else:
[270] Fix | Delete
try:
[271] Fix | Delete
os.remove(fullname)
[272] Fix | Delete
except os.error, err:
[273] Fix | Delete
onerror(os.remove, fullname, sys.exc_info())
[274] Fix | Delete
try:
[275] Fix | Delete
os.rmdir(path)
[276] Fix | Delete
except os.error:
[277] Fix | Delete
onerror(os.rmdir, path, sys.exc_info())
[278] Fix | Delete
[279] Fix | Delete
[280] Fix | Delete
def _basename(path):
[281] Fix | Delete
# A basename() variant which first strips the trailing slash, if present.
[282] Fix | Delete
# Thus we always get the last component of the path, even for directories.
[283] Fix | Delete
sep = os.path.sep + (os.path.altsep or '')
[284] Fix | Delete
return os.path.basename(path.rstrip(sep))
[285] Fix | Delete
[286] Fix | Delete
def move(src, dst):
[287] Fix | Delete
"""Recursively move a file or directory to another location. This is
[288] Fix | Delete
similar to the Unix "mv" command.
[289] Fix | Delete
[290] Fix | Delete
If the destination is a directory or a symlink to a directory, the source
[291] Fix | Delete
is moved inside the directory. The destination path must not already
[292] Fix | Delete
exist.
[293] Fix | Delete
[294] Fix | Delete
If the destination already exists but is not a directory, it may be
[295] Fix | Delete
overwritten depending on os.rename() semantics.
[296] Fix | Delete
[297] Fix | Delete
If the destination is on our current filesystem, then rename() is used.
[298] Fix | Delete
Otherwise, src is copied to the destination and then removed.
[299] Fix | Delete
A lot more could be done here... A look at a mv.c shows a lot of
[300] Fix | Delete
the issues this implementation glosses over.
[301] Fix | Delete
[302] Fix | Delete
"""
[303] Fix | Delete
real_dst = dst
[304] Fix | Delete
if os.path.isdir(dst):
[305] Fix | Delete
if _samefile(src, dst):
[306] Fix | Delete
# We might be on a case insensitive filesystem,
[307] Fix | Delete
# perform the rename anyway.
[308] Fix | Delete
os.rename(src, dst)
[309] Fix | Delete
return
[310] Fix | Delete
[311] Fix | Delete
real_dst = os.path.join(dst, _basename(src))
[312] Fix | Delete
if os.path.exists(real_dst):
[313] Fix | Delete
raise Error, "Destination path '%s' already exists" % real_dst
[314] Fix | Delete
try:
[315] Fix | Delete
os.rename(src, real_dst)
[316] Fix | Delete
except OSError:
[317] Fix | Delete
if os.path.isdir(src):
[318] Fix | Delete
if _destinsrc(src, dst):
[319] Fix | Delete
raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
[320] Fix | Delete
copytree(src, real_dst, symlinks=True)
[321] Fix | Delete
rmtree(src)
[322] Fix | Delete
else:
[323] Fix | Delete
copy2(src, real_dst)
[324] Fix | Delete
os.unlink(src)
[325] Fix | Delete
[326] Fix | Delete
def _destinsrc(src, dst):
[327] Fix | Delete
src = abspath(src)
[328] Fix | Delete
dst = abspath(dst)
[329] Fix | Delete
if not src.endswith(os.path.sep):
[330] Fix | Delete
src += os.path.sep
[331] Fix | Delete
if not dst.endswith(os.path.sep):
[332] Fix | Delete
dst += os.path.sep
[333] Fix | Delete
return dst.startswith(src)
[334] Fix | Delete
[335] Fix | Delete
def _get_gid(name):
[336] Fix | Delete
"""Returns a gid, given a group name."""
[337] Fix | Delete
if getgrnam is None or name is None:
[338] Fix | Delete
return None
[339] Fix | Delete
try:
[340] Fix | Delete
result = getgrnam(name)
[341] Fix | Delete
except KeyError:
[342] Fix | Delete
result = None
[343] Fix | Delete
if result is not None:
[344] Fix | Delete
return result[2]
[345] Fix | Delete
return None
[346] Fix | Delete
[347] Fix | Delete
def _get_uid(name):
[348] Fix | Delete
"""Returns an uid, given a user name."""
[349] Fix | Delete
if getpwnam is None or name is None:
[350] Fix | Delete
return None
[351] Fix | Delete
try:
[352] Fix | Delete
result = getpwnam(name)
[353] Fix | Delete
except KeyError:
[354] Fix | Delete
result = None
[355] Fix | Delete
if result is not None:
[356] Fix | Delete
return result[2]
[357] Fix | Delete
return None
[358] Fix | Delete
[359] Fix | Delete
def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
[360] Fix | Delete
owner=None, group=None, logger=None):
[361] Fix | Delete
"""Create a (possibly compressed) tar file from all the files under
[362] Fix | Delete
'base_dir'.
[363] Fix | Delete
[364] Fix | Delete
'compress' must be "gzip" (the default), "bzip2", or None.
[365] Fix | Delete
[366] Fix | Delete
'owner' and 'group' can be used to define an owner and a group for the
[367] Fix | Delete
archive that is being built. If not provided, the current owner and group
[368] Fix | Delete
will be used.
[369] Fix | Delete
[370] Fix | Delete
The output tar file will be named 'base_name' + ".tar", possibly plus
[371] Fix | Delete
the appropriate compression extension (".gz", or ".bz2").
[372] Fix | Delete
[373] Fix | Delete
Returns the output filename.
[374] Fix | Delete
"""
[375] Fix | Delete
if compress is None:
[376] Fix | Delete
tar_compression = ''
[377] Fix | Delete
elif _ZLIB_SUPPORTED and compress == 'gzip':
[378] Fix | Delete
tar_compression = 'gz'
[379] Fix | Delete
elif _BZ2_SUPPORTED and compress == 'bzip2':
[380] Fix | Delete
tar_compression = 'bz2'
[381] Fix | Delete
else:
[382] Fix | Delete
raise ValueError("bad value for 'compress', or compression format not "
[383] Fix | Delete
"supported : {0}".format(compress))
[384] Fix | Delete
[385] Fix | Delete
compress_ext = '.' + tar_compression if compress else ''
[386] Fix | Delete
archive_name = base_name + '.tar' + compress_ext
[387] Fix | Delete
archive_dir = os.path.dirname(archive_name)
[388] Fix | Delete
[389] Fix | Delete
if archive_dir and not os.path.exists(archive_dir):
[390] Fix | Delete
if logger is not None:
[391] Fix | Delete
logger.info("creating %s", archive_dir)
[392] Fix | Delete
if not dry_run:
[393] Fix | Delete
os.makedirs(archive_dir)
[394] Fix | Delete
[395] Fix | Delete
[396] Fix | Delete
# creating the tarball
[397] Fix | Delete
import tarfile # late import so Python build itself doesn't break
[398] Fix | Delete
[399] Fix | Delete
if logger is not None:
[400] Fix | Delete
logger.info('Creating tar archive')
[401] Fix | Delete
[402] Fix | Delete
uid = _get_uid(owner)
[403] Fix | Delete
gid = _get_gid(group)
[404] Fix | Delete
[405] Fix | Delete
def _set_uid_gid(tarinfo):
[406] Fix | Delete
if gid is not None:
[407] Fix | Delete
tarinfo.gid = gid
[408] Fix | Delete
tarinfo.gname = group
[409] Fix | Delete
if uid is not None:
[410] Fix | Delete
tarinfo.uid = uid
[411] Fix | Delete
tarinfo.uname = owner
[412] Fix | Delete
return tarinfo
[413] Fix | Delete
[414] Fix | Delete
if not dry_run:
[415] Fix | Delete
tar = tarfile.open(archive_name, 'w|%s' % tar_compression)
[416] Fix | Delete
try:
[417] Fix | Delete
tar.add(base_dir, filter=_set_uid_gid)
[418] Fix | Delete
finally:
[419] Fix | Delete
tar.close()
[420] Fix | Delete
[421] Fix | Delete
return archive_name
[422] Fix | Delete
[423] Fix | Delete
def _call_external_zip(base_dir, zip_filename, verbose, dry_run, logger):
[424] Fix | Delete
# XXX see if we want to keep an external call here
[425] Fix | Delete
if verbose:
[426] Fix | Delete
zipoptions = "-r"
[427] Fix | Delete
else:
[428] Fix | Delete
zipoptions = "-rq"
[429] Fix | Delete
cmd = ["zip", zipoptions, zip_filename, base_dir]
[430] Fix | Delete
if logger is not None:
[431] Fix | Delete
logger.info(' '.join(cmd))
[432] Fix | Delete
if dry_run:
[433] Fix | Delete
return
[434] Fix | Delete
import subprocess
[435] Fix | Delete
try:
[436] Fix | Delete
subprocess.check_call(cmd)
[437] Fix | Delete
except subprocess.CalledProcessError:
[438] Fix | Delete
# XXX really should distinguish between "couldn't find
[439] Fix | Delete
# external 'zip' command" and "zip failed".
[440] Fix | Delete
raise ExecError, \
[441] Fix | Delete
("unable to create zip file '%s': "
[442] Fix | Delete
"could neither import the 'zipfile' module nor "
[443] Fix | Delete
"find a standalone zip utility") % zip_filename
[444] Fix | Delete
[445] Fix | Delete
def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
[446] Fix | Delete
"""Create a zip file from all the files under 'base_dir'.
[447] Fix | Delete
[448] Fix | Delete
The output zip file will be named 'base_name' + ".zip". Uses either the
[449] Fix | Delete
"zipfile" Python module (if available) or the InfoZIP "zip" utility
[450] Fix | Delete
(if installed and found on the default search path). If neither tool is
[451] Fix | Delete
available, raises ExecError. Returns the name of the output zip
[452] Fix | Delete
file.
[453] Fix | Delete
"""
[454] Fix | Delete
zip_filename = base_name + ".zip"
[455] Fix | Delete
archive_dir = os.path.dirname(base_name)
[456] Fix | Delete
[457] Fix | Delete
if archive_dir and not os.path.exists(archive_dir):
[458] Fix | Delete
if logger is not None:
[459] Fix | Delete
logger.info("creating %s", archive_dir)
[460] Fix | Delete
if not dry_run:
[461] Fix | Delete
os.makedirs(archive_dir)
[462] Fix | Delete
[463] Fix | Delete
# If zipfile module is not available, try spawning an external 'zip'
[464] Fix | Delete
# command.
[465] Fix | Delete
try:
[466] Fix | Delete
import zlib
[467] Fix | Delete
import zipfile
[468] Fix | Delete
except ImportError:
[469] Fix | Delete
zipfile = None
[470] Fix | Delete
[471] Fix | Delete
if zipfile is None:
[472] Fix | Delete
_call_external_zip(base_dir, zip_filename, verbose, dry_run, logger)
[473] Fix | Delete
else:
[474] Fix | Delete
if logger is not None:
[475] Fix | Delete
logger.info("creating '%s' and adding '%s' to it",
[476] Fix | Delete
zip_filename, base_dir)
[477] Fix | Delete
[478] Fix | Delete
if not dry_run:
[479] Fix | Delete
with zipfile.ZipFile(zip_filename, "w",
[480] Fix | Delete
compression=zipfile.ZIP_DEFLATED) as zf:
[481] Fix | Delete
path = os.path.normpath(base_dir)
[482] Fix | Delete
if path != os.curdir:
[483] Fix | Delete
zf.write(path, path)
[484] Fix | Delete
if logger is not None:
[485] Fix | Delete
logger.info("adding '%s'", path)
[486] Fix | Delete
for dirpath, dirnames, filenames in os.walk(base_dir):
[487] Fix | Delete
for name in sorted(dirnames):
[488] Fix | Delete
path = os.path.normpath(os.path.join(dirpath, name))
[489] Fix | Delete
zf.write(path, path)
[490] Fix | Delete
if logger is not None:
[491] Fix | Delete
logger.info("adding '%s'", path)
[492] Fix | Delete
for name in filenames:
[493] Fix | Delete
path = os.path.normpath(os.path.join(dirpath, name))
[494] Fix | Delete
if os.path.isfile(path):
[495] Fix | Delete
zf.write(path, path)
[496] Fix | Delete
if logger is not None:
[497] Fix | Delete
logger.info("adding '%s'", path)
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function