From 6b6b71fcbc6aa2606e9e01df302383aba0f25da3 Mon Sep 17 00:00:00 2001 From: mathieu longtin Date: Mon, 10 Aug 2015 11:18:03 -0400 Subject: [PATCH] Added version_files --- README.rst | 44 +++++++++++++++++++++++++++++++++++++++ setup.py | 3 ++- setuptools_git_version.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index b8afd15..68abc91 100644 --- a/README.rst +++ b/README.rst @@ -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 ++++++++++++++++++ diff --git a/setup.py b/setup.py index 475c56a..e21159e 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -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 """, ) diff --git a/setuptools_git_version.py b/setuptools_git_version.py index cfd990c..b45fe4b 100644 --- a/setuptools_git_version.py +++ b/setuptools_git_version.py @@ -1,4 +1,5 @@ from pkg_resources import get_distribution +from distutils.errors import DistutilsSetupError from subprocess import check_output @@ -6,6 +7,9 @@ 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: @@ -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)