Skip to content

Commit

Permalink
Consistent naming of utils in arc contrib.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Aug 30, 2023
1 parent d277954 commit d56380d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
12 changes: 6 additions & 6 deletions law/contrib/arc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
"""

__all__ = [
"get_arc_proxy_file", "get_arc_proxy_user", "get_arc_proxy_lifetime", "get_arc_proxy_vo",
"check_arc_proxy_validity", "renew_arc_proxy",
"get_arcproxy_file", "get_arcproxy_user", "get_arcproxy_lifetime", "get_arcproxy_vo",
"check_arcproxy_validity", "renew_arcproxy",
"ARCJobManager", "ARCJobFileFactory", "ARCWorkflow",
"ensure_arc_proxy",
"ensure_arcproxy",
]


# provisioning imports
from law.contrib.arc.util import (
get_arc_proxy_file, get_arc_proxy_user, get_arc_proxy_lifetime, get_arc_proxy_vo,
check_arc_proxy_validity, renew_arc_proxy,
get_arcproxy_file, get_arcproxy_user, get_arcproxy_lifetime, get_arcproxy_vo,
check_arcproxy_validity, renew_arcproxy,
)
from law.contrib.arc.job import ARCJobManager, ARCJobFileFactory
from law.contrib.arc.workflow import ARCWorkflow
from law.contrib.arc.decorator import ensure_arc_proxy
from law.contrib.arc.decorator import ensure_arcproxy
8 changes: 4 additions & 4 deletions law/contrib/arc/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
Decorators for task methods for convenient working with ARC.
"""

__all__ = ["ensure_arc_proxy"]
__all__ = ["ensure_arcproxy"]


from law.decorator import factory
from law.contrib.arc import check_arc_proxy_validity
from law.contrib.arc import check_arcproxy_validity


@factory(accept_generator=True)
def ensure_arc_proxy(fn, opts, task, *args, **kwargs):
def ensure_arcproxy(fn, opts, task, *args, **kwargs):
"""
Decorator for law task methods that checks the validity of the arc proxy and throws an
exception in case it is invalid. This can prevent late errors on remote worker notes that except
arc proxies to be present. Accepts generator functions.
"""
def before_call():
# check the proxy validity
if not check_arc_proxy_validity():
if not check_arcproxy_validity():
raise Exception("arc proxy not valid")

return None
Expand Down
59 changes: 30 additions & 29 deletions law/contrib/arc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""

__all__ = [
"get_arc_proxy_file", "get_arc_proxy_user", "get_arc_proxy_lifetime", "get_arc_proxy_vo",
"check_arc_proxy_validity", "renew_arc_proxy",
"get_arcproxy_file", "get_arcproxy_user", "get_arcproxy_lifetime", "get_arcproxy_vo",
"check_arcproxy_validity", "renew_arcproxy",
]


Expand All @@ -21,31 +21,32 @@
logger = get_logger(__name__)


def get_arc_proxy_file():
def get_arcproxy_file():
"""
Returns the path to the arc proxy file.
"""
if "X509_USER_PROXY" in os.environ:
return os.environ["X509_USER_PROXY"]
else:
# resolution order as in http://www.nordugrid.org/documents/arc-ce-sysadm-guide.pdf
tmp = "/tmp"
for v in ["TMPDIR", "TMP", "TEMP"]:
if os.getenv(v):
tmp = os.environ[v]
break
return os.path.join(tmp, "x509up_u{}".format(os.getuid()))

# resolution order as in http://www.nordugrid.org/documents/arc-ce-sysadm-guide.pdf
tmp = "/tmp"
for v in ["TMPDIR", "TMP", "TEMP"]:
if os.getenv(v):
tmp = os.environ[v]
break

return os.path.join(tmp, "x509up_u{}".format(os.getuid()))


def _arc_proxy_info(args=None, proxy_file=None, silent=False):
def _arcproxy_info(args=None, proxy_file=None, silent=False):
if args is None:
args = ["--info"]
cmd = ["arcproxy"] + (args or [])

# when proxy_file is None, get the default
# when empty string, don't add a --proxy argument
if proxy_file is None:
proxy_file = get_arc_proxy_file()
proxy_file = get_arcproxy_file()
if proxy_file:
proxy_file = os.path.expandvars(os.path.expanduser(proxy_file))
cmd.extend(["--proxy", proxy_file])
Expand All @@ -63,52 +64,52 @@ def _arc_proxy_info(args=None, proxy_file=None, silent=False):
return code, out, err


def get_arc_proxy_user(proxy_file=None):
def get_arcproxy_user(proxy_file=None):
"""
Returns the owner of the arc proxy. When *proxy_file* is *None*, it defaults to the result of
:py:func:`get_arc_proxy_file`. Otherwise, when it evaluates to *False*, ``arcproxy`` is queried
:py:func:`get_arcproxy_file`. Otherwise, when it evaluates to *False*, ``arcproxy`` is queried
without a custom proxy file.
"""
out = _arc_proxy_info(args=["--infoitem=identity"], proxy_file=proxy_file)[1].strip()
out = _arcproxy_info(args=["--infoitem=identity"], proxy_file=proxy_file)[1].strip()
try:
return re.match(r".*\/CN\=([^\/]+).*", out.strip()).group(1)
except:
raise Exception("no valid identity found in arc proxy: {}".format(out))


def get_arc_proxy_lifetime(proxy_file=None):
def get_arcproxy_lifetime(proxy_file=None):
"""
Returns the remaining lifetime of the arc proxy in seconds. When *proxy_file* is *None*, it
defaults to the result of :py:func:`get_arc_proxy_file`. Otherwise, when it evaluates to
defaults to the result of :py:func:`get_arcproxy_file`. Otherwise, when it evaluates to
*False*, ``arcproxy`` is queried without a custom proxy file.
"""
out = _arc_proxy_info(args=["--infoitem=validityLeft"], proxy_file=proxy_file)[1].strip()
out = _arcproxy_info(args=["--infoitem=validityLeft"], proxy_file=proxy_file)[1].strip()
try:
return int(out)
except:
raise Exception("no valid lifetime found in arc proxy: {}".format(out))


def get_arc_proxy_vo(proxy_file=None):
def get_arcproxy_vo(proxy_file=None):
"""
Returns the virtual organization name of the arc proxy. When *proxy_file* is *None*, it defaults
to the result of :py:func:`get_arc_proxy_file`. Otherwise, when it evaluates to *False*,
to the result of :py:func:`get_arcproxy_file`. Otherwise, when it evaluates to *False*,
``arcproxy`` is queried without a custom proxy file.
"""
return _arc_proxy_info(args=["--infoitem=vomsVO"], proxy_file=proxy_file)[1].strip()
return _arcproxy_info(args=["--infoitem=vomsVO"], proxy_file=proxy_file)[1].strip()


def check_arc_proxy_validity(log=False, proxy_file=None):
def check_arcproxy_validity(log=False, proxy_file=None):
"""
Returns *True* when a valid arc proxy exists, *False* otherwise. When *log* is *True*, a
warning will be logged. When *proxy_file* is *None*, it defaults to the result of
:py:func:`get_arc_proxy_file`. Otherwise, when it evaluates to *False*, ``arcproxy`` is queried
:py:func:`get_arcproxy_file`. Otherwise, when it evaluates to *False*, ``arcproxy`` is queried
without a custom proxy file.
"""
code, out, err = _arc_proxy_info(proxy_file=proxy_file, silent=True)
code, out, err = _arcproxy_info(proxy_file=proxy_file, silent=True)

if code == 0:
valid = get_arc_proxy_lifetime(proxy_file=proxy_file) > 0
valid = get_arcproxy_lifetime(proxy_file=proxy_file) > 0
elif err.strip().lower().startswith("error: cannot find file at"):
valid = False
else:
Expand All @@ -120,20 +121,20 @@ def check_arc_proxy_validity(log=False, proxy_file=None):
return valid


def renew_arc_proxy(password="", lifetime="8 days", proxy_file=None):
def renew_arcproxy(password="", lifetime="8 days", proxy_file=None):
"""
Renews the arc proxy using a password *password* and a default *lifetime* of 8 days, which is
internally parsed by :py:func:`law.util.parse_duration` where the default input unit is hours.
To ensure that the *password* it is not visible in any process listing, it is written to a
temporary file first and piped into the ``arcproxy`` command. When *proxy_file* is *None*, it
defaults to the result of :py:func:`get_arc_proxy_file`. Otherwise, when it evaluates to
defaults to the result of :py:func:`get_arcproxy_file`. Otherwise, when it evaluates to
*False*, ``arcproxy`` is invoked without a custom proxy file.
"""
# convert the lifetime to seconds
lifetime_seconds = int(parse_duration(lifetime, input_unit="h", unit="s"))

if proxy_file is None:
proxy_file = get_arc_proxy_file()
proxy_file = get_arcproxy_file()

args = "--constraint=validityPeriod={}".format(lifetime_seconds)
if proxy_file:
Expand Down
4 changes: 2 additions & 2 deletions law/workflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def _cache_branches(self):
# deprecation warning until v0.1
logger.warning(
"accessing {0}._cache_branches is deprecated, use {0}.cache_branch_map instead".format(
self.__class.__name,
self.__class__.__name__,
),
)
return self._cache_branch_map
Expand All @@ -729,7 +729,7 @@ def _cache_branches(self):
def _cache_branches(self, cache_branches):
logger.warning(
"setting {0}._cache_branches is deprecated, use {0}.cache_branch_map instead".format(
self.__class.__name,
self.__class__.__name__,
),
)
self._cache_branch_map = cache_branches
Expand Down

0 comments on commit d56380d

Please sign in to comment.