summaryrefslogtreecommitdiff
path: root/patchwork/version.py
blob: 834e0f67922b8e2742e4258996a5c9e7e662da61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Stephen Finucane <stephen@that.guru>
#
# SPDX-License-Identifier: GPL-2.0-or-later

import subprocess
import os


ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                        os.pardir)


def get_latest_version(version):
    """Returns the most recent version available.

    This is either the hard-coded version or, if using Git, the version
    per the most recent Git tag.
    """
    git_version = format_git_version(get_raw_git_version())
    str_version = format_version(version)

    return git_version or str_version


def format_version(version):
    """Format version tuple."""
    return '.'.join(['.'.join([str(x) for x in version[:3]]),
                     '-'.join([str(x) for x in version[3:]])])


def format_git_version(version):
    """Returns a version based on Git tags."""
    if '-' in version:  # after tag
        # convert version-N-githash to version.postN-githash
        return version.replace('-', '.post', 1)
    else:  # at tag
        return version


def get_raw_git_version():
    """Returns the raw git version via 'git-describe'."""
    try:
        git_version = subprocess.check_output(['git', 'describe'],
                                              stderr=subprocess.STDOUT,
                                              cwd=ROOT_DIR)
    except (OSError, subprocess.CalledProcessError):
        return ''

    return git_version.strip().decode('utf-8')