|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import urllib.request |
| 4 | +import subprocess |
| 5 | + |
| 6 | +import yaml |
| 7 | +from InquirerPy import inquirer |
| 8 | + |
| 9 | +def get_zephyr_revision(): |
| 10 | + try: |
| 11 | + with open("west.yml", "r") as f: |
| 12 | + content = f.read() |
| 13 | + |
| 14 | + try: |
| 15 | + west_config = yaml.safe_load(content) |
| 16 | + projects = west_config.get("manifest", {}).get("projects", []) |
| 17 | + for project in projects: |
| 18 | + if project.get("name") == "zephyr": |
| 19 | + return project.get("revision", "main") |
| 20 | + except Exception: |
| 21 | + # Fallback to regex if YAML fails (e.g. jinja tags) |
| 22 | + import re |
| 23 | + # Look for revision: <something> |
| 24 | + # This is a simple heuristic. |
| 25 | + match = re.search(r"revision:\s*(.+)", content) |
| 26 | + if match: |
| 27 | + rev = match.group(1).strip() |
| 28 | + # If it's a jinja tag, default to main |
| 29 | + if "{{" in rev: |
| 30 | + return "main" |
| 31 | + return rev |
| 32 | + |
| 33 | + except Exception as e: |
| 34 | + print(f"Error reading west.yml: {e}") |
| 35 | + sys.exit(1) |
| 36 | + return "main" |
| 37 | + |
| 38 | +def fetch_upstream_manifest(revision): |
| 39 | + url = f"https://raw.githubusercontent.com/zephyrproject-rtos/zephyr/{revision}/west.yml" |
| 40 | + print(f"Fetching upstream west.yml from {url}...") |
| 41 | + try: |
| 42 | + with urllib.request.urlopen(url) as response: |
| 43 | + return yaml.safe_load(response.read()) |
| 44 | + except Exception as e: |
| 45 | + print(f"Error fetching upstream west.yml: {e}") |
| 46 | + if revision != "main": |
| 47 | + print("Retrying with 'main'...") |
| 48 | + return fetch_upstream_manifest("main") |
| 49 | + return None |
| 50 | + |
| 51 | +def get_modules_from_manifest(manifest): |
| 52 | + if not manifest: |
| 53 | + return [] |
| 54 | + |
| 55 | + projects = manifest.get("manifest", {}).get("projects", []) |
| 56 | + # Filter out zephyr itself |
| 57 | + return [p["name"] for p in projects if p.get("name") != "zephyr"] |
| 58 | + |
| 59 | +def select_modules(modules): |
| 60 | + if not modules: |
| 61 | + print("No modules found in upstream manifest.") |
| 62 | + return [] |
| 63 | + |
| 64 | + |
| 65 | + print("\nInteractive Module Selection") |
| 66 | + |
| 67 | + try: |
| 68 | + selected_modules = inquirer.fuzzy( |
| 69 | + message="Select Zephyr modules to include:", |
| 70 | + choices=modules, |
| 71 | + multiselect=True, |
| 72 | + max_height="70%", |
| 73 | + instruction="Type to search, TAB/Space to select, ENTER to confirm", |
| 74 | + ).execute() |
| 75 | + except Exception as e: |
| 76 | + print(f"Error during selection: {e}") |
| 77 | + return [] |
| 78 | + |
| 79 | + print(f"\nSelected {len(selected_modules)} modules.") |
| 80 | + return selected_modules |
| 81 | + |
| 82 | +def update_west_yml(selected_modules): |
| 83 | + # We use simple string replacement for the allowlist to avoid reformatting the whole file with PyYAML |
| 84 | + # which might lose comments or formatting. |
| 85 | + if not selected_modules: |
| 86 | + print("No modules selected (or all selected). Removing name-allowlist to include everything.") |
| 87 | + |
| 88 | + try: |
| 89 | + with open("west.yml", "r") as f: |
| 90 | + lines = f.readlines() |
| 91 | + |
| 92 | + with open("west.yml", "w") as f: |
| 93 | + skip_allowlist = False |
| 94 | + for line in lines: |
| 95 | + if "name-allowlist:" in line: |
| 96 | + if not selected_modules: |
| 97 | + # If we want all modules, we skip writing the allowlist block |
| 98 | + skip_allowlist = True |
| 99 | + continue |
| 100 | + else: |
| 101 | + f.write(line) |
| 102 | + # Write our new list |
| 103 | + for module in selected_modules: |
| 104 | + f.write(f" - {module}\n") |
| 105 | + skip_allowlist = True # Skip the old list |
| 106 | + continue |
| 107 | + |
| 108 | + if skip_allowlist: |
| 109 | + stripped = line.strip() |
| 110 | + # If line starts with -, it's a list item. |
| 111 | + # If it's empty or comment, we might keep it or skip it. |
| 112 | + # If it unindents, we stop skipping. |
| 113 | + # But indentation is hard to track line by line without state. |
| 114 | + # The template has: |
| 115 | + # - cmsis_6 |
| 116 | + # - ... |
| 117 | + # Next line is usually end of file or next section. |
| 118 | + # We assume allowlist is the last thing in the import block or followed by less indentation. |
| 119 | + # Simple heuristic: if it starts with - and has same indentation, skip. |
| 120 | + # Or just skip until we see something that doesn't look like a list item. |
| 121 | + if stripped.startswith("-"): |
| 122 | + continue |
| 123 | + else: |
| 124 | + skip_allowlist = False |
| 125 | + |
| 126 | + f.write(line) |
| 127 | + |
| 128 | + except Exception as e: |
| 129 | + print(f"Error updating west.yml: {e}") |
| 130 | + |
| 131 | +def main(): |
| 132 | + |
| 133 | + print("Setting up Zephyr modules...") |
| 134 | + revision = get_zephyr_revision() |
| 135 | + print(f"Detected Zephyr revision: {revision}") |
| 136 | + |
| 137 | + manifest = fetch_upstream_manifest(revision) |
| 138 | + |
| 139 | + if manifest: |
| 140 | + modules = get_modules_from_manifest(manifest) |
| 141 | + selected = select_modules(modules) |
| 142 | + update_west_yml(selected) |
| 143 | + |
| 144 | + # Self-destruct |
| 145 | + try: |
| 146 | + os.remove(__file__) |
| 147 | + except: |
| 148 | + pass |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments