0

Am I able to configure setup.py and then install the egg through easy_install such that it can do some sort of template or inject the version number into one of my source files? I have version = os.environ.get('BUILD_NUMBER', 0.1) in my setup.py and I want to put this version number into one of the source files because I need a version number to run some stuff. The reason I don’t hard code this is because the version number changes each time there is a new build.

I want to inject this version/build number into a runner script - not a Python file. This runner script is sent to /usr/local/bin by easy_install.

gruuuvy
  • 2,028
  • 4
  • 31
  • 52
  • possible duplicate of [What is the correct way to share package version with setup.py and the package?](http://stackoverflow.com/questions/17583443/what-is-the-correct-way-to-share-package-version-with-setup-py-and-the-package) – Martijn Pieters Nov 11 '13 at 19:16

1 Answers1

2

If your build tool is already generating the build numbers, why not have it fill in a template version.py in your project?

  • project/
    • project/__init__.py
    • project/version.py

Where version.py is simply:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__all__ = ('__version__',)
__version__ = $BUILD_NUMBER

This way, it is always available and doesn't depend on something like setup.py metadata. For your setup.py script, try something like this:

def get_version():
    """
    Gets the latest version number out of the package,
    saving us from maintaining it in multiple places.
    """
    local_results = {}
    execfile('project/version.py', {}, local_results)
    return local_results['__version__']

setup(
    name="Project",
    version=get_version(),
    ...
)
TkTech
  • 4,729
  • 1
  • 24
  • 32
  • How does this line `__version__ = $BUILD_NUMBER` make sense? `$BUILD_NUMBER` is a bash variable, but version.py is a Python file? – gruuuvy Nov 11 '13 at 21:11
  • 1
    @poleapple It's just a placeholder, as there is no way for me to know what build system you're using. Simply use that as a template, and replace `$BUILD_NUMBER` when you build, or append to the file, or any of a hundred different ways. – TkTech Nov 11 '13 at 21:15