簡體   English   中英

使用gitpython后如何在Windows上刪除臨時目錄?

[英]How do I delete a temp directory on Windows after using gitpython?

我有以下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中的臨時目錄?

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月)以來的作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM