import pathlib
from typing import Tuple
from typing import List
from typing import Optional
import re


def output_files(output_dir: pathlib.Path,
                 jobid: str) -> Tuple[List[pathlib.Path], Optional[str]]:
  """Gives the list of output files from an array job.

  Args:
      output_dir: The directory containing output files
      jobid: A str containing an integer slurm job ID
  Returns:
      A tuple containing:
      - The list of all output files matching the specified jobid
      - The name of the job that produced the output, or None if a name could
        not be parsed
  """
  files = list(output_dir.glob("*-{}-r*.out".format(jobid)))
  if not files:
    return [], None
  name_match = re.search("(.+)-" + jobid, files[0].name)
  if not name_match:
    return files, None
  job_name = name_match.groups()[0]
  return files, job_name