forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.py
executable file
·82 lines (74 loc) · 2.33 KB
/
version.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import subprocess
def get_version_info_from_git(repo_path, extra_args=[]):
if "CP_VERSION" in os.environ:
git_tag = os.environ["CP_VERSION"]
else:
# Note: git describe doesn't work if no tag is available
try:
git_tag = subprocess.check_output(
# CIRCUITPY-CHANGE
[
"git",
"describe",
"--dirty",
"--tags",
"--always",
"--match",
"[1-9].*",
*extra_args,
],
cwd=repo_path,
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
# CIRCUITPY-CHANGE
except subprocess.CalledProcessError as er:
if er.returncode == 128:
# git exit code of 128 means no repository found
return None
git_tag = ""
except OSError:
return None
try:
# CIRCUITPY-CHANGE
git_hash = subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=repo_path,
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
except subprocess.CalledProcessError:
# CIRCUITPY-CHANGE
git_hash = "unknown"
except OSError:
return None
# CIRCUITPY-CHANGE
try:
# Check if there are any modified files.
subprocess.check_call(
["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"],
cwd=repo_path,
stderr=subprocess.STDOUT,
)
# Check if there are any staged files.
subprocess.check_call(
["git", "diff-index", "--cached", "--quiet", "HEAD", "--"],
cwd=repo_path,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError:
git_hash += "-dirty"
except OSError:
return None
# CIRCUITPY-CHANGE
# Try to extract MicroPython version from git tag
ver = git_tag.split("-")[0].split(".")
return git_tag, git_hash, ver
if __name__ == "__main__":
import pathlib
import sys
git_tag, _, _ = get_version_info_from_git(pathlib.Path("."), sys.argv[1:])
if git_tag is None:
sys.exit(-1)
print(git_tag)