Previously I wrote about gengetopt tool to parse command line arguments. There was a line about application version.

version "13.10.25"

It is important to keep this line up to date every time you compile and deploy binary.

I suggest to use Python script for this. Preliminaries: hg id -n returns local revision number, hg id -i returns global revision id. Combining date, local revision and global revision I generate version enough to determine source files used to create binary. For the following script gengetopt config must be placed in src/cmdline.ggo file. Script replaces version line with string “date local_revision:global_revision”. Listing for

#!/usr/bin/env python

from subprocess import check_output
from datetime import datetime

ggofile = open('src/cmdline.ggo', 'r')
ggo = [line.strip('\n') for line in ggofile.readlines() if not 'version' in line]

version = check_output(['hg','id','-n']).strip('\n')+':'+check_output(['hg','id','-i']).strip('\n')
version ='%Y.%m.%d') + ' ' + version

ggo.insert(0, 'version "' + version + '"')

ggofile = open('src/cmdline.ggo', 'w')
print >>ggofile,'\n'.join(ggo)

Now just run every time you want to build and deploy application binary (do not forget to regenerate parcer files with gengetopt: gengetopt -G -i cmdline.ggo in the directory with config).