Repository navigation
Expand file tree
/
Copy pathupdate_ze_api_version.py
More file actions
executable file
·137 lines (108 loc) · 4.68 KB
/
Copy pathupdate_ze_api_version.py
File metadata and controls
executable file
·137 lines (108 loc) · 4.68 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
136
137
#!/usr/bin/env python3
"""
Copyright (C) 2026 Intel Corporation
SPDX-License-Identifier: MIT
Script to update ZE_API_VERSION_CURRENT value in a new dump file to match the old dump file.
This ensures ABI compatibility by reverting the version to the previous value.
"""
import re
import sys
import argparse
def get_ze_api_version_current_value(dump_file_path, name="ZE_API_VERSION_CURRENT"):
"""Extract the named *_API_VERSION_CURRENT value from a dump file."""
try:
with open(dump_file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern to match the version and its value
pattern = r"'name'\s*=>\s*'%s',\s*\n\s*'value'\s*=>\s*'(\d+)'" % name
match = re.search(pattern, content)
if match:
return match.group(1)
else:
print(f"Error: Could not find {name} in {dump_file_path}")
return None
except FileNotFoundError:
print(f"Error: File {dump_file_path} not found")
return None
except Exception as e:
print(f"Error reading {dump_file_path}: {e}")
return None
def update_ze_api_version_current(dump_file_path, old_value, new_value, name="ZE_API_VERSION_CURRENT"):
"""Update the named *_API_VERSION_CURRENT value in the dump file."""
try:
with open(dump_file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern to match the version and replace its value
pattern = r"('name'\s*=>\s*'%s',\s*\n\s*'value'\s*=>\s*')(\d+)(')" % name
def replace_value(match):
if match.group(2) == new_value:
return match.group(1) + old_value + match.group(3)
return match.group(0) # No change if value doesn't match expected
updated_content = re.sub(pattern, replace_value, content)
# Check if any replacement was made
if updated_content == content:
print(f"Warning: No replacement made. Expected to find value '{new_value}' but it was not found.")
return False
# Write the updated content back to the file
with open(dump_file_path, 'w', encoding='utf-8') as f:
f.write(updated_content)
print(f"Successfully updated {name} from '{new_value}' to '{old_value}' in {dump_file_path}")
return True
except Exception as e:
print(f"Error updating {dump_file_path}: {e}")
return False
def main():
parser = argparse.ArgumentParser(
description="Update ZE_API_VERSION_CURRENT in new dump file to match old dump file"
)
parser.add_argument(
"old_dump",
help="Path to the old dump file (reference for the target value)"
)
parser.add_argument(
"new_dump",
help="Path to the new dump file (to be modified)"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be changed without actually modifying the file"
)
args = parser.parse_args()
reset_version(args, "ZE_API_VERSION_CURRENT")
# Sysman has its own enum since #576. Older base dumps do not have it.
zes = "'ZES_API_VERSION_CURRENT'"
if all(zes in open(p, encoding='utf-8').read() for p in (args.old_dump, args.new_dump)):
reset_version(args, "ZES_API_VERSION_CURRENT")
def reset_version(args, name):
"""Set name in the new dump to its value in the old dump."""
# Get the version value from the old dump
old_value = get_ze_api_version_current_value(args.old_dump, name)
if old_value is None:
sys.exit(1)
# Get the version value from the new dump
new_value = get_ze_api_version_current_value(args.new_dump, name)
if new_value is None:
sys.exit(1)
print(f"Old dump ({args.old_dump}) {name} value: {old_value}")
print(f"New dump ({args.new_dump}) {name} value: {new_value}")
if old_value == new_value:
print("Values are already the same. No update needed.")
return
if args.dry_run:
print(f"DRY RUN: Would update {name} from '{new_value}' to '{old_value}' in {args.new_dump}")
return
# Perform the update
success = update_ze_api_version_current(args.new_dump, old_value, new_value, name)
if success:
# Verify the change
updated_value = get_ze_api_version_current_value(args.new_dump, name)
if updated_value == old_value:
print("Verification successful: Value has been updated correctly.")
else:
print(f"Verification failed: Expected '{old_value}', but found '{updated_value}'")
sys.exit(1)
else:
sys.exit(1)
if __name__ == "__main__":
main()