Automatically deleting pyc files when corresponding py is moved (Mercurial)

前端 未结 7 1407
庸人自扰
庸人自扰 2021-02-13 04:00

(I foresaw this problem might happen 3 months ago, and was told to be diligent to avoid it. Yesterday, I was bitten by it, hard, and now that it has cost me real money, I am kee

7条回答
  •  时光取名叫无心
    2021-02-13 04:13

    Here's a unix one-liner that will delete .pyc files each time you run hg update.

    Add this to your hgrc file:

    [hooks]
    preupdate.cleanpyc = hg status --no-status --removed --deleted --include "**.py" --rev .:$HG_PARENT1 --print0 | xargs -0 -I '{}' rm -f '{}c'
    

    This runs just prior to update, and gets all .py files which will be removed or deleted when the update is performed, and then deletes corresponding .pyc files.

    Here's a quick breakdown of how it works:

    hg status --no-status --removed --deleted --include "**.py" --rev .:$HG_PARENT1
    

    This gets all files removed (e.g. hg forget) or deleted (hg rm, hg mv, etc) between the current revision . and the destination ($HG_PARENT). You could add --subrepos to get all changes in sub-repositories as well if you use that feature.

    xargs -0 -I '{}' rm -f '{}c'
    

    This simply adds a 'c' to the end of each file name returned from hg status and tries to delete it. The -f flag for rm ensures that it doesn't error if the .pyc file does not exist.

    Note that mercurial automatically deletes empty directories after an update, but orphaned .pyc files often cause directories to be left around. Since this runs before update, it ensure that empty directories are properly deleted.

提交回复
热议问题