def GenerateAppleSUSCatalog(os_version, track, _datetime=datetime.datetime):
  """Generates an Apple SUS catalog for a given os_version and track.

  This function loads the untouched/raw Apple SUS catalog, removes any
  products/updates that are not approved for the given track, then saves
  a new catalog (plist/xml) to Datastore for client consumption.

  Args:
    os_version: str OS version to generate the catalog for.
    track: str track name to generate the catalog for.
    _datetime: datetime module; only used for stub during testing.
  Returns:
    tuple, new models.AppleSUSCatalog object and plist.ApplePlist object. Or,
    if there is no "untouched" catalog for the os_version, then (None, None) is
    returned.
  """
  logging.info('Generating catalog: %s_%s', os_version, track)

  # clear any locks on this track, potentially set by admin product changes.
  gae_util.ReleaseLock(CATALOG_REGENERATION_LOCK_NAME % track)

  catalog_key = '%s_untouched' % os_version
  untouched_catalog_obj = models.AppleSUSCatalog.get_by_key_name(catalog_key)
  if not untouched_catalog_obj:
    logging.warning('Apple Update catalog does not exist: %s', catalog_key)
    return None, None
  untouched_catalog_plist = plist.ApplePlist(untouched_catalog_obj.plist)
  untouched_catalog_plist.Parse()

  approved_product_ids = set()
  products_query = models.AppleSUSProduct.AllActive().filter('tracks =', track)
  for product in products_query:
    approved_product_ids.add(product.product_id)

  product_ids = untouched_catalog_plist.get('Products', {}).keys()
  new_plist = untouched_catalog_plist
  for product_id in product_ids:
    if product_id not in approved_product_ids:
      del new_plist['Products'][product_id]

  catalog_plist_xml = new_plist.GetXml()

  # Save the catalog using a time-specific key for rollback purposes.
  now = _datetime.utcnow()
  now_str = now.strftime('%Y-%m-%d-%H-%M-%S')
  backup = models.AppleSUSCatalog(
      key_name='backup_%s_%s_%s' % (os_version, track, now_str))
  backup.plist = catalog_plist_xml
  backup.put()
  # Overwrite the catalog being served for this os_version/track pair.
  c = models.AppleSUSCatalog(key_name='%s_%s' % (os_version, track))
  c.plist = catalog_plist_xml
  c.put()
  return c, new_plist
dataset/ETHPy150Open google/simian/src/simian/mac/common/applesus.py/GenerateAppleSUSCatalog/original