from pathlib import Path


def extract_working_dir_and_slurm_id(pid):
    """Retrieve the working directory of a process (pid) if present in logs."""
    pid = str(pid)
    gnina_tf_root = Path(__file__).expanduser().resolve().parents[1]
    if not Path(gnina_tf_root, 'process_ids.log').is_file():
        return
    with open(gnina_tf_root / 'process_ids.log', 'r') as f:
        for line in f.readlines():
            chunks = line.split()
            if len(chunks) == 2 and chunks[0] == pid:
                return Path(chunks[1]), None
            if len(chunks) == 3 and chunks[0] == pid:
                return Path(chunks[1]), chunks[2]