Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,53 @@ this package allows to extract it from the underlying Git repository:
...)


Writing the version in a python file
------------------------------------

If you also want it in a readily accessible files:

.. code-block:: python

setup(
name='foobar',
version_format='{tag}.dev{commitcount}+{gitsha}',
version_files='foobar/__version__.py'
setup_requires=['setuptools-git-version'],
...)

For this to work, do the following:

.. code-block:: bash

$ echo foobar/__version__.py >> .gitignore

Then add this to your __init__.py:

.. code-block:: python

try:
from __version__ import __version__
except:
__version__ = 'notset; run setup.py'

Now, everytime you run setuptools, it will update the __version__.py file.

And you can get the version like this:

.. code-block:: python

>>> import foobar
>>> print foobar.__version


Changes
-------

1.0.4 - 2015-08-10
++++++++++++++++++

- [feature] add version_files

1.0.3 - 2015-04-23
++++++++++++++++++

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='setuptools-git-version',
version='1.0.3',
version='1.0.4',
url='https://github.com/pyfidelity/setuptools-git-version',
author='pyfidelity UG',
author_email='mail@pyfidelity.com',
Expand All @@ -22,5 +22,6 @@
entry_points="""
[distutils.setup_keywords]
version_format = setuptools_git_version:validate_version_format
version_files = setuptools_git_version:write_version_files
""",
)
39 changes: 39 additions & 0 deletions setuptools_git_version.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from pkg_resources import get_distribution
from distutils.errors import DistutilsSetupError
from subprocess import check_output


command = 'git describe --tags --long --dirty'


def validate_version_format(dist, attr, value):
if dist.metadata.version:
# Probably called by write_version_files already
return
try:
version = check_output(command.split()).decode('utf-8').strip()
except:
Expand All @@ -15,6 +19,41 @@ def validate_version_format(dist, attr, value):
dist.metadata.version = version


def write_version_files(dist, attr, value):
"""Write the version in one or more files

If file ends in __version__.py, write: "__version__ = 'VERSION'", else just
dump the version in the file.

Only change files if it would be different
"""
if not dist.metadata.version:
if dist.version_format:
# Force evaluation of version
validate_version_format(dist, 'version_format', dist.version_format)
else:
raise DistutilsSetupError("'version_format' must be defined for %r to work" % attr)

if isinstance(value, basestring):
value = [value, ]
for path in value:
if path.endswith('__version__.py'):
content = "__version__ = %r\n" % dist.metadata.version
else:
content = dist.metadata.version
overwrite = False
try:
with open(path) as reader:
current_content = reader.read()
if current_content != content:
overwrite = True
except:
overwrite = True
if overwrite:
with open(path, 'w') as writer:
writer.write(content)


def format_version(version, fmt):
parts = version.split('-')
assert len(parts) in (3, 4)
Expand Down