@@ -105,9 +105,43 @@ def wait_for_udp(host: str, port: int, timeout: float) -> bool:
105105 return False
106106
107107
108- def clear_cache (cache_dir : str ) -> None :
108+ def default_cache_dir () -> Optional [Path ]:
109+ xdg = os .environ .get ("XDG_CACHE_HOME" )
110+ if xdg :
111+ return Path (xdg ) / "trakx"
112+ home = Path .home ()
113+ if home :
114+ return home / ".cache" / "trakx"
115+ return None
116+
117+
118+ def read_cache_path (config_path : str , repo_root : Path ) -> Optional [Path ]:
119+ try :
120+ with open (config_path , "r" , encoding = "utf-8" ) as fh :
121+ for line in fh :
122+ stripped = line .strip ()
123+ if not stripped or stripped .startswith ("#" ):
124+ continue
125+ if stripped .startswith ("cache:" ):
126+ value = stripped [len ("cache:" ) :].strip ()
127+ if not value :
128+ return default_cache_dir ()
129+ value = value .strip ("\" '" )
130+ path = Path (value ).expanduser ()
131+ if not path .is_absolute ():
132+ path = repo_root / path
133+ return path
134+ except OSError :
135+ return default_cache_dir ()
136+ return default_cache_dir ()
137+
138+
139+ def clear_cache (cache_dir : Optional [Path ]) -> None :
109140 if not cache_dir :
110141 return
142+ cache_dir = cache_dir .resolve ()
143+ if cache_dir == Path ("/" ):
144+ return
111145 shutil .rmtree (cache_dir , ignore_errors = True )
112146
113147
@@ -132,6 +166,8 @@ def main() -> int:
132166
133167 listen_host , listen_port = parse_hostport (args .listen )
134168 env_base = os .environ .copy ()
169+ env_base .pop ("TRAKX_CACHE" , None )
170+ cache_dir = read_cache_path (args .config , repo_root )
135171
136172 server_sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
137173 server_sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
@@ -161,9 +197,7 @@ def main() -> int:
161197 env ["TRAKX_HTTP_ROUTINES" ] = str (http_routines )
162198
163199 _ = run_cmd ([args .trakx_bin , "--config" , args .config , "stop" ], env = env )
164- cache_dir = env .get ("TRAKX_CACHE" )
165- if cache_dir :
166- clear_cache (cache_dir )
200+ clear_cache (cache_dir )
167201
168202 start = run_cmd ([args .trakx_bin , "--config" , args .config , "start" ], env = env )
169203 if start .returncode != 0 :
0 commit comments