<commit_before>require "pkgwat/version"
require 'net/https'
require 'json'
require 'sanitize'

module Pkgwat
  require 'pkgwat/railtie' if defined?(Rails)

  F17 = "Fedora 17"
  F16 = "Fedora 16"
  F18 = "Fedora 18"
  EPEL6 = "Fedora EPEL 6"
  EPEL5 = "Fedora EPEL 5"
  DEFAULT_DISTROS = [F17, F16, EPEL6]
  PACKAGE_NAME = "rubygem-:gem"
  PACKAGES_URL = "https://apps.fedoraproject.org/packages/fcomm_connector/bodhi/query/query_active_releases"
  PACKAGES_URL_LIST = "https://apps.fedoraproject.org/packages/fcomm_connector/xapian/query/search_packages"

  def self.check_gem(name, version, distros = DEFAULT_DISTROS, throw_ex = false)
    puts "Checking #{name} #{version}...\n"
    versions = get_versions(name)
    matches = []
    distros.each do |distro|
      dv = versions.detect { |v| v["release"] == distro }
      match = compare_versions(version, dv["stable_version"])
      matches << dv["release"] if match
    end
    puts "#{name} is available in the following distros: #{matches.join(",")}"
  end

  def self.compare_versions(version, distro)
    distro.to_s.split("-").first == version.to_s
  end

  def self.get_versions(gem_name)
    uri = search_url(gem_name)
    response = submit_request(uri)
    raise "Could not connect to packages API (#{response.inspect})" unless response.code == "200"
    parse_results(response.body)
  end

  def self.package_name(gem)
    PACKAGE_NAME.gsub(":gem", gem)
  end

  def self.total_rows(pattern)
    query = {"filters"=>{"search"=>pattern}, "rows_per_page"=>10, "start_row"=>0}
    url = PACKAGES_URL_LIST + "/" + query.to_json
    uri = URI.parse(URI.escape(url)) 
    response = submit_request(uri)
    JSON.parse(response.body)["total_rows"]
  end
  
  #this function queries and returns the specified number of packages starting at the specified row
  def self.get_packages(pattern, start=0, num=nil) 
    if num == nil
      num = total_rows(pattern)
    end
    query = {"filters"=>{"search"=>pattern}, "rows_per_page"=>num, "start_row"=>start}
    url = PACKAGES_URL_LIST + "/" + query.to_json
    uri = URI.parse(URI.escape(url)) 
    response = submit_request(uri)
    clean_response = Sanitize.clean(response.body)
    parse_results(clean_response)    
  end 
 
  #this function just queries for and returns a single package  
  def self.get_package(pattern)
    get_packages(pattern, 0, 1).first
  end 
  
  def self.search_params(gem)
    filters = { :package => package_name(gem) }
    { :filters => filters }
  end

  def self.search_url(gem)
    query = search_params(gem)
    url = PACKAGES_URL + "/" + query.to_json
    URI.parse(URI.escape(url))
  end

  def self.submit_request(uri)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE #TODO: verify<commit_msg>Use BugZilla API to queries for and returns the open bugs list of an rpm<commit_after>require "pkgwat/version"
require 'net/https'
require 'json'
require 'sanitize'

module Pkgwat
  require 'pkgwat/railtie' if defined?(Rails)

  F17 = "Fedora 17"
  F16 = "Fedora 16"
  F18 = "Fedora 18"
  EPEL6 = "Fedora EPEL 6"
  EPEL5 = "Fedora EPEL 5"
  DEFAULT_DISTROS = [F17, F16, EPEL6]
  PACKAGE_NAME = "rubygem-:gem"
  PACKAGES_URL = "https://apps.fedoraproject.org/packages/fcomm_connector/bodhi/query/query_active_releases"
  PACKAGES_URL_LIST = "https://apps.fedoraproject.org/packages/fcomm_connector/xapian/query/search_packages"
  PACKAGES_URL_BUGS = "https://apps.fedoraproject.org/packages/fcomm_connector/bugzilla/query/query_bugs"                       
  BUGZILLA_RELEASEA = ["all" => "", "f17" =>"17", "f16" => "16", "f15" => "15", "e16" => "16", "e15" => "15"]

  def self.check_gem(name, version, distros = DEFAULT_DISTROS, throw_ex = false)
    puts "Checking #{name} #{version}...\n"
    versions = get_versions(name)
    matches = []
    distros.each do |distro|
      dv = versions.detect { |v| v["release"] == distro }
      match = compare_versions(version, dv["stable_version"])
      matches << dv["release"] if match
    end
    puts "#{name} is available in the following distros: #{matches.join(",")}"
  end

  def self.compare_versions(version, distro)
    distro.to_s.split("-").first == version.to_s
  end

  def self.get_versions(gem_name)
    uri = search_url(gem_name)
    response = submit_request(uri)
    raise "Could not connect to packages API (#{response.inspect})" unless response.code == "200"
    parse_results(response.body)
  end

  def self.package_name(gem)
    PACKAGE_NAME.gsub(":gem", gem)
  end

  def self.total_rows(pattern)
    query = {"filters"=>{"search"=>pattern}, "rows_per_page"=>10, "start_row"=>0}
    url = PACKAGES_URL_LIST + "/" + query.to_json
    uri = URI.parse(URI.escape(url)) 
    response = submit_request(uri)
    JSON.parse(response.body)["total_rows"]
  end
  
  #this function queries and returns the specified number of packages starting at the specified row
  def self.get_packages(pattern, start=0, num=nil) 
    if num == nil
      num = total_rows(pattern)
    end
    query = {"filters"=>{"search"=>pattern}, "rows_per_page"=>num, "start_row"=>start}
    url = PACKAGES_URL_LIST + "/" + query.to_json
    uri = URI.parse(URI.escape(url)) 
    response = submit_request(uri)
    clean_response = Sanitize.clean(response.body)
    parse_results(clean_response)    
  end 
 
  #this function just queries for and returns a single package  
  def self.get_package(pattern)
    get_packages(pattern, 0, 1).first
  end 

  #this function queries for and returns a list of then open bugs  
  def self.get_bugs(pattern, version='all', rows_per_page=10, start_row =0)
    if BUGZILLA_RELEASEA[0][version].nil?
      version = BUGZILLA_RELEASEA[0]['all']
    else
      version = BUGZILLA_RELEASEA[0][version]
    end   
    query = {"filters"=> {"package"=> pattern, "version"=> version}, "rows_per_page"=> 10, "start_row"=> 0}
    url = PACKAGES_URL_BUGS + "/" + query.to_json
    uri = URI.parse(URI.escape(url)) 
    response = submit_request(uri)
    parse_results(response.body)  
  end
  
  def self.search_params(gem)
    filters = { :package => package_name(gem) }
    { :filters => filters }
  end

  def self.search_url(gem)
    query = search_params(gem)
    url = PACKAGES_URL + "/" + query.to_json
    URI.parse(URI.escape(url))
  end

  def self.submit_request(uri)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE #TODO: verify
{'fraction_non_alphanumeric': 0.13412563667232597, 'fraction_numerical': 0.01373668776045686, 'mean_word_length': 3.7542186353631695, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 34, 'https://': 5, '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': '1144840', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2266, 'n_words': 492}