aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/version.py
blob: 92b14b9addf28c8dbb66350b835a97de30105a75 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import subprocess

# The actual version string. For precompiled binaries, this will be changed to include the build
# tag, e.g. "3.0.0.dev0042-0xcafeabc"
VERSION = "4.0.0"
PATHOD = "pathod " + VERSION
MITMPROXY = "mitmproxy " + VERSION

# Serialization format version. This is displayed nowhere, it just needs to be incremented by one
# for each change in the file format.
FLOW_FORMAT_VERSION = 7


def get_version(dev: bool = False, build: bool = False, refresh: bool = False) -> str:
    """
    Return a detailed version string, sourced either from a hardcoded VERSION constant
    or obtained dynamically using git.

    Args:
        dev: If True, non-tagged releases will include a ".devXXXX" suffix, where XXXX is the number
             of commits since the last tagged release.
        build: If True, non-tagged releases will include a "-0xXXXXXXX" suffix, where XXXXXXX are
               the first seven digits of the commit hash.
        refresh: If True, always try to use git instead of a potentially hardcoded constant.
    """

    mitmproxy_version = VERSION

    if "dev" in VERSION and not refresh:
        pass  # There is a hardcoded build tag, so we just use what's there.
    elif dev or build:
        here = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
        try:
            git_describe = subprocess.check_output(
                ['git', 'describe', '--long'],
                stderr=subprocess.STDOUT,
                cwd=here,
            )
            last_tag, tag_dist, commit = git_describe.decode().strip().rsplit("-", 2)
            commit = commit.lstrip("g")[:7]
            tag_dist = int(tag_dist)
        except Exception:
            pass
        else:
            # Remove current suffix
            mitmproxy_version = mitmproxy_version.split(".dev")[0]

            # Add suffix for non-tagged releases
            if tag_dist > 0:
                mitmproxy_version += ".dev{tag_dist}".format(tag_dist=tag_dist)
                # The wheel build tag (we use the commit) must start with a digit, so we include "0x"
                mitmproxy_version += "-0x{commit}".format(commit=commit)

    if not dev:
        mitmproxy_version = mitmproxy_version.split(".dev")[0]
    elif not build:
        mitmproxy_version = mitmproxy_version.split("-0x")[0]

    return mitmproxy_version


if __name__ == "__main__":  # pragma: no cover
    print(VERSION)