Recently, I've found myself increasingly caught out by stale .pyc files in our project. When I change from our mainline branch to a story branch (or vice-versa), I often find myself with inexplicable test failures because Python is using the .pyc files for no-longer-current code.
Luckily, it's pretty easy to fix this in git, using hooks, specifically the post-checkout hook. To do that, add the following to .git/hooks/post-checkout, and make the file executable:
#!/bin/bash find $(git rev-parse --show-cdup) -name "*.pyc" -delete
Now, every time you checkout a new branch, all the .pyc files will be cleared out of your git branch.
That's a bit of a big hammer if you have a large project, isn't it?
ReplyDeleteSomething like the following would target only the files changed:
#!/bin/bash
OLD_HEAD="$1"
NEW_HEAD="$2"
FLAG="$3"
olddir="$PWD"
d=$(git rev-parse --show-cdup)
if [ -n "$d" ]; then
cd $d
fi
IFS='
'
for file in $(git log "$OLD_HEAD".."$NEW_HEAD" --pretty="format:" --name-only|grep '.py$'|sort -u); do
rm -f "${file}c"
done
cd "$olddir"
That really depends on your machine; I haven't noticed a problem with ~1000 Python files in our project.
DeleteAnd are you sure that the extra git calls and shell time won't out-weigh the benefits to be gained with your smaller hammer? ;)
Why not just adding a .gitignore file in the root of your repo ? Compiled python files should not even get into your repo in first place.
ReplyDeletehttps://help.github.com/articles/ignoring-files
Just saying.
You completely miss the point. This is about removing stale .pyc files from the filesystem, where Python picks them up, and not about removing them from version control. I'm not enough of a sadist to not have .pyc files in my .gitignore.
DeleteI recommend setting $PYTHONDONTWRITEBYTECODE to 1 (See http://docs.python.org/2/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE).
ReplyDeleteThis is exactly the kind of thing that git clean is for.
ReplyDeleteAny sane person will have pyc files ignored, meaning that you would have to clean all your ignored files. I definitely wouldn't want to do this automatically, nor recommend that people do it automatically.
Delete