...
1#!/hint/python3
2
3import re
4import subprocess
5from typing import Any, List
6from os import getenv
7import subprocess
8
9from .gitutil import git_check_clean as git_check_clean # Stop mypy complaining about implicit reexport
10from .gitutil import parse_bool as parse_bool # Stop mypy complaining about implicit reexport
11from .gitutil import git_add as git_add # Stop mypy complaining about implicit reexport
12from .uiutil import run_txtcapture
13
14# These are some regular expressions to validate and parse
15# X.Y.Z[-rc.N] versions.
16re_rc = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)-rc\.([0-9]+)$')
17re_ga = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)$')
18re_ea = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)-ea$')
19vX = 1
20vY = 2
21vZ = 3
22vN = 4
23
24DEFAULT_REPO = "emissary-ingress/emissary"
25
26
27def base_version(release_version: str) -> str:
28 """Given 'X.Y.Z[-rc.N]', return 'X.Y'."""
29 return build_version(release_version).rsplit(sep='.', maxsplit=1)[0]
30
31
32def build_version(release_version: str) -> str:
33 """Given 'X.Y.Z[-rc.N]', return 'X.Y.Z'."""
34 return release_version.split('-')[0]
35
36
37def assert_eq(actual: Any, expected: Any) -> None:
38 """`assert_eq(a, b)` is like `assert a == b`, but has a useful error
39 message when they're not equal.
40 """
41 if actual != expected:
42 raise AssertionError(f"wanted '{expected}', got '{actual}'")
43
44
45def get_is_private() -> bool:
46 """Return whether we're in a "private" Git checkout, for doing
47 embargoed work.
48 """
49 remote_names = run_txtcapture(['git', 'remote']).split()
50 remote_urls: List[str] = []
51 for remote_name in remote_names:
52 remote_urls += run_txtcapture(['git', 'remote', 'get-url', '--all', remote_name]).split()
53 return 'private' in "\n".join(remote_urls)
54
55
56def get_gh_repo() -> str:
57 remote_url = run_txtcapture(['git', 'remote', 'get-url', 'origin']).strip()
58 re_repo = re.compile(r'github\.com[:\/]([a-z\d-]+\/[a-z]+)(\.git)?$')
59 m = re_repo.search(remote_url)
60 if not m:
61 raise Exception(f"Could not find repo from {remote_url}")
62 return m[1]
View as plain text