<commit_before>class FeedValidationService
  include Singleton

  FEEDVALIDATOR_PATH = './virtualenv/bin/feedvalidator.py'

  def self.run_google_feedvalidator(filename)
    # Validate
    return unless Figaro.env.run_google_feedvalidator.presence == 'true'
    # Create a tempfile to use the filename.
    outfile = nil
    Tempfile.open(['feedvalidator', '.html']) do |tmpfile|
      outfile = tmpfile.path
    end

    # Run feedvalidator
    feedvalidator_output = nil
    IO.popen([FEEDVALIDATOR_PATH, '-n', '-o', outfile, filename], "w+") do |io|
      io.write("\n")
      io.close_write
      feedvalidator_output = io.read
    end
    # feedvalidator_output

    return unless File.exists?(outfile)
    # Unlink temporary file
    file_feedvalidator = File.open(outfile)
    File.unlink(outfile)
    file_feedvalidator
  end

  def self.run_validators(feed_version)
    # Validate the new data
    gtfs_filename = feed_version.file.local_path_copying_locally_if_needed
    file_feedvalidator = run_google_feedvalidator(gtfs_filename)
    feed_version.file.remove_any_local_cached_copies
    # Save
    feed_version.update!(
      file_feedvalidator: file_feedvalidator,
    )
    # Return
    feed_version
  end
end<commit_msg>Check for file<commit_after>class FeedValidationService
  include Singleton

  FEEDVALIDATOR_PATH = './virtualenv/bin/feedvalidator.py'

  def self.run_google_feedvalidator(filename)
    # Create a tempfile to use the filename.
    outfile = nil
    Tempfile.open(['feedvalidator', '.html']) do |tmpfile|
      outfile = tmpfile.path
    end

    # Run feedvalidator
    feedvalidator_output = nil
    IO.popen([FEEDVALIDATOR_PATH, '-n', '-o', outfile, filename], "w+") do |io|
      io.write("\n")
      io.close_write
      feedvalidator_output = io.read
    end
    # feedvalidator_output

    return unless File.exists?(outfile)
    # Unlink temporary file
    file_feedvalidator = File.open(outfile)
    File.unlink(outfile)
    file_feedvalidator
  end

  def self.run_validators(feed_version)
    # Copy file
    gtfs_filename = feed_version.file.local_path_copying_locally_if_needed
    fail Exception.new('FeedVersion has no file attachment') unless gtfs_filename

    # Run validators
    file_feedvalidator = run_google_feedvalidator(gtfs_filename)

    # Cleanup
    feed_version.file.remove_any_local_cached_copies

    # Save
    feed_version.update!(
      file_feedvalidator: file_feedvalidator,
    )

    # Return
    feed_version
  end
end
{'fraction_non_alphanumeric': 0.08380112269446673, 'fraction_numerical': 0.0, 'mean_word_length': 3.511754068716094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '961386', 'n_tokens_mistral': 792, 'n_tokens_neox': 802, 'n_words': 159}