移动相应的py时自动删除pyc文件

移动相应的py时自动删除pyc文件

本文介绍了移动相应的py时自动删除pyc文件(Mercurial)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我预见到这个问题可能会在3个月前发生,并被告知要努力避免.昨天,我被它咬伤了,辛苦了,现在它花了我很多钱,我很想解决这个问题)

(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 keen to fix it.)

如果将我的Python源文件之一移动到另一个目录,则需要记住告诉Mercurial它已移动(hg move).

If I move one of my Python source files into another directory, I need to remember to tell Mercurial that it moved (hg move).

当我使用Mercurial将新软件部署到服务器时,它会仔细删除旧的Python文件并在新目录中创建它.

When I deploy the new software to my server with Mercurial, it carefully deletes the old Python file and creates it in the new directory.

但是,Mercurial并未意识到同一目录中的pyc文件,并将其留在了后面.同一目录中的其他模块优先使用旧的pyc而不是新的python文件.

However, Mercurial is unaware of the pyc file in the same directory, and leaves it behind. The old pyc is used preferentially over new python file by other modules in the same directory.

随之而来的不是欢笑.

当我移动python文件时,如何说服Mercurial自动删除旧的pyc文件?还有其他更好的做法吗?试图记住从所有Mercurial存储库中删除pyc文件无效.

How can I persuade Mercurial to automatically delete my old pyc file when I move the python file? Is there another better practice? Trying to remember to delete the pyc file from all the Mercurial repositories isn't working.

推荐答案

我实际上所做的事情:

1)我正在考虑尼古拉斯·奈特(Nicholas Knight)关于使用适当部署策略的建议.我一直在阅读有关 Buildout Collective.hostout 了解更多信息.我需要确定这样的重量级策略是否适合我的项目相对简单的需求.

1) I am considering Nicholas Knight's suggestion about using a proper deployment strategy. I have been reading about Buildout and Collective.hostout to learn more. I need to decide whether such heavy-weight strategies are worthwhile for my project's relatively simple requirements.

2)在我决定之前,我在短期内采用了Ry4an的更新挂钩概念.

2) I have adopted Ry4an's update hook concept, in the short-term, until I decide.

3)我忽略了Ry4an关于过大杀伤力的警告,并编写了Python脚本来仅删除杂散的.pyc文件.

3) I ignored Ry4an's warning about overkill, and wrote a Python script to only delete stray .pyc files.

#!/usr/bin/env python
""" Searches subdirectories of the current directory looking for .pyc files which
    do not have matching .py files, and deletes them.

    This is useful as a hook for version control when Python files are moved.
    It is dangerous for projects that deliberately include Python
    binaries without source.
"""
import os
import os.path
for root, dirs, files in os.walk("."):
    pyc_files = filter(lambda filename: filename.endswith(".pyc"), files)
    py_files = set(filter(lambda filename: filename.endswith(".py"), files))
    excess_pyc_files = filter(lambda pyc_filename: pyc_filename[:-1] not in py_files, pyc_files)
    for excess_pyc_file in excess_pyc_files:
        full_path = os.path.join(root, excess_pyc_file)
        print "Removing old PYC file:", full_path
        os.remove(full_path)

我的更新挂钩现在称为此命令,而不是其他人建议的查找"命令.

My update hooks now call this rather than the "find" commands suggested by others.

这篇关于移动相应的py时自动删除pyc文件(Mercurial)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:51