Recursively flatten nested lists for the list of phases or groups.

def flatten_phases_and_groups(phases_or_groups):
  """Recursively flatten nested lists for the list of phases or groups."""
  if isinstance(phases_or_groups, PhaseGroup):
    phases_or_groups = [phases_or_groups]
  ret = []
  for phase in phases_or_groups:
    if isinstance(phase, PhaseGroup):
      ret.append(phase.flatten())
    elif isinstance(phase, collections.Iterable):
      ret.extend(flatten_phases_and_groups(phase))
    else:
      ret.append(phase_descriptor.PhaseDescriptor.wrap_or_copy(phase))
  return ret