44
55import shutil
66import subprocess
7+ import sys
78from collections .abc import Generator
89from contextlib import redirect_stdout
910from io import StringIO
2324_WEST_STAGING_PATH = ".zmk"
2425_WEST_CONFIG_PATH = ".west/config"
2526
27+ # Don't clone projects from ZMK's manifest that aren't needed for discovering keyboards
28+ _PROJECT_BLOCKLIST = [
29+ "lvgl" ,
30+ "zephyr" ,
31+ "zmk-studio-messages" ,
32+ ]
33+
2634
2735def is_repo (path : Path ) -> bool :
2836 """Get whether a path is a ZMK config repo."""
@@ -186,7 +194,9 @@ def ensure_west_ready(self) -> None:
186194 self ._update_west_manifest ()
187195
188196 config_path = self .path / _WEST_STAGING_PATH / _WEST_CONFIG_PATH
189- if not config_path .exists ():
197+ if config_path .exists ():
198+ self ._update_project_filter ()
199+ else :
190200 self ._init_west_app ()
191201
192202 self ._west_ready = True
@@ -201,14 +211,15 @@ def _run_west(self, *args: str, capture_output: Literal[True]) -> str: ...
201211 def _run_west (self , * args : str , capture_output : bool ) -> str | None : ...
202212
203213 def _run_west (self , * args : str , capture_output = False ):
214+ command = [sys .executable , "-m" , "west" , * args ]
215+
204216 if capture_output :
205- with redirect_stdout ( StringIO ()) as output :
206- self .run_west ( * args , capture_output = False )
207- return output . getvalue ( )
217+ return subprocess . check_output (
218+ command , cwd = self .west_path , text = True , stderr = subprocess . STDOUT
219+ )
208220
209- with set_directory (self .west_path ):
210- west_main (args )
211- return None
221+ subprocess .check_call (command , cwd = self .west_path )
222+ return None
212223
213224 def _update_gitignore (self ):
214225 gitignore = self .path / ".gitignore"
@@ -249,18 +260,23 @@ def _init_west_app(self):
249260 print ("Initializing west application. This may take a while..." )
250261 self ._run_west ("init" , "-l" , _CONFIG_DIR_NAME )
251262
252- # Don't clone zephyr, because it's not necessary to discover keyboards.
253- self ._run_west ("config" , "--local" , "manifest.project-filter" , " -zephyr" )
254-
263+ self ._update_project_filter ()
255264 self ._run_west ("update" )
256265
266+ def _get_project_filter (self ):
267+ try :
268+ return self ._run_west (
269+ "config" , "--local" , "manifest.project-filter" , capture_output = True
270+ ).strip ()
271+ except subprocess .CalledProcessError :
272+ return ""
257273
258- def _run_west (path : Path , args : list [str ], capture_output = False ):
259- if capture_output :
260- with redirect_stdout (StringIO ()) as output :
261- _run_west (path , args , capture_output = False )
262- return output .getvalue ()
274+ def _update_project_filter (self ):
275+ current_filter = self ._get_project_filter ()
263276
264- with set_directory (path ):
265- west_main (args )
266- return None
277+ new_filter = "," .join ("-" + project for project in _PROJECT_BLOCKLIST )
278+
279+ if current_filter != new_filter :
280+ self ._run_west (
281+ "config" , "--local" , "manifest.project-filter" , "--" , new_filter
282+ )
0 commit comments