-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbcVersion.py
104 lines (85 loc) · 3.15 KB
/
bcVersion.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import platform
import os
import sys
import shutil
import subprocess
def current_version():
# try to get the current tag from git...
version = get_git_tag()
# if not, try to find version.txt
# We do all this MEIPASS juggling for pyinstall madness
#
# version.txt gets written/bundled by tools/build_windows.bat
if not version:
try:
base_path = getattr(sys, '_MEIPASS', '')
if not base_path:
base_path = os.path.dirname(os.path.abspath(__file__))
filename = f"{base_path}/version.txt"
with open(filename, 'r') as file:
version = file.read().strip()
except:
pass
if not version:
version = "<No version found>"
return version
def get_git_tag(path=None):
if not path: path = os.path.curdir
tag = None
# we'll try asl git, mostly just for me. If that breaks, try just plain git
command = 'git describe --tags'
if wsl_available():
try: tag = do_git("wsl " + command, path)
except: pass
if not tag:
try: tag = do_git(command, path)
except: pass
return tag
def do_git(command, path):
tag = None
proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE, cwd=path)
out = proc.stdout
if out :
tag = out.read().decode('utf-8')
if tag:
return tag.strip()
### two functions below from https://github.com/scivision/detect-windows-subsystem-for-linux
#
# Copyright (c) 2023 scivision
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def is_wsl(v: str = platform.uname().release) -> int:
### detects if Python is running in WSL
if v.endswith("-Microsoft"):
return 1
elif v.endswith("microsoft-standard-WSL2"):
return 2
return 0
def wsl_available() -> int:
### detect if Windows Subsystem for Linux is available from Windows
if os.name != "nt" or not shutil.which("wsl"):
return False
try:
return is_wsl(
subprocess.check_output(
["wsl", "uname", "-r"], text=True, timeout=15
).strip()
)
except subprocess.SubprocessError:
return False