<commit_before>#
# Copyright (C) 2009 Red Hat, Inc.
# Written by <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.  A copy of the GNU General Public License is
# also available at http://www.gnu.org/copyleft/gpl.html.

require 'yaml'
require 'util/repository_manager'

class ImageDescriptorXML

  UNKNOWN_GROUP = 'Individual packages'

  def initialize(xmlstr = "")
    @doc = Nokogiri::XML(xmlstr)
    # create at least root node if it doesn't exist
    unless @doc.root
      @doc.root = Nokogiri::XML::Node.new('image', @doc)
    end
    @root = @doc.root.at_xpath('/image')
  end

  def name=(str)
    node = get_or_create_node('name')
    node.content = str
  end

  def name
    return get_node_text('name')
  end

  def platform
    node = @root.at_xpath('/image/os')
    return node ? node['name'] : nil
  end

  def platform=(str)
    # FIXME: we remove all repos because we don't know which one is for
    # platform
    # update: we don't add platform repo, image builder chooses right one from OS
    # name, but we add all other repos
    #recreate_repo_nodes
    get_or_create_node('os')['name'] = str
  end

  def platform_version
    node = @root.at_xpath('/image/os')
    return node ? node['version'] : nil
  end

  def platform_version=(str)
    get_or_create_node('os')['version'] = str
  end

  def architecture
    node = @root.at_xpath('/image/os')
    return node ? node['architecture'] : nil
  end

  def architecture=(str)
    get_or_create_node('os')['architecture'] = str
  end

  def description=(str)
    node = get_or_create_node('description')
    node.content = str
  end

  def description
    return get_node_text('description')
  end

  def services=(services)
    service_node = get_or_create_node('services')
    service_node.xpath('.//service').remove
    services.each do |s|
      snode = Nokogiri::XML::Node.new('service', @doc)
      service_node << snode
      snode.content = s[0]
    end
    @services = nil
  end

  def services
    unless @services
      @services = []
      @root.xpath('/image/services/service').each do |s|
        services << s.text
      end
    end
    return @services
  end

  def to_xml
    return @doc.to_xml
  end

  def packages
    @root.xpath('/image/packages/package').map do |s|
      s.at_xpath('.//name').text
    end
  end

  def groups
    @root.xpath('/image/groups/group').map do |s|
      s.at_xpath('.//name').text
    end
  end

  def add_package(pkg)
    pkgs_node = get_or_create_node('packages')
    add_package_node(pkgs_node, pkg) unless packages.include?(pkg)
  end

  def add_group(gname)
    #unless group = repository_manager.groups.find(gname)
    #  raise "group #{gname} not found in repositories"
    #end
    node = get_or_create_node('groups')
    add_group_node(node, gname) unless groups.include?(gname)
  end

  def remove_package(package)
    @root.xpath('/image/packages/package').each do |s|
      if name = s.at_xpath('.//name') and name.text.to_s == package
        s.remove
      end
    end
  end

  def clear_packages
    @root.xpath('/image/packages').each { |s| s.remove }
  end

  def clear_groups
    @root.xpath('/image/groups').each { |s| s.remove }
  end

  private

  def add_group_node(parent, group)
    pnode = get_or_create_node('group', parent)
    n = Nokogiri::XML::Node.new('name', @doc)<commit_msg>Template SW selection - adding collections<commit_after>#
# Copyright (C) 2009 Red Hat, Inc.
# Written by <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.  A copy of the GNU General Public License is
# also available at http://www.gnu.org/copyleft/gpl.html.

require 'yaml'
require 'util/repository_manager'

class ImageDescriptorXML

  UNKNOWN_GROUP = 'Individual packages'

  def initialize(xmlstr = "")
    @doc = Nokogiri::XML(xmlstr)
    # create at least root node if it doesn't exist
    unless @doc.root
      @doc.root = Nokogiri::XML::Node.new('image', @doc)
    end
    @root = @doc.root.at_xpath('/image')
  end

  def name=(str)
    node = get_or_create_node('name')
    node.content = str
  end

  def name
    return get_node_text('name')
  end

  def platform
    node = @root.at_xpath('/image/os')
    return node ? node['name'] : nil
  end

  def platform=(str)
    # FIXME: we remove all repos because we don't know which one is for
    # platform
    # update: we don't add platform repo, image builder chooses right one from OS
    # name, but we add all other repos
    #recreate_repo_nodes
    get_or_create_node('os')['name'] = str
  end

  def platform_version
    node = @root.at_xpath('/image/os')
    return node ? node['version'] : nil
  end

  def platform_version=(str)
    get_or_create_node('os')['version'] = str
  end

  def architecture
    node = @root.at_xpath('/image/os')
    return node ? node['architecture'] : nil
  end

  def architecture=(str)
    get_or_create_node('os')['architecture'] = str
  end

  def description=(str)
    node = get_or_create_node('description')
    node.content = str
  end

  def description
    return get_node_text('description')
  end

  def services=(services)
    service_node = get_or_create_node('services')
    service_node.xpath('.//service').remove
    services.each do |s|
      snode = Nokogiri::XML::Node.new('service', @doc)
      service_node << snode
      snode.content = s[0]
    end
    @services = nil
  end

  def services
    unless @services
      @services = []
      @root.xpath('/image/services/service').each do |s|
        services << s.text
      end
    end
    return @services
  end

  def to_xml
    return @doc.to_xml
  end

  def packages
    @root.xpath('/image/packages/package').map do |s|
      s.at_xpath('.//name').text
    end
  end

  def groups
    @root.xpath('/image/groups/group').map do |s|
      s.at_xpath('.//name').text
    end
  end

  def add_package(pkg)
    pkgs_node = get_or_create_node('packages')
    add_package_node(pkgs_node, pkg) unless packages.include?(pkg)
  end

  def add_group(gname)
    # FIXME: this is temporary hack until we have new design:
    # when adding group, we add particular group packages
    # instead of whole group
    unless group = repository_manager.groups.find {|g| g[:id] == gname}
      raise "group #{gname} not found in repositories"
    end
    group[:packages].keys.each {|p| add_package(p)}
    #node = get_or_create_node('groups')
    #add_group_node(node, gname) unless groups.include?(gname)
  end

  def remove_package(package)
    @root.xpath('/image/packages/package').each do |s|
      if name = s.at_xpath('.//name') and name.text.to_s == package
        s.remove
      end
    end
  end

  def clear_packages
    @root.xpath('/image/packages').each { |s| s.remove }
  end

  def clear_groups
    @root.xpath('/image/groups').each { |s| s.remove }
  end

  private

  def add_group_node(parent, group)
    pnode = get_or_create_node('group', parent)
    n = Nokogiri::XML::Node.new('name', @doc)
{'fraction_non_alphanumeric': 0.11293483586477217, 'fraction_numerical': 0.004164625183733464, 'mean_word_length': 3.1850333162480777, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '2475288', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2588, 'n_words': 795}