26
26
import os
27
27
import shutil
28
28
29
+ from functools import cached_property
29
30
from pathlib import Path
30
31
from time import sleep
31
32
from typing import TYPE_CHECKING
32
33
33
34
from molecule import scenarios , util
34
35
from molecule .constants import RC_TIMEOUT
36
+ from molecule .text import checksum
35
37
36
38
37
39
if TYPE_CHECKING :
@@ -119,7 +121,7 @@ def directory(self) -> str:
119
121
path = Path (self .config .molecule_file ).parent
120
122
return str (path )
121
123
122
- @property
124
+ @cached_property
123
125
def ephemeral_directory (self ) -> str :
124
126
"""Acquire the ephemeral directory.
125
127
@@ -129,22 +131,17 @@ def ephemeral_directory(self) -> str:
129
131
Raises:
130
132
SystemExit: If lock cannot be acquired before timeout.
131
133
"""
132
- path : str | Path | None = os . getenv ( "MOLECULE_EPHEMERAL_DIRECTORY" , None )
133
- if not path :
134
+ path : Path
135
+ if "MOLECULE_EPHEMERAL_DIRECTORY" not in os . environ :
134
136
project_directory = Path (self .config .project_directory ).name
135
137
136
138
if self .config .is_parallel :
137
139
project_directory = f"{ project_directory } -{ self .config ._run_uuid } " # noqa: SLF001
138
140
139
- project_scenario_directory = Path (
140
- self .config .cache_directory ,
141
- project_directory ,
142
- self .name ,
143
- )
144
- path = ephemeral_directory (project_scenario_directory )
145
-
146
- if isinstance (path , str ):
147
- path = Path (path )
141
+ project_scenario_directory = f"molecule.{ checksum (project_directory , 4 )} .{ self .name } "
142
+ path = self .config .runtime .cache_dir / "tmp" / project_scenario_directory
143
+ else :
144
+ path = Path (os .getenv ("MOLECULE_EPHEMERAL_DIRECTORY" , "" ))
148
145
149
146
if os .environ .get ("MOLECULE_PARALLEL" , False ) and not self ._lock :
150
147
lock_file = path / ".lock"
@@ -165,7 +162,7 @@ def ephemeral_directory(self) -> str:
165
162
LOG .warning ("Timedout trying to acquire lock on %s" , path )
166
163
raise SystemExit (RC_TIMEOUT )
167
164
168
- return str ( path )
165
+ return path . absolute (). as_posix ( )
169
166
170
167
@property
171
168
def inventory_directory (self ) -> str :
@@ -314,36 +311,3 @@ def _setup(self) -> None:
314
311
inventory = Path (self .inventory_directory )
315
312
if not inventory .is_dir ():
316
313
inventory .mkdir (exist_ok = True , parents = True )
317
-
318
-
319
- def ephemeral_directory (path : Path | None = None ) -> Path :
320
- """Return temporary directory to be used by molecule.
321
-
322
- Molecule users should not make any assumptions about its location,
323
- permissions or its content as this may change in future release.
324
-
325
- Args:
326
- path: Ephemeral directory name.
327
-
328
- Returns:
329
- The full ephemeral directory path.
330
-
331
- Raises:
332
- RuntimeError: If ephemeral directory location cannot be determined
333
- """
334
- d : str | Path | None = os .getenv ("MOLECULE_EPHEMERAL_DIRECTORY" )
335
- if not d :
336
- d = os .getenv ("XDG_CACHE_HOME" , Path ("~/.cache" ).expanduser ())
337
- if not d :
338
- msg = "Unable to determine ephemeral directory to use."
339
- raise RuntimeError (msg )
340
-
341
- if isinstance (d , str ):
342
- d = Path (d )
343
- d = d .resolve () / (path if path else "molecule" )
344
-
345
- if not d .is_dir ():
346
- os .umask (0o077 )
347
- d .mkdir (mode = 0o700 , parents = True , exist_ok = True )
348
-
349
- return d
0 commit comments