因此,我试图monkeypatch shutil模块,以便使用最近修复其make_archive函数,该函数允许创建大型zip文件。为什么我在猴子补丁shutil不工作的尝试?
我证明了一些想法,所以想通过快速入门来解决这个问题,可以让我继续做我想做的事情。
我的代码:
import shutil
import os
def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
zip_filename = base_name + ".zip"
archive_dir = os.path.dirname(base_name)
if not os.path.exists(archive_dir):
if logger is not None:
logger.info("creating %s", archive_dir)
if not dry_run:
os.makedirs(archive_dir)
# If zipfile module is not available, try spawning an external 'zip'
# command.
try:
import zipfile
except ImportError:
zipfile = None
if zipfile is None:
shutil._call_external_zip(base_dir, zip_filename, verbose, dry_run)
else:
if logger is not None:
logger.info("creating '%s' and adding '%s' to it",
zip_filename, base_dir)
if not dry_run:
zip = zipfile.ZipFile(zip_filename, "w",
compression=zipfile.ZIP_DEFLATED,
allowZip64=True) # this is the extra argument
for dirpath, dirnames, filenames in os.walk(base_dir):
for name in filenames:
path = os.path.normpath(os.path.join(dirpath, name))
if os.path.isfile(path):
zip.write(path, path)
if logger is not None:
logger.info("adding '%s'", path)
zip.close()
shutil._make_zipfile = _make_zipfile
# This function calls _make_zipfile when it runs
shutil.make_archive('blah', someargs)
所以,问题是......它没有做任何事情。我显然在做一些愚蠢的事情,但对我而言,我看不到它是什么。我假设有一件事情显而易见,在看了这么长时间之后,我已经失明了,所以需要一些新鲜的眼睛。我曾尝试以下方法/检查,对在这些描述答案:
Monkey-patch Python class Python monkey patch private function 和What is a monkey patch?
再加上一些其他。没有快乐
你先生,是一个非常好的人。我甚至看着那条线并且想知道,但是然后陷入一个无限循环,试图用三种等价的方法来修补_make_zipfile函数。好吧,要么没有足够的咖啡因,要么太多。 –
+++超出咖啡因错误+++从START +++重新开始,因为Hex会说。 –