Skip to content

Commit a1309bb

Browse files
committed
Add version info to main command
1 parent ffd5a15 commit a1309bb

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

twitchio/__main__.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
MIT License
3+
4+
Copyright (c) 2017 - Present PythonistaGuild
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
import argparse
26+
import platform
27+
import re
28+
import sys
29+
30+
import aiohttp
31+
32+
33+
try:
34+
import starlette
35+
36+
starlette_version = starlette.__version__
37+
except ImportError:
38+
starlette_version = "Not Installed/Not Found"
39+
40+
try:
41+
import uvicorn
42+
43+
uvicorn_version = uvicorn.__version__
44+
except ImportError:
45+
uvicorn_version = "Not Installed/Not Found"
46+
47+
48+
parser = argparse.ArgumentParser(prog="twitchio")
49+
parser.add_argument("--version", action="store_true", help="Get version and debug information for TwitchIO.")
50+
51+
args = parser.parse_args()
52+
53+
54+
def _get_version() -> str:
55+
version = ""
56+
with open("twitchio/__init__.py") as f:
57+
match = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)
58+
59+
if not match or not match.group(1):
60+
raise RuntimeError("Version is not set")
61+
62+
version = match.group(1)
63+
64+
if version.endswith(("dev", "a", "b", "rc")):
65+
# append version identifier based on commit count
66+
try:
67+
import subprocess
68+
69+
p = subprocess.Popen(["git", "rev-list", "--count", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
70+
out, _ = p.communicate()
71+
if out:
72+
version += out.decode("utf-8").strip()
73+
p = subprocess.Popen(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
74+
out, _ = p.communicate()
75+
if out:
76+
version += "+g" + out.decode("utf-8").strip()
77+
except Exception:
78+
pass
79+
80+
return version
81+
82+
83+
def version_info() -> None:
84+
python_info = "\n".join(sys.version.split("\n"))
85+
86+
info: str = f"""
87+
twitchio : {_get_version()}
88+
aiohttp : {aiohttp.__version__}
89+
90+
Python:
91+
- {python_info}
92+
System:
93+
- {platform.platform()}
94+
Extras:
95+
- Starlette : {starlette_version}
96+
- Uvicorn : {uvicorn_version}
97+
"""
98+
99+
print(info)
100+
101+
102+
if args.version:
103+
version_info()

0 commit comments

Comments
 (0)