If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
When developing and deploying its handy to have a version number. Managing this by hand is error prone. But python-versioner comes to the rescue, also for web2py projects, at least if you're using GIT.
How does Versioneer work?
Versioneer reads the 'tag' from your git repository en puts the tag inside your code. Otherwise you have to update __version__ by hand in your python code of whatever variable you use in Web2y to record and show your version number.
Using it in Web2py
Versioneer is designed to integrate into the standard Python setup.py from Distutils, but can easely integrate in Web2py. In one of my model files I used to hardcode the version like this:
     settings.VERSION = "2.4.1"
But now we can do better:
install python-versioneer using: pip install python-versioneer
Now you can inside your web2py folder, applications/[your w2py app] and
  • run versioneer-installer in your source tree: this installs versioneer.py 
  • create setup.py, see below for a minimal version
from distutils.core import setup
import versioneer

versioneer.VCS = 'git'
versioneer.versionfile_source = 'modules/_version.py'
versioneer.versionfile_build = None
versioneer.tag_prefix = '' # tags are like 1.2.0
versioneer.parentdir_prefix = '' # or dirname like 'myproject-1.2.0'

setup(name='[your w2py app]',
Now you can give distutils-commands including a new command 'versioneer' as
python setup.py versioneer
This creates a _version file as a module, that can be included in your web2py code like this:
from _version import get_versions

settings = Storage()
settings.VERSION = get_versions()['version']
But wait, we first need to 'tag' our code otherwise the get_version() can't find a starting point for the version number.
  • 1: git tag 1.0
  • 2: git push; git push --tags
From now on, when committing the version number gets appended an identifying number of commits since the last tag, followed by the commit identifier.
Drawback & Solution
When using the code as-is inside your Web2py app the version number is determined at every request. If you use setup.py to 'built'or 'sdist' your app Versioneer is smart enough to replace the dynamic _version.py by a static _version.py with the fixed version info. I've made a PR to add a setup.py command 'static_version' that does the same.  If you get the code from https://github.com/ndegroot/python-versioneer you can use the new command directly. You have to use it before you commit a production-ready version, so it's best to make this an automatic step.
Another option is to cache this module method. See http://www.web2py.com/books/default/chapter/29/04/the-core?search=lazy_cache  
Nico de Groot

Related slices

Comments (0)

Hosting graciously provided by:
Python Anywhere