(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.)
If I move one of my Python source files into another directory, I need to remember to tell Mercurial that it moved (hg move
).
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.
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.
What ensues is NOT hilarity.
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.
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.
My update hooks now call this rather than the "find" commands suggested by others.
Here's a unix one-liner that will delete .pyc files each time you run
hg update
.Add this to your hgrc file:
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:
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.This simply adds a 'c' to the end of each file name returned from
hg status
and tries to delete it. The-f
flag forrm
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.
How about using an update hook on the server side? Put this in the repository's
.hg
directory'shgrc
file:That will delete all .pyc files whenever you update on the server. If you're worried about the cost of rebuilding all the .pyc files you could always get just a little more clever in the hook and delete only the .pyc's for which there is no .py, but that's probably overkill.
I use the
.hgignore
file to skip versionning of all my .pyc and .py~ (editor's temp files). For example, this is my version :Also adding a hook on update to remove them is also a interesting trick if you want to not only ignore noise but remove it from your local workspace area.
i use this script to delete the .pyc files in the current folder, this script could be used alone or include it in the exit function to delete the .pyc files when u exit.