9
9
from types import MethodType
10
10
from typing import TYPE_CHECKING , Any , Callable , Generator , Iterator , cast
11
11
12
+ from filelock import FileLock
13
+
12
14
from tox .config .main import Config
13
15
from tox .config .sets import EnvConfigSet
14
16
@@ -31,29 +33,39 @@ def __str__(self) -> str:
31
33
return str (self .path )
32
34
33
35
34
- def _lock_method (lock : RLock , meth : Callable [..., Any ]) -> Callable [..., Any ]:
36
+ def _lock_method (thread_lock : RLock , file_lock : FileLock | None , meth : Callable [..., Any ]) -> Callable [..., Any ]:
35
37
def _func (* args : Any , ** kwargs : Any ) -> Any :
36
- with lock :
37
- return meth (* args , ** kwargs )
38
+ with thread_lock :
39
+ if file_lock is not None and file_lock .is_locked is False : # file_lock is to lock from other tox processes
40
+ file_lock .acquire ()
41
+ try :
42
+ return meth (* args , ** kwargs )
43
+ finally :
44
+ if file_lock is not None :
45
+ file_lock .release ()
38
46
39
47
return _func
40
48
41
49
42
50
class PackageToxEnv (ToxEnv , ABC ):
43
51
def __init__ (self , create_args : ToxEnvCreateArgs ) -> None :
44
- self ._lock = RLock ()
52
+ self ._thread_lock = RLock ()
53
+ self ._file_lock : FileLock | None = None
45
54
super ().__init__ (create_args )
46
55
self ._envs : set [str ] = set ()
47
56
48
57
def __getattribute__ (self , name : str ) -> Any :
49
58
# the packaging class might be used by multiple environments in parallel, hold a lock for operations on it
50
59
obj = object .__getattribute__ (self , name )
51
60
if isinstance (obj , MethodType ):
52
- obj = _lock_method (self ._lock , obj )
61
+ obj = _lock_method (self ._thread_lock , self . _file_lock , obj )
53
62
return obj
54
63
55
64
def register_config (self ) -> None :
56
65
super ().register_config ()
66
+ file_lock_path : Path = self .conf ["env_dir" ] / "file.lock"
67
+ self ._file_lock = FileLock (file_lock_path )
68
+ file_lock_path .parent .mkdir (parents = True , exist_ok = True )
57
69
self .core .add_config (
58
70
keys = ["package_root" , "setupdir" ],
59
71
of_type = Path ,
0 commit comments