Clone wiki

fastdev-django / Makefile_and_Less_Keyboard_Banging_in_Shell

Imagine: you've added a package to buildout.cfg. You go to the shell, stop the development server and run `bin/buildout`. Then re-run the server. Or another case: you edit the locale file, need to re-compile it and see it in the page.

Think a while and answer this question: what do you REALLY need?

In the end, you need to re-start the server.

Mercurial is smart enough to show us what files were changed. It's smart enough to ignore some files. Can we use a tool that can watch what we need to update for a Django server? Yes! Re-discover *Make*, a ubiquitous Unix tool that every C developer knows, but many of us, web developers, remain ignorant of.

How to Use Make in FastDev

Here's the command that you will call most of the times:

make run

As of 13.11.11, Makefile in fastdev-django is written so that it can trace the changes of the following files:

  • buildout.cfg (will rebuild bin/django)
  • locale files in locale/ (will recompile messages)

It also checks if the following necessary files are present, and installs them when needed:

  • bin/django to run the server
  • bin/sass
  • bin/pyflakes
  • bin/pep8

So, when you install the project, after downloading it, just do make run. When you update a locale file, or change buildout.cfg, also do make run, and Make will take care of updating the necessary files. and translation strings are not watched yet, but are my topmost priority now.

Makefile Tutorial

We put a file called Makefile in our repository folder to describe what operations (targets) require what commands and how they depend on each other.

Here's a simple example:

# use tabs to indent, not spaces
bin/django: bin/buildout
    ./bin/buildout install django

    python --distribute

This file says: to make bin/django, you need bin/buildout to be made first. You can add more prerequisites after the colon (:) in the line; they all have to be present in the Makefile.

Run make bin/buildout, and the command python will be executed.

Below that line, we list the commands to be executed.

In our case, the bin/buildout target creates file called bin/buildout. So, the next time, when you run make bin/buildout, Make will write that bin/buildout is up to date and won't re-execute the command.

The same with bin/django. But, if bin/buildout becomes newer than bin/django, Make will consider bin/django not up to date and that it requires re-making, so Make will re-execute all the commands in bin/django section.

If our commands do not create a file to be watched, but we want to avoid unnecessary re-execution, we can add touch command:

    touch some_target

Opposite of that, to make a target be re-executed every time, make it phony:

.PHONY: run
run: bin/django
    ./bin/django runserver

Further reading

1. GNU Make, O'Reilly, 2004. Tough, but comprehensive.

Links to tutorials are welcome.