-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_utils.py
More file actions
135 lines (110 loc) · 4.03 KB
/
build_utils.py
File metadata and controls
135 lines (110 loc) · 4.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Build utilities for version management and git operations."""
import os
import subprocess
from pathlib import Path
from typing import Optional
def get_musa_version_suffix() -> str:
"""Get MUSA version suffix for wheel name.
Returns:
MUSA version string like "mu436" for MUSA 4.3.6 or "mu-unknown".
"""
musa_path = os.environ.get("MUSA_PATH", "/usr/local/musa")
musa_header = os.path.join(musa_path, "include", "musa.h")
if not os.path.exists(musa_header):
return "mu-unknown"
try:
with open(musa_header, "r") as f:
for line in f:
if "MUSA_VERSION" in line and "define" in line:
parts = line.split()
if len(parts) >= 3 and parts[1] == "MUSA_VERSION":
try:
version_int = int(parts[2])
# Convert version integer (e.g., 50100 for 5.1.0) to suffix
major = version_int // 10000
minor = (version_int % 10000) // 100
patch = version_int % 100
return f"mu{major}{minor}{patch}"
except ValueError:
continue
except Exception:
pass
return "mu-unknown"
def get_git_version(cwd: Optional[Path] = None) -> str:
"""Get git commit hash (full).
Args:
cwd: Working directory for git command. If None, uses current directory.
Returns:
Git commit hash or "unknown" if git is not available.
"""
try:
git_version = (
subprocess.check_output(
["git", "rev-parse", "HEAD"],
cwd=cwd,
stderr=subprocess.DEVNULL,
)
.decode("ascii")
.strip()
)
return git_version
except Exception:
return "unknown"
def get_git_short_commit(cwd: Optional[Path] = None) -> Optional[str]:
"""Get short git commit hash (7 characters).
Args:
cwd: Working directory for git command. If None, uses current directory.
Returns:
Short commit hash (e.g., "g40c8139") or None if git is not available.
"""
try:
commit = (
subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=cwd,
stderr=subprocess.DEVNULL,
)
.decode("ascii")
.strip()
)
return f"g{commit}"
except Exception:
return None
def build_version_string(
base_version: str,
cwd: Optional[Path] = None,
dev_suffix: str = "",
local_version: Optional[str] = None,
) -> tuple[str, str]:
"""Build full version string with MUSA info (git version separate).
Args:
base_version: Base version from version.txt (e.g., "0.1.2")
cwd: Working directory for git commands
dev_suffix: Optional dev release suffix (e.g., "1" for .dev1)
local_version: Optional additional local version string
Returns:
Tuple of (full_version, git_version)
- full_version: e.g., "0.1.2+mu436" (no git commit in package name)
- git_version: full git commit hash for CLI display
"""
version = base_version
# Add dev suffix if specified
if dev_suffix:
version = f"{version}.dev{dev_suffix}"
# Get git version (full hash for metadata/CLI display)
git_version = get_git_version(cwd=cwd)
# Build local version parts.
# Note: git commit is NOT included in package name. The wheel local suffix
# still carries the MUSA SDK version plus any explicit local suffix.
local_parts = []
# Add MUSA version
musa_suffix = get_musa_version_suffix()
local_parts.append(musa_suffix)
# Append additional local version suffix if available
if local_version:
local_parts.append(local_version)
# Combine all parts
if local_parts:
local_version_str = "".join(local_parts)
version = f"{version}+{local_version_str}"
return version, git_version