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

前端 未结 7 1406
庸人自扰
庸人自扰 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:09

    What I have actually done:

    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) I have adopted Ry4an's update hook concept, in the short-term, until I decide.

    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.

提交回复
热议问题