@@ -92,31 +92,65 @@ def _setup_temp_dir(self):
9292 raise ValueError (f"Temp directory does not exist: { self .temp_dir } " )
9393 self .temp_dir = os .path .abspath (self .temp_dir )
9494 else :
95- # Try to detect temp directory
96- temp_dirs = [
97- os .environ .get ("TMPDIR" ),
98- "/tmp" ,
99- os .environ .get ("XDG_RUNTIME_DIR" ),
100- ]
101- for target in temp_dirs :
102- if target and os .path .exists (target ):
95+ # Detect temp directory if not provided, platform dependent
96+ if os .name == "nt" : # Windows
97+ # Windows temp directory selection logic
98+ temp_dirs = [
99+ os .environ .get ("TEMP" ),
100+ os .environ .get ("TMP" ),
101+ os .environ .get ("SYSTEMROOT" ) + "\\ Temp" ,
102+ os .path .expanduser ("~" ), # user profile directory
103+ "C:\\ " ,
104+ ]
105+ # Remove None values from the list
106+ temp_dirs = [d for d in temp_dirs if d is not None ]
107+ # Try to create temp directory in candidate locations
108+ for base_dir in temp_dirs :
103109 try :
104- # Check if it's mounted on tmpfs
105- result = subprocess .run (
106- ["df" , "-P" , "-t" , "tmpfs" , target ],
107- capture_output = True ,
108- text = True ,
110+ # Try to create temp directory in this location
111+ self .temp_dir = tempfile .mkdtemp (
112+ prefix = "lua-helper-" , dir = base_dir
109113 )
110- if result .returncode == 0 :
111- self .temp_dir = target
112- break
113- except Exception :
114+ break
115+ except (OSError , IOError ):
116+ # Failed to create in this location, try next
114117 continue
115- if not self .temp_dir :
116- self .temp_dir = "/tmp"
117-
118- # Create unique temp directory
119- self .temp_dir = tempfile .mkdtemp (prefix = "lua-helper-" , dir = self .temp_dir )
118+ else :
119+ # If we get here, all locations failed
120+ raise RuntimeError (
121+ "Unable to create temporary directory in any candidate location on Windows"
122+ )
123+ else :
124+ # Locations for linux and other OS:
125+ temp_dirs = [
126+ os .environ .get ("TMPDIR" ),
127+ "/tmp" ,
128+ os .environ .get ("XDG_RUNTIME_DIR" ),
129+ ]
130+ # Remove None values from the list
131+ temp_dirs = [d for d in temp_dirs if d is not None ]
132+ # Selection for linux and other OS, try to choose tmp dir mounted on tmpfs:
133+ for target in temp_dirs :
134+ if target and os .path .exists (target ):
135+ try :
136+ # Check if it's mounted on tmpfs
137+ result = subprocess .run (
138+ ["df" , "-P" , "-t" , "tmpfs" , target ],
139+ capture_output = True ,
140+ text = True ,
141+ )
142+ if result .returncode == 0 :
143+ self .temp_dir = target
144+ break
145+ except Exception :
146+ continue
147+ if not self .temp_dir :
148+ self .temp_dir = "/tmp"
149+ # Create unique temp directory
150+ self .temp_dir = tempfile .mkdtemp (
151+ prefix = "lua-helper-" , dir = self .temp_dir
152+ )
153+ # Create data storage directories in selected temp dir
120154 self .meta_dir = os .path .join (self .temp_dir , "meta" )
121155 self .data_dir = os .path .join (self .temp_dir , "data" )
122156 os .makedirs (self .meta_dir )
0 commit comments