From 77c18c1d6980bb4f7119eb1b75753de95611a315 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Fri, 31 Jul 2020 03:56:21 +0500 Subject: [PATCH] export a __version__ variable for python module (#5309) * export a __version__ variable for python module * assign version to be used below * better support python2 * Add copyright header --- python/flatbuffers/__init__.py | 1 + python/flatbuffers/_version.py | 17 +++++++++++++++++ python/setup.py | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 python/flatbuffers/_version.py diff --git a/python/flatbuffers/__init__.py b/python/flatbuffers/__init__.py index 5e45eb796..74dc7ee58 100644 --- a/python/flatbuffers/__init__.py +++ b/python/flatbuffers/__init__.py @@ -15,4 +15,5 @@ from .builder import Builder from .table import Table from .compat import range_func as compat_range +from ._version import __version__ from . import util diff --git a/python/flatbuffers/_version.py b/python/flatbuffers/_version.py new file mode 100644 index 000000000..a44e900a4 --- /dev/null +++ b/python/flatbuffers/_version.py @@ -0,0 +1,17 @@ +# Copyright 2019 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Placeholder, to be updated during the release process +# by the setup.py +__version__ = u"latest" diff --git a/python/setup.py b/python/setup.py index b3fc56658..0615f2b8b 100644 --- a/python/setup.py +++ b/python/setup.py @@ -12,18 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. +import fileinput import os +import re +import sys from datetime import datetime from setuptools import setup +def _update_version_attr(new_version): + for line in fileinput.input('flatbuffers/_version.py', inplace=True): + if line.startswith('__version__'): + line = re.sub(r'".*"', '"{}"'.format(new_version), line) + sys.stdout.write(line) + + def version(): version = os.getenv('VERSION', None) if version: # Most git tags are prefixed with 'v' (example: v1.2.3) this is # never desirable for artifact repositories, so we strip the # leading 'v' if it's present. - return version[1:] if version.startswith('v') else version + version = version[1:] if version.startswith('v') else version else: # Default version is an ISO8601 compiliant datetime. PyPI doesn't allow # the colon ':' character in its versions, and time is required to allow @@ -39,8 +49,11 @@ def version(): print("VERSION environment variable not set, using datetime instead: {}" .format(version)) + _update_version_attr(version) + return version + setup( name='flatbuffers', version=version(),