Skip to content

Commit

Permalink
Improve sandbox key parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Sep 26, 2023
1 parent 1443e26 commit e300819
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
6 changes: 5 additions & 1 deletion law/contrib/cms/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def __init__(self, sandbox_name=None, proxy=None, instance=None, threads=1):
sandbox_name = cfg.get_expanded("job", "crab_sandbox_name")

# create the cmssw sandbox
self.cmssw_sandbox = Sandbox.new("cmssw::{}".format(sandbox_name))
self.cmssw_sandbox = Sandbox.new(
sandbox_name
if sandbox_name.startswith("cmssw::")
else "cmssw::{}".format(sandbox_name),
)

# store attributes
self.proxy = proxy
Expand Down
32 changes: 20 additions & 12 deletions law/sandbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,38 @@ class Sandbox(six.with_metaclass(ABCMeta, object)):
# cached envs
_envs = {}

@staticmethod
def check_key(key, silent=False):
@classmethod
def check_key(cls, key, silent=False):
# commas are not allowed since the LAW_SANDBOX env variable is allowed to contain multiple
# comma-separated sandbox keys that need to be separated
valid = "," not in key

if not valid and not silent:
if "," in key:
if silent:
return False
raise ValueError("invalid sandbox key format '{}'".format(key))

return valid
return True

@staticmethod
def split_key(key):
parts = str(key).split(Sandbox.delimiter, 1)
@classmethod
def split_key(cls, key):
parts = str(key).split(cls.delimiter, 1)
if len(parts) != 2 or any(not p.strip() for p in parts):
raise ValueError("invalid sandbox key '{}'".format(key))

return tuple(parts)

@staticmethod
def join_key(_type, name):
@classmethod
def remove_type(cls, key):
# check for key format
cls.check_key(key)

# remove leading type if present
return key.split(cls.delimiter, 1)[-1]

@classmethod
def join_key(cls, _type, name):
""" join_key(type, name)
"""
return str(_type) + Sandbox.delimiter + str(name)
return str(_type) + cls.delimiter + str(name)

@classmethod
def new(cls, key, *args, **kwargs):
Expand Down

0 comments on commit e300819

Please sign in to comment.