#!/usr/bin/env python3 """Resolve the machine-local Vivado install path. The install path differs per machine (C:\\Xilinx here, D:\\Xilinx elsewhere), so it is never hardcoded in a skill. It lives in ~/skills_env.md, which stays out of git. Precedence: explicit --vivado-dir > OS env var > ~/skills_env.md. """ import os import re import sys def env_lookup(key, default=None): """Read KEY from the OS environment, else from a `KEY = value` line in ~/skills_env.md. Trailing `# comments` are stripped.""" v = os.environ.get(key) if v: return v path = os.path.join(os.path.expanduser("~"), "skills_env.md") try: with open(path, encoding="utf-8") as f: for line in f: m = re.match(rf"\s*{re.escape(key)}\s*=\s*(.+?)\s*(?:#.*)?$", line) if m: val = m.group(1).strip() # Placeholders like `<未安装>` mean "not configured on this machine". if val and not val.startswith("<"): return val except OSError: pass return default def resolve_vivado_dir(cli_value=None): """Vivado install ROOT (the dir containing bin/vivado.bat).""" if cli_value: return cli_value root = env_lookup("VIVADO_ROOT") if root: return root exe = env_lookup("VIVADO_PATH") if exe: # ...//bin/vivado.bat -> return os.path.dirname(os.path.dirname(exe)) sys.exit( "Vivado path not configured. Add `VIVADO_ROOT = ` to " "~/skills_env.md, set it as an env var, or pass --vivado-dir." ) def resolve_vivado_exe(cli_value=None): """Full path to bin/vivado.bat.""" if cli_value: return cli_value exe = env_lookup("VIVADO_PATH") if exe: return exe return os.path.join(resolve_vivado_dir(), "bin", "vivado.bat") def resolve_sdk_dir(cli_value=None): """Xilinx SDK install ROOT (the dir containing bin/bootgen.bat). Precedence matches the Vivado resolver: explicit CLI value, SDK_ROOT, SDK_PATH, then the same keys in ~/skills_env.md. """ if cli_value: return cli_value root = env_lookup("SDK_ROOT") if root: return root exe = env_lookup("SDK_PATH") if exe: return os.path.dirname(os.path.dirname(exe)) sys.exit( "Xilinx SDK path not configured. Add `SDK_ROOT = ` to " "~/skills_env.md, set it as an env var, or pass --sdk-dir." )