コード例 #1
0
    def serialize(self, value):
        """"""
        if not value:
            value = 0

        value_seconds = parse_duration(value, input_unit=self.unit, unit="s")
        return human_duration(seconds=value_seconds, colon_format=True)
コード例 #2
0
def renew_arc_proxy(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
    *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()

    args = "--constraint=validityPeriod={}".format(lifetime_seconds)
    if proxy_file:
        proxy_file = os.path.expandvars(os.path.expanduser(proxy_file))
        args += " --proxy={}".format(proxy_file)

    with tmp_file() as (_, tmp):
        with open(tmp, "w") as f:
            f.write(password)

        cmd = "arcproxy --passwordsource=key=file:{} {}".format(tmp, args)
        code, out, _ = interruptable_popen(cmd,
                                           shell=True,
                                           executable="/bin/bash",
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT)

        if code != 0:
            raise Exception("arcproxy failed: {}".format(out))
コード例 #3
0
ファイル: util.py プロジェクト: riga/law
def renew_voms_proxy(password="", vo=None, lifetime="8 days", proxy_file=None):
    """
    Renews the voms proxy using a password *password*, an optional virtual organization name *vo*,
    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* is not visible in any process listing, it is written to a temporary file first and
    piped into the ``voms-proxy-init`` command. When *proxy_file* is *None*, it defaults to the
    result of :py:func:`get_voms_proxy_file`.
    """
    # parse and format the lifetime
    lifetime_seconds = max(parse_duration(lifetime, input_unit="h", unit="s"),
                           60.0)
    lifetime = human_duration(seconds=lifetime_seconds, colon_format="h")
    # cut the seconds part
    normalized = ":".join((2 - lifetime.count(":")) * ["00"] + [""]) + lifetime
    lifetime = ":".join(normalized.rsplit(":", 3)[-3:-1])

    # when proxy_file is None, get the default
    # when empty string, don't add a --out argument
    if proxy_file is None:
        proxy_file = get_voms_proxy_file()

    with tmp_file() as (_, tmp):
        with open(tmp, "w") as f:
            f.write(password)

        cmd = "cat '{}' | voms-proxy-init --valid '{}'".format(tmp, lifetime)
        if vo:
            cmd += " -voms '{}'".format(vo)
        if proxy_file:
            proxy_file = os.path.expandvars(os.path.expanduser(proxy_file))
            cmd += " --out '{}'".format(proxy_file)

        code, out, _ = interruptable_popen(cmd,
                                           shell=True,
                                           executable="/bin/bash",
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT)

        if code != 0:
            raise Exception("voms-proxy-init failed: {}".format(out))
コード例 #4
0
 def get_time(section, option):
     value = cfg.get_expanded(section, option)
     return parse_duration(value, input_unit="s", unit="s")
コード例 #5
0
    def parse(self, inp):
        """"""
        if not inp or inp == NO_STR:
            inp = "0"

        return parse_duration(inp, input_unit=self.unit, unit=self.unit)
コード例 #6
0
ファイル: parameter.py プロジェクト: mschnepf/law
 def parse(self, inp):
     """"""
     if not inp:
         return 0.
     else:
         return parse_duration(inp, input_unit=self.unit, unit=self.unit)