2

There are several questions about getting the version string into setup.py. I have a slightly different set of requirements:

  1. I need version (e.g. 1.2.3) string and a build number (the result of $ git describe --tags --always HEAD)
  2. I need these versions available in my Flask application (e.g. app.config['VERSION'] = '1.2.3' and app.config['BUILD'] = '1.2.3-33-g93abc32'
  3. I need the version string available in setup.py

I don't really care if the version/build strings are under version control, though it seems that the version would make sense to have there.

So I guess the question, perhaps, is how do I inject some build information into my project from setup.py?

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290

1 Answers1

1

What I have decided to do is roll with the following:

setup.py:

# Stuff

import subprocess

__version__ = '1.2.3'
__build__ = subprocess.check_output('git describe --tags --always HEAD'
                                    .split()).decode().strip()

with open('mypkg/_version.py', 'w') as f:
    f.write('''\
# I will destroy any changes you make to this file.
# Sincerely,
# setup.py ;)

__version__ = '{}'
__build__ = '{}'
'''.format(__version__, __build__))

# other stuff

settings.update(
    version=__version__,
# more stuff
)

I might use the re module to simply find/replace the __version__ and __build__ lines, but for now this seems to work just fine.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • I would generate `mypkg/__version__.py` conditionally as a part of creating the source distribution (somewhere before `sdist` command). Make sure that `python setup.py develop` also runs it. The numbers themselves could go to `setup.cfg` to avoid changing setup.py only to bump version. – jfs Oct 14 '14 at 23:44