This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdater.py
More file actions
65 lines (57 loc) · 2.13 KB
/
updater.py
File metadata and controls
65 lines (57 loc) · 2.13 KB
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
import requests
import re
import os
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.styles import Style
import pyperclip # pylint: disable=import-error
from packaging import version
from gitcommit.style import style, Ansi
def get_github_tags():
resp = requests.get("https://api.github.com/repos/nebbles/gitcommit/tags")
if resp.status_code == requests.codes["ok"]:
tags_json = resp.json()
tags = [tag["name"] for tag in tags_json]
return tags
else:
print("Error fetching tags from GitHub")
resp.raise_for_status()
def find_version():
dir_path = os.path.dirname(os.path.realpath(__file__))
version_file_path = os.path.join(dir_path, "__version__.py")
with open(version_file_path, "r") as f:
version_file = f.read()
version_match = re.search(
r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M
)
if version_match:
version = version_match.group(1)
return version
raise RuntimeError("Unable to find version string in __version__.py.")
def check_for_update(verbose=False):
try:
tags = get_github_tags()
except Exception as e:
print(
"An error occured whilst checking for updates. Check your network connection."
)
return
latest_tag_version = tags[0][1:]
cur_version = find_version()
if version.parse(cur_version) < version.parse(latest_tag_version):
Ansi.print_ok("There is an update available for conventional-commit.")
upgrade_command = "pip install --upgrade conventional-commit"
pyperclip.copy(upgrade_command)
text = FormattedText(
[
("", "Version "),
("class:red", cur_version),
("", " → "),
("class:green", latest_tag_version),
("", "\nAdded to your clipboard: "),
("class:command", upgrade_command),
]
)
print_formatted_text(text, style=style)
elif verbose:
print("Current version:", cur_version)