-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
80 lines (65 loc) · 2.34 KB
/
version.py
File metadata and controls
80 lines (65 loc) · 2.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
import yaml
import subprocess
from datetime import datetime
def load_version_config(config_file="version.cfg"):
try:
with open(config_file, "r", encoding="utf-8") as f:
return yaml.safe_load(f) or {}
except FileNotFoundError:
print(f"⚠️ Version configuration not found: {config_file}. "
"It's okay, it's optional.")
return {}
except Exception as e:
print(f"❌ Error reading {config_file}: {e}")
return {}
def generate_version_badge(version, badge_dir=".badges"):
try:
os.makedirs(badge_dir, exist_ok=True)
badge_content = {
"schemaVersion": 1,
"label": "version",
"message": version,
"color": "blue"
}
badge_file = os.path.join(badge_dir, "version.json")
with open(badge_file, "w", encoding="utf-8") as f:
json.dump(badge_content, f, ensure_ascii=False, indent=2)
print(f"🏷️ Badge created: {badge_file}")
except Exception as e:
print(f"❌ Error creating badge: {e}")
def update_version_file(config):
version_file = config.get("file", "version.txt")
version_mode = config.get("mode", "manual")
version = None
if version_mode == "commits":
try:
output = subprocess.check_output(
["git", "rev-list", "--count", "HEAD"],
stderr=subprocess.DEVNULL)
commit_count = int(output.decode().strip())
commit_count += 1
version = f"{commit_count:011,}"
# version = str(commit_count + 1)
except Exception as e:
print(f"⚠️ Error getting number of commits: {e}. "
"Or there are no commits.")
# return None
commit_count = 1
version = f"{commit_count:011,}"
elif version_mode == "date":
version = datetime.now().strftime("%Y%m%d%H%M%S")
else:
return None
try:
with open(version_file, "w", encoding="utf-8") as f:
f.write(version)
print(f"🆙 Updated version: {version}"
f" → saved in {version_file}")
except Exception as e:
print(f"❌ Error writing {version_file}: {e}")
return None
if os.path.isdir(".badges"):
generate_version_badge(version)
return version