geekchick77: (Default)
[personal profile] geekchick77
I work on a lot of different coding projects, nearly all Python and often but not always Django. I am a huge fan of virtualenvwrapper, but I always find myself editing the activate script to export project-specific variables. It's not hard to do, but it's tedious and definitely repetitive. I also notice there are certain predictable ways I break the build:
  • Forgetting to add a package to requirements
  • Forgetting to run the linter
  • (rarely, but it does happen) Forgetting to run unit tests
I've been on an automation kick lately, and I decided to see what I could do about these issues.

As it turns out, virtualenvwrapper is highly customizable, and all I had to do was edit mkvirtualenv in WORKON_HOME to have it modify my activate scripts. While I was in there, I decided to make a skeleton pre-commit hook for the project:

#!/bin/bash
# This hook is run after a new virtualenv is created and before it is activated.

# Automatically set django settings for the virtualenv
echo "export DJANGO_SETTINGS_MODULE=$1.settings.development" >> "$1/bin/activate"

# Create a pre-commit hook script that can be copied over as desired
echo "#!/bin/sh
set -e
. $WORKON_HOME/$1/bin/activate
piplint requirements/*
flake8
jshint $1/static/js
python manage.py test $1
" >> "$1/bin/pre-commit"

Now every new virtualenv comes with Django settings prepopulated. If it's not a Django project, that unused environment variable isn't going to hurt anything.

You can add more environment variables later by editing ~/.virtualenvs/<VIRTUAL_ENV_NAME>/bin/activate and adding "export <ENV_VAR>=<VALUE>" entries to the end of the file.

I don't automatically move that pre-commit hook because there is no guarantee I have anywhere to put it. Sometimes I make the virtualenv before I initialized a git repo. However, the hard work has been done so once I make a project, I can simply copy over the hook.

Date: 2015-02-08 04:30 pm (UTC)
mdlbear: the positively imaginary half of a cubic mandelbrot set (Default)
From: [personal profile] mdlbear
Nice.

You could easily check for the presence of a git repo and create one if it's not there. You could also add a flag that says what kind of project you're making, or detect it from what's in the directory.

My tendency is to put this kind of thing into a Makefile, so that I can give a command like

make setup

and have make work out what needs to be done given the dependencies.

Date: 2015-02-16 02:13 pm (UTC)
brainwane: My smiling face, including a small gold bindi (Default)
From: [personal profile] brainwane
Interesting! I will have to consider doing similar things for my projects....

Profile

geekchick77: (Default)
Jessamyn Smith

November 2016

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
272829 30   

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 19th, 2017 06:52 pm
Powered by Dreamwidth Studios