def grab_files(dst, files, src_vendor, name_date_re, extract_fn):
  """Get date from each filename in files, create a folder for it, under
  dst/drops, then extract the files to it."""

  # The zip indicates that the corresponding drop is good and built from it. But
  # we might have messed up along the way, so:
  # - if we have a drop and a zip, assume it's already handled
  # - if we have a drop but no zip, assume the drop needs to be rebuilt from the zip
  # - if we have a zip and no drop
  #   - if we have new zip, complain
  #   - else rebuild the drop from the old zip
  # - else build the drop, and if successful, save the zip

  for f in files:
    if not os.path.exists(f):
      print 'file \'%s\' does not exist, skipping' % f
      continue

    filename = os.path.basename(f)
    result = name_date_re.match(filename)
    if not result:
      print 'could not parse %s, skipping' % f
      continue

    name = result.group(1)
    date = '_'.join([d for d in result.group(2,3,4)])
    drop_dir = os.path.join(dst, 'drops', name + '_' + date)

    zip_dir = os.path.join(dst, 'zips')
    zip_filename = os.path.join(zip_dir, filename)
    if os.path.exists(drop_dir):
      if os.path.exists(zip_filename):
        print 'already have a %s drop and zip for %s' % (src_vendor, filename)
        continue
      else:
        # clean up, assume needs rebuild
        shutil.rmtree(drop_dir)
    else:
      if os.path.exists(zip_filename):
        if os.path.realpath(f) != os.path.realpath(zip_filename):
          print 'already have a zip file named %s for %s' % (zip_filename, f)
          continue

    os.mkdir(drop_dir)
    extract_fn(drop_dir, f)

    if not os.path.exists(zip_filename):
      print 'writing %s to %s' % (f, zip_filename)
      shutil.copy2(f, zip_filename)
dataset/ETHPy150Open googlei18n/nototools/nototools/grab_download.py/grab_files/original