Python setuptools override version from command line

3.6k views Asked by At

I am setting up a continuous delivery system for a python project, and I am trying to figure out how to set the ENTIRE version string of the project build via egg_info.

I am using thoughtworks GO which has a built in version tracking label called GO_PIPELINE_LABEL. I would like to invoke the setup_tools egg building command with this version as an argument completely overriding the version in setup.py.

eg:

GO_PIPELINE_LABEL='1.2.3.4' python setup.py egg_info --tag-build=$GO_PIPELINE_LABEL bdist_egg

seems to result in

'dist/myproject-0.0.01.2.3.4-py2.7.egg' 

It always seems to concat the setup.py version and the command line one. My setup.py looks like this:

import os
from setuptools import setup, find_packages
from setuptools.command.install import install

ROOT_DIR = os.path.dirname(__file__)
SOURCE_DIR = os.path.join(ROOT_DIR)

# python setup.py egg_info -rb14 bdist_egg rotate -m.egg -k5
# python setup.py egg_info --tag-build=1.2.3.4 bdist_egg

version = ''

setup(name='myproject',
    version=version,
    description='baa',
    author='Me',
    author_email='[email protected]',
    packages=['submodule', 'another.submodule'],
    package_data = {
                       '': ['*.xsd'],
                       },
    install_requires=['cmd2',
                    'dnspython',
                    'ordereddict',
                    'prettyprint',
                    'pycontrol',
                    'simplejson',
                    'suds',
                    'pyparsing<2.0.0',
                    'urllib3',
                    'lxml',
                    ]
    )

I have tried nulling / removing the version variable in my setup.py, but whatever I do seems to result in a concatenation of the version values, and a null seems to equate to '0.0.0'. Anyone know how I can utilize setup_tools and set my version string?

Thanks, K

2

There are 2 answers

0
Tails86 On

You could try providing the version as a positional command line argument then deleting it before executing setup:

import sys
from setuptools import setup

version = sys.argv[1]
del sys.argv[1]

setup(name='myproject',
    version=version,
    ...

Then you'd execute it like so:

python setup.py '1.2.3.4' egg_info
1
Dileep On

One option is to parse option and set the version like below just before calling setup

from optparse import OptionParser
parser = OptionParser("")
parser.add_option("--tag-build", dest="version")
(info,a)=parser.parse_args()
version = info.version
print "version is ", version