def __init__(self, lock, venv_python): """ :param SoftLock lock: Acquired lock """ self.venv_python = venv_python self.lock = lock self.folder = lock.folder self.bin = os.path.join(self.folder, "bin") self.python = os.path.join(self.bin, "python") self._frozen = None if runez.file.is_younger(self.python, self.lock.keep): return runez.delete(self.folder) is_py2 = runez.PY2 if venv_python: is_py2 = runez.to_int(venv_python.major, default=2) < 3 if is_py2: venv = virtualenv_path() if not venv: runez.abort("Can't determine path to virtualenv.py") runez.run(self.venv_python.executable, venv, self.folder) else: runez.run(self.venv_python.executable, "-mvenv", self.folder) runez.run(self.python, "-mpip", "install", "wheel")
def _locked(self): """ :return bool: True if lock is held by another process """ if not runez.is_younger(self.lock, self.invalid): # Lock file does not exist or invalidation age reached return False # Consider locked if pid stated in lock file is still valid pid = runez.to_int(runez.first_line(self.lock)) return runez.check_pid(pid)
def _locked(self): """ :return bool: True if lock is held by another process """ if not runez.file.is_younger(self.lock, self.invalid): # Lock file does not exist or invalidation age reached return False # Consider locked if pid stated in lock file is still valid for line in runez.readlines(self.lock, errors="ignore", fatal=False): return runez.check_pid(runez.to_int(line))
def test_to_int(): # bogus assert runez.to_int(None) is None assert runez.to_int(None, default=0) == 0 assert runez.to_int("foo", default=1) == 1 assert runez.to_int("6.1", default=2) == 2 # valid assert runez.to_int(5, default=3) == 5 assert runez.to_int("5", default=3) == 5
def _locked_by(self): """ Returns: (str): CLI args of process holding the lock, if any """ if self.invalid and self.invalid > 0 and not runez.file.is_younger( self.lock_path, self.invalid): return None # Lock file does not exist or invalidation age reached pid = None for line in runez.readlines(self.lock_path): if pid is not None: return line # 2nd line hold CLI args process was invoked with pid = runez.to_int(line) if not runez.check_pid(pid): return None # PID is no longer active
def test_ps(): assert runez.PsInfo.from_pid(None) is None assert runez.PsInfo.from_pid(0) is None p = runez.PsInfo() check_process_tree(p) assert p == runez.PsInfo(0) assert p == runez.PsInfo("0") assert p == runez.PsInfo(os.getpid()) assert p == runez.PsInfo("%s" % os.getpid()) info = p.info assert info["PID"] in str(p) assert p.cmd assert p.cmd_basename assert p.ppid == os.getppid() assert p.userid != p.uid parent = p.parent assert parent.pid == p.ppid # Verify that both variants (user name or uid number) for UID work uid = p.uid userid = p.userid p = runez.PsInfo() if runez.to_int(info["UID"]) is None: p.info["UID"] = uid else: p.info["UID"] = userid assert p.uid == uid assert p.userid == userid # Edge case: verify __eq__ based on pid p.pid = 0 assert p != runez.PsInfo(0)
def test_to_int(): assert runez.to_int(None) is None assert runez.to_int("foo") is None assert runez.to_int(["foo"]) is None assert runez.to_int("5.0") is None assert runez.to_int("_5_") is None assert runez.to_int("_5") is None assert runez.to_int("5_") is None assert runez.to_int("1__5") is None assert runez.to_int("1_ 5") is None assert runez.to_int("0") == 0 assert runez.to_int("-5") == -5 assert runez.to_int("15") == 15 assert runez.to_int("0o10") == 8 assert runez.to_int("0x10") == 16 assert runez.to_int("0o1_0") == 8 assert runez.to_int("0x1_0") == 16 assert runez.to_int(" 1_500 ") == 1500 assert runez.to_int("1_5 ") == 15 assert runez.to_int("1_500_001 ") == 1500001
def version_check_seconds(self): """ :return float: How many seconds to wait before checking for upgrades again """ return runez.to_int(self.get_value("version_check_delay"), default=DEFAULT_VERSION_CHECK_DELAY) * 60
def install_timeout(self): """ :return float: How many minutes to give an installation to complete before assuming it failed """ return runez.to_int(self.get_value("install_timeout"), default=DEFAULT_INSTALL_TIMEOUT)