Skip to content

Commit f5e6fa6

Browse files
feat: LockException (snakemake#1276)
* add LockException * add tests * try older mamba Co-authored-by: Johannes Köster <[email protected]>
1 parent bf2d1b9 commit f5e6fa6

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

snakemake/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,17 @@ def __init__(self, rule, targetfile):
530530

531531
class CacheMissException(Exception):
532532
pass
533+
534+
535+
class LockException(WorkflowError):
536+
def __init__(self):
537+
super().__init__(
538+
"Error: Directory cannot be locked. Please make "
539+
"sure that no other Snakemake process is trying to create "
540+
"the same files in the following directory:\n{}\n"
541+
"If you are sure that no other "
542+
"instances of snakemake are running on this directory, "
543+
"the remaining lock was likely caused by a kill signal or "
544+
"a power loss. It can be removed with "
545+
"the --unlock argument.".format(os.getcwd())
546+
)

snakemake/persistence.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from itertools import filterfalse, count
1616
from pathlib import Path
1717

18+
import snakemake.exceptions
1819
from snakemake.logging import logger
1920
from snakemake.jobs import jobfiles
2021
from snakemake.utils import listfiles
@@ -167,7 +168,7 @@ def lock_warn_only(self):
167168

168169
def lock(self):
169170
if self.locked:
170-
raise IOError("Another snakemake process " "has locked this directory.")
171+
raise snakemake.exceptions.LockException()
171172
self._lock(self.all_inputfiles(), "input")
172173
self._lock(self.all_outputfiles(), "output")
173174

snakemake/workflow.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -798,20 +798,7 @@ def files(items):
798798
dag.update_checkpoint_dependencies()
799799
dag.check_dynamic()
800800

801-
try:
802-
self.persistence.lock()
803-
except IOError:
804-
logger.error(
805-
"Error: Directory cannot be locked. Please make "
806-
"sure that no other Snakemake process is trying to create "
807-
"the same files in the following directory:\n{}\n"
808-
"If you are sure that no other "
809-
"instances of snakemake are running on this directory, "
810-
"the remaining lock was likely caused by a kill signal or "
811-
"a power loss. It can be removed with "
812-
"the --unlock argument.".format(os.getcwd())
813-
)
814-
return False
801+
self.persistence.lock()
815802

816803
if cleanup_shadow:
817804
self.persistence.cleanup_shadow()

tests/testapi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,17 @@ def test_dicts_in_config():
8080
"test": {"this_dict": "shoult_not_either"},
8181
},
8282
)
83+
84+
def test_lockexception():
85+
from snakemake.persistence import Persistence
86+
from snakemake.exceptions import LockException
87+
88+
persistence = Persistence()
89+
persistence.all_inputfiles = lambda: ["A.txt"]
90+
persistence.all_outputfiles = lambda: ["B.txt"]
91+
persistence.lock()
92+
try:
93+
persistence.lock()
94+
except LockException as e:
95+
return True
96+
assert False

0 commit comments

Comments
 (0)