ONE - On-device Neural Engine
Loading...
Searching...
No Matches
validate_global_conf Namespace Reference

Data Structures

class  _Action
 
class  _ArgSpec
 
class  ArgumentParser
 
class  DriverName
 
class  NormalOption
 
class  SchemaReport
 
class  TargetOption
 

Functions

 _install_onelib_shim ()
 
Tuple[List[_ArgSpec], SchemaReport_load_schema (str file_path)
 
bool _has_action (List[_ArgSpec] args, type action_type)
 
bool _has_any_name (List[_ArgSpec] args, List[str] names)
 
List[str] _check_codegen_contract (List[_ArgSpec] args)
 
Tuple[List[str], List[str]] _check_profile_contract (List[_ArgSpec] args)
 
Dict[str, str] _read_ini (str path)
 
 _resolve_paths (str root, str target, str backend, bool installed)
 
 main ()
 

Detailed Description

Validator for ONE global target configuration packages.

What this script checks:
1) Target INI existence & minimal required keys (TARGET, BACKEND).
2) Command schema files (codegen.py, profile.py) exist for BACKEND.
3) Command schema is importable without ONE runtime by shimming `onelib.argumentparse`.
4) "Required" arguments are present in schemas:
   - codegen: DriverName, TargetOption, input{,_path}, output{,_path}
   - profile: DriverName, TargetOption, input{,_path}

Usage:
  python tools/validate_global_conf.py --root . \
      --target {TARGET_NAME} --backend {BACKEND_NAME}

You can also point to the "installed" layout:
  python tools/validate_global_conf.py --installed \
      --target {TARGET_NAME} --backend {BACKEND_NAME}

Function Documentation

◆ _check_codegen_contract()

List[str] validate_global_conf._check_codegen_contract ( List[_ArgSpec args)
protected

Definition at line 157 of file validate_global_conf.py.

157def _check_codegen_contract(args: List[_ArgSpec]) -> List[str]:
158 errs = []
159 if not _has_action(args, DriverName):
160 errs.append("Missing DriverName action.")
161 if not _has_action(args, TargetOption):
162 errs.append("Missing TargetOption action.")
163 if not _has_any_name(args, ["input", "input_path"]):
164 errs.append("Missing input/input_path argument.")
165 if not _has_any_name(args, ["--output", "--output_path"]):
166 errs.append("Missing --output/--output_path option.")
167 return errs
168
169

References _has_action(), and _has_any_name().

Referenced by main().

◆ _check_profile_contract()

Tuple[List[str], List[str]] validate_global_conf._check_profile_contract ( List[_ArgSpec args)
protected

Definition at line 170 of file validate_global_conf.py.

170def _check_profile_contract(args: List[_ArgSpec]) -> Tuple[List[str], List[str]]:
171 errs, warns = [], []
172 if not _has_action(args, DriverName):
173 errs.append("Missing DriverName action.")
174 if not _has_action(args, TargetOption):
175 errs.append("Missing TargetOption action.")
176 if not _has_any_name(args, ["input", "input_path"]):
177 errs.append("Missing input/input_path argument.")
178 return errs, warns
179
180

References _has_action(), and _has_any_name().

Referenced by main().

◆ _has_action()

bool validate_global_conf._has_action ( List[_ArgSpec args,
type  action_type 
)
protected

Definition at line 145 of file validate_global_conf.py.

145def _has_action(args: List[_ArgSpec], action_type: type) -> bool:
146 return any(a.action is action_type for a in args)
147
148

Referenced by _check_codegen_contract(), and _check_profile_contract().

◆ _has_any_name()

bool validate_global_conf._has_any_name ( List[_ArgSpec args,
List[str]  names 
)
protected

Definition at line 149 of file validate_global_conf.py.

149def _has_any_name(args: List[_ArgSpec], names: List[str]) -> bool:
150 for a in args:
151 for n in a.names:
152 if n in names:
153 return True
154 return False
155
156

Referenced by _check_codegen_contract(), and _check_profile_contract().

◆ _install_onelib_shim()

validate_global_conf._install_onelib_shim ( )
protected

Definition at line 87 of file validate_global_conf.py.

87def _install_onelib_shim():
88 mod_onelib = types.ModuleType("onelib")
89 mod_argparse = types.ModuleType("onelib.argumentparse")
90 mod_argparse.ArgumentParser = ArgumentParser
91 mod_argparse.DriverName = DriverName
92 mod_argparse.TargetOption = TargetOption
93 mod_argparse.NormalOption = NormalOption
94
95 # Both import styles are used in examples:
96 # from onelib import argumentparse
97 # from onelib.argumentparse import DriverName, ...
98 sys.modules["onelib"] = mod_onelib
99 sys.modules["onelib.argumentparse"] = mod_argparse
100 mod_onelib.argumentparse = mod_argparse
101
102
103# -----------------------------
104# Utilities
105# -----------------------------
106@dataclass

Referenced by _load_schema().

◆ _load_schema()

Tuple[List[_ArgSpec], SchemaReport] validate_global_conf._load_schema ( str  file_path)
protected

Definition at line 113 of file validate_global_conf.py.

113def _load_schema(file_path: str) -> Tuple[List[_ArgSpec], SchemaReport]:
114 rep = SchemaReport(file_path=file_path, ok=False)
115 if not os.path.isfile(file_path):
116 rep.messages.append(f"Missing schema file: {file_path}")
117 return [], rep
118
119 _install_onelib_shim()
120 spec = importlib.util.spec_from_file_location("schema_module", file_path)
121 mod = importlib.util.module_from_spec(spec)
122 try:
123 assert spec and spec.loader
124 spec.loader.exec_module(mod) # type: ignore
125 except Exception as e:
126 rep.messages.append(f"Import error: {e}")
127 return [], rep
128 if not hasattr(mod, "command_schema"):
129 rep.messages.append("Schema module has no `command_schema()` function.")
130 return [], rep
131 try:
132 parser = mod.command_schema()
133 except Exception as e:
134 rep.messages.append(f"command_schema() execution failed: {e}")
135 return [], rep
136 if not isinstance(parser, ArgumentParser):
137 rep.messages.append(
138 "command_schema() did not return an ArgumentParser instance (shim).")
139 return [], rep
140
141 rep.ok = True
142 return parser.args, rep
143
144

References _install_onelib_shim().

Referenced by main().

◆ _read_ini()

Dict[str, str] validate_global_conf._read_ini ( str  path)
protected

Definition at line 181 of file validate_global_conf.py.

181def _read_ini(path: str) -> Dict[str, str]:
182 # configparser with ; as comment
183 parser = configparser.ConfigParser(strict=False,
184 interpolation=None,
185 delimiters=("=", ))
186 # Treat keys as case-sensitive; normalize ourselves by not lowercasing
187 parser.optionxform = str # preserve case
188 with io.open(path, "r", encoding="utf-8") as f:
189 # Place everything in DEFAULT to keep simple key=value structure
190 content = "[DEFAULT]\n" + f.read()
191 parser.read_string(content)
192 return dict(parser["DEFAULT"])
193
194

Referenced by main().

◆ _resolve_paths()

validate_global_conf._resolve_paths ( str  root,
str  target,
str  backend,
bool  installed 
)
protected

Definition at line 195 of file validate_global_conf.py.

195def _resolve_paths(root: str, target: str, backend: str, installed: bool):
196 if installed:
197 target_ini = f"/usr/share/one/target/{target}.ini"
198 codegen_py = f"/usr/share/one/backends/command/{backend}/codegen.py"
199 profile_py = f"/usr/share/one/backends/command/{backend}/profile.py"
200 else:
201 target_ini = os.path.join(root, "target", target, f"{target}.ini")
202 codegen_py = os.path.join(root, "backend", backend, "one-cmds", "codegen.py")
203 profile_py = os.path.join(root, "backend", backend, "one-cmds", "profile.py")
204 return target_ini, codegen_py, profile_py
205
206

Referenced by main().

◆ main()

validate_global_conf.main ( void  )

Definition at line 207 of file validate_global_conf.py.

207def main():
208 ap = argparse.ArgumentParser(
209 description="Validate ONE global target configuration package.")
210 ap.add_argument("--root", default=".", help="Repo root (for source-tree validation).")
211 ap.add_argument("--target", required=True, help="Target name, e.g., Rose")
212 ap.add_argument("--backend", required=True, help="Backend name, e.g., dummy")
213 ap.add_argument("--installed",
214 action="store_true",
215 help="Validate installed paths instead of source tree.")
216 args = ap.parse_args()
217
218 target_ini, codegen_py, profile_py = _resolve_paths(args.root, args.target,
219 args.backend, args.installed)
220
221 print("== ONE Global Conf Validator ==")
222 print(f"Mode : {'installed' if args.installed else 'source-tree'}")
223 print(f"Target INI: {target_ini}")
224 print(f"Codegen : {codegen_py}")
225 print(f"Profile : {profile_py}")
226 print("")
227
228 # 1) INI
229 if not os.path.isfile(target_ini):
230 print(f"[ERROR] Target INI not found: {target_ini}")
231 return 2
232 kv = _read_ini(target_ini)
233 errors = []
234 for k in ("TARGET", "BACKEND"):
235 if k not in kv or not kv[k].strip():
236 errors.append(f"Missing required key {k} in INI.")
237 if errors:
238 for e in errors:
239 print(f"[ERROR] {e}")
240 return 2
241
242 target_val = kv["TARGET"].strip()
243 backend_val = kv["BACKEND"].strip()
244 if target_val != args.target:
245 print(f"[WARN] TARGET in INI is '{target_val}' but --target is '{args.target}'")
246 if backend_val != args.backend:
247 print(
248 f"[WARN] BACKEND in INI is '{backend_val}' but --backend is '{args.backend}'")
249
250 # 2) Load schemas
251 codegen_args, codegen_rep = _load_schema(codegen_py)
252 profile_args, profile_rep = _load_schema(profile_py)
253
254 ok = True
255
256 def print_report(name: str, rep: SchemaReport):
257 nonlocal ok
258 status = "OK" if rep.ok else "FAIL"
259 print(f"[{name}] {rep.file_path}: {status}")
260 for m in rep.messages:
261 print(f" - {m}")
262 if not rep.ok:
263 ok = False
264
265 print_report("SCHEMA", codegen_rep)
266 print_report("SCHEMA", profile_rep)
267
268 # 3) Contracts
269 if codegen_rep.ok:
270 cerrs = _check_codegen_contract(codegen_args)
271 for e in cerrs:
272 print(f"[ERROR] codegen schema: {e}")
273 ok = ok and not cerrs
274 if profile_rep.ok:
275 perrs, pwrns = _check_profile_contract(profile_args)
276 for e in perrs:
277 print(f"[ERROR] profile schema: {e}")
278 for w in pwrns:
279 print(f"[WARN] profile schema: {w}")
280 ok = ok and not perrs
281
282 print("")
283 if ok:
284 print("[PASS] Validation succeeded.")
285 return 0
286 else:
287 print("[FAIL] Validation failed.")
288 return 2
289
290
int main(void)

References _check_codegen_contract(), _check_profile_contract(), _load_schema(), _read_ini(), _resolve_paths(), and main().

Referenced by main().