[Distutils] Single version number

Zooko Wilcox-O'Hearn zooko at zooko.com
Sat Jul 11 16:42:28 CEST 2009


On Jul 10, 2009, at 6:58 AM, Marius Gedminas wrote:

> What do people use to avoid repeating the version number both in  
> the setup.py as well as in application/library code, when the  
> application/library wants to know its own version number?

I have a script that reads revision control history and writes out a  
_version.py file with just "verstr='1.2.3'" in it.

Then my setup.py does this:

PKG = "zfec"
VERSIONFILE = PKG+"/_version.py"
verstr = "unknown"
try:
     verstrline = open(VERSIONFILE, "rt").read()
except EnvironmentError:
     pass # Okay, there is no version file.
else:
     VSRE = r"^verstr = ['\"]([^'\"]*)['\"]"
     mo = re.search(VSRE, verstrline, re.M)
     if mo:
         verstr = mo.group(1)
     else:
         print "unable to find version in %s" % (VERSIONFILE,)
         raise RuntimeError("if %s.py exists, it is required to be  
well-formed" % (VERSIONFILE,))


I packaged up the script that reads revision control history (darcs)  
and writes out the _version.py file as "darcsver": http:// 
pypi.python.org/pypi/darcsver .  It can be invoked as a command-line  
tool or a setuptools plugin.

I also then extended the darcsver setuptools plugin so that it would  
do effectively the same thing as the above code, so that I don't have  
to copy that code into each of my setup.py files.  Unfortunately  
issue20 of setuptools (http://bugs.python.org/setuptools/msg235 )  
means that at most one of my packages can use darcsver to do that in  
the same build, so currently my Tahoe project uses darcsver to set  
the "version" attribute and doesn't have that code snippet that I  
pasted above, but my other projects (many of which Tahoe depends on)  
have that code snippet in the setup.py.

Regards,

Zooko


More information about the Distutils-SIG mailing list