aboutsummaryrefslogtreecommitdiff
blob: c1bf17b34021f9aa993d14aceed704e5a45eac31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Gentoo
  class StaticMirrorDataGenerator < Jekyll::Generator
    DISTFILES_XML = '_data/mirrors-distfiles.xml'
    RSYNC_XML    = '_data/mirrors-rsync.xml'

    def generate(site)
      site.data['mirrors'] ||= { 'rsync' => {}, 'distfiles' => {} }

      load_mirrors(site, DISTFILES_XML, 'distfiles')
      load_mirrors(site, RSYNC_XML, 'rsync')
    end

    def load_mirrors(site, xml, key)
      mirrorinfo = Nokogiri::XML(File.open(xml))

      mirrorinfo.xpath('/mirrors/mirrorgroup').each do |mirrorgroup|
        region = mirrorgroup['region']
        country = mirrorgroup['countryname']
        country_code = mirrorgroup['country']

        site.data['mirrors'][key][region] ||= {}
        site.data['mirrors'][key][region][country_code] ||= { 'name' => country, 'mirrors' => [] }

        mirrorgroup.children.each do |mirror|
          mirror_data = { 'uris' => [] }
          next unless mirror.name == 'mirror'

          mirror.children.each do |tag|
            case tag.name
            when 'name'
              mirror_data['name'] = tag.text
            when 'uri'
              uri = {
                'protocol' => tag['protocol'],
                'ipv4' => tag['ipv4'],
                'ipv6' => tag['ipv6'],
                'partial' => tag['partial'],
                'uri' => tag.text
              }

              mirror_data['uris'] << uri
            end
          end

          site.data['mirrors'][key][region][country_code]['mirrors'] << mirror_data
        end
      end
    end
  end
end