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? ;)
Some projects (like ours) have directories with LOTS of images/directories. The find takes ages and eats all the diskio.
DeleteWhy 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.
DeleteExactly. If you run git clean then also your local config files will be purged. And you don't want to do that I presume. It is a pretty big mistake to store configuration files in the repository, so it is usually ignored as well so git clean would delete that for you. you don't want to reset your configuration every time you switch branches. Thanks for the author btw. This hook is exactly what I needed.
DeleteSo while the machine is theoretically paying out more cash, the percentages are at all times in the house’s favor. Knowing the key arithmetic that a slot machine uses to create pseudorandom outcomes isn’t enough to assist hackers, though. That’s end result of|as a result of} the inputs for a PRNG vary depending on the temporal state of each machine. The seeds are totally different at totally different times, for instance, as is the information culled from the inner clocks. So even if they perceive how a machine’s PRNG capabilities, hackers would even have to investigate the machine’s gameplay to discern its sample. That requires both time and substantial computing power, and pounding away on one’s laptop 사설토토 in front of a Pelican Pete is an effective way to entice the eye of on line casino safety.
ReplyDelete