3

我有以下 Python 函数,我在 Windows 7 上运行:

def update():
    temp_dir = tempfile.mkdtemp()
    git.Git().clone('my_repo', temp_dir)
    try:
        repo = git.Repo(temp_dir)
        repo.index.add('*')
        repo.index.commit('Empty commit')
    finally:
        from git.util import rmtree
        rmtree(temp_dir)

不幸的是,在 rmtree 线上,我得到:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\myaccount\\appdata\\local\\temp\\tmpdega8h\\.git\\objects\\pack\\pack-0ea07d13498ab92388dc33fbabaadf37511623c1.idx'

我应该怎么做才能删除 Windows 中的临时目录?

4

1 回答 1

2

已经花费了很多精力来修复 Windows 中的这种行为,其中文件锁定是文件系统的默认行为,但正如限制中所述:

[GitPython] 是在析构函数(在__del__ 方法中实现)仍然确定性运行的时代编写的。”

如果您仍想在这种情况下使用它,您将需要在代码库中搜索__del__实现,并在您认为合适时自己调用它们。

无论如何,项目测试用例所采用的技巧可能会有所帮助:

repo_dir = tempfile.mktemp()
repo = Repo.init(repo_dir)
try:
    ## use the repo

finally:
    repo.git.clear_cache()    # kill deamon-procs responding to hashes with objects
    repo = None
    if repo_dir is not None:
        gc.collect()
        gitdb.util.mman.collect()  # kept open over git-object files
        gc.collect()
        rmtree(repo_dir)

注意:这是repo.close()自 2.1.1(2016 年 12 月)以来所做的。

于 2018-06-20T01:23:56.607 回答