Skip to content

Commit

Permalink
Add rlocks to guard checksum creation in contrib bundle tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Aug 19, 2023
1 parent a0c559c commit 04fedbf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
29 changes: 19 additions & 10 deletions law/contrib/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import os
import threading
import subprocess
from abc import abstractmethod

Expand Down Expand Up @@ -43,6 +44,7 @@ def __init__(self, *args, **kwargs):
super(BundleGitRepository, self).__init__(*args, **kwargs)

self._checksum = None
self._checksum_lock = threading.RLock()

@abstractmethod
def get_repo_path(self):
Expand All @@ -53,16 +55,23 @@ def checksum(self):
if self.custom_checksum != NO_STR:
return self.custom_checksum

if self._checksum is None:
cmd = [rel_path(__file__, "scripts", "repository_checksum.sh"), self.get_repo_path()]
cmd = quote_cmd(cmd)

code, out, _ = interruptable_popen(cmd, shell=True, executable="/bin/bash",
stdout=subprocess.PIPE)
if code != 0:
raise Exception("repository checksum calculation failed")

self._checksum = out.strip()
with self._checksum_lock:
if self._checksum is None:
cmd = quote_cmd([
rel_path(__file__, "scripts", "repository_checksum.sh"),
self.get_repo_path(),
])

code, out, _ = interruptable_popen(
cmd,
shell=True,
executable="/bin/bash",
stdout=subprocess.PIPE,
)
if code != 0:
raise Exception("repository checksum calculation failed")

self._checksum = out.strip()

return self._checksum

Expand Down
30 changes: 20 additions & 10 deletions law/contrib/mercurial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import os
import threading
import subprocess
from abc import abstractmethod

Expand All @@ -18,7 +19,7 @@
from law.target.local import LocalFileTarget
from law.parameter import NO_STR, CSVParameter
from law.decorator import log
from law.util import rel_path, interruptable_popen
from law.util import rel_path, interruptable_popen, quote_cmd


class BundleMercurialRepository(Task):
Expand All @@ -43,6 +44,7 @@ def __init__(self, *args, **kwargs):
super(BundleMercurialRepository, self).__init__(*args, **kwargs)

self._checksum = None
self._checksum_lock = threading.RLock(())

@abstractmethod
def get_repo_path(self):
Expand All @@ -53,15 +55,23 @@ def checksum(self):
if self.custom_checksum != NO_STR:
return self.custom_checksum

if self._checksum is None:
checksum_script = rel_path(__file__, "scripts", "repository_checksum.sh")
cmd = [checksum_script, self.get_repo_path()]

code, out, _ = interruptable_popen(cmd, stdout=subprocess.PIPE)
if code != 0:
raise Exception("repository checksum calculation failed")

self._checksum = out.strip()
with self._checksum_lock:
if self._checksum is None:
cmd = quote_cmd([
rel_path(__file__, "scripts", "repository_checksum.sh"),
self.get_repo_path(),
])

code, out, _ = interruptable_popen(
cmd,
shell=True,
executable="/bin/bash",
stdout=subprocess.PIPE,
)
if code != 0:
raise Exception("repository checksum calculation failed")

self._checksum = out.strip()

return self._checksum

Expand Down

0 comments on commit 04fedbf

Please sign in to comment.