1
# Copyright (C) 2010-2012 Linaro Limited
3
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
5
# This file is part of versiontools.
7
# versiontools is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License version 3
9
# as published by the Free Software Foundation
11
# versiontools is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public License
17
# along with versiontools. If not, see <http://www.gnu.org/licenses/>.
20
versiontools.setuptools_hooks
21
=============================
23
Plugins for setuptools that add versintools features.
25
Setuptools has a framework where external packages, such as versiontools, can
26
hook into setup.py metadata and commands. We use this feature to intercept
27
special values of the ``version`` keyword argument to ``setup()``. This
28
argument handled by the following method:
33
from distutils.errors import DistutilsSetupError
34
from versiontools import Version, _get_exception_message
37
def version(dist, attr, value):
39
Handle the ``version`` keyword to setuptools.setup()
42
This function is normally called by setuptools, it is advertised in the
43
entry points of versiontools as setuptools extension. There is no need
48
# We need to look at dist.metadata.version to actually see the version
49
# that was passed to setup. Something in between does not seem to like our
50
# version string and we get 0 here, odd.
52
value = dist.metadata.version
53
if sys.version_info[:1] < (3,):
54
isstring = lambda string: isinstance(string, basestring)
56
isstring = lambda string: isinstance(string, str)
57
if not (isstring(value)
58
and value.startswith(":versiontools:")):
60
# Peel away the magic tag
61
value = value[len(":versiontools:"):]
63
# Lookup the version object
64
version = Version.from_expression(value)
65
# Update distribution metadata
66
dist.metadata.version = str(version)
68
message = _get_exception_message(*sys.exc_info())
69
if message.startswith(": "):
71
raise DistutilsSetupError(message)