Skip to content
Snippets Groups Projects
__init__.py 2.44 KiB
Newer Older
# Copyright (c) 2021-2022 Neogeo-Technologies.
# 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.


default_app_config = 'onegeo_rproxy_mapstore2.apps.OnegeoRproxyMapstore2Config'


# from django/utils/version.py

import datetime  # noqa E402
import functools  # noqa E402
import logging  # noqa E402
import os  # noqa E402
import os.path  # noqa E402
import subprocess  # noqa E402


logger = logging.getLogger(__name__)


@functools.lru_cache()
def get_git_changeset():
    """Return a numeric identifier of the latest git changeset.
    The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
    This value isn't guaranteed to be unique, but collisions are very unlikely,
    so it's sufficient for generating the development version numbers.
    """
    # Repository may not be found if __file__ is undefined, e.g. in a frozen
    # module.
    if '__file__' not in globals():
        return None
    repo_dir = os.path.dirname(os.path.abspath(__file__))
    git_log = subprocess.run(
        'git log --pretty=format:%ct --quiet -1 HEAD',
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        shell=True, cwd=repo_dir, universal_newlines=True,
    )
    timestamp = git_log.stdout
    try:
        timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
    except ValueError as err:
        logger.exception(err)
        return ''
    return timestamp.strftime('%Y%m%d%H%M%S')


def get_version(version=None):
    assert len(version) == 5
    assert version[3] in ('dev', 'beta', 'rc', 'final')

    main = '.'.join(str(x) for x in version[:3])
    sub = ''
    if version[3] == 'dev' and version[4] == 0:
        git_changeset = get_git_changeset()
        if git_changeset:
            sub = '.dev%s' % git_changeset
    elif version[3] != 'final':
        mapping = {'beta': 'b', 'rc': 'rc'}
        sub = mapping[version[3]] + str(version[4])

    return main + sub


VERSION = (1, 0, 0, 'beta', 0)


__version__ = get_version(VERSION)