File size: 1,737 Bytes
e202b16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
#
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import subprocess
from pathlib import Path
from typing import Optional

# TODO: consolidate with the code in build_conda.py
THIS_PATH = Path(__file__).resolve()
version_from_file = (THIS_PATH.parents[1] / "version.txt").read_text().strip()


def get_tagged_version() -> Optional[str]:
    """
    Return whether we are at an exact version (namely the version variable).
    """
    try:
        tag = subprocess.check_output(
            ["git", "describe", "--tags", "--exact-match", "HEAD"],
            text=True,
            stderr=subprocess.DEVNULL,
        ).strip()
    except subprocess.CalledProcessError:  # no tag
        return None

    if not tag.startswith("v"):
        return None
    return tag[1:]


def get_dev_version() -> str:
    assert ".dev" not in version_from_file
    num_commits = subprocess.check_output(
        ["git", "rev-list", "--count", "HEAD"], text=True
    ).strip()
    return f"{version_from_file}.dev{num_commits}"


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--source", choices=["tag", "dev", "tag,dev"], required=False, default="tag,dev"
    )
    args = parser.parse_args()

    if "tag" in args.source:
        tagged_version = get_tagged_version()
        if args.source == "tag" and tagged_version is None:
            raise ValueError("No tag found")
    else:
        tagged_version = None
    if tagged_version is not None:
        print(tagged_version, end="")
    else:
        print(get_dev_version(), end="")