summaryrefslogtreecommitdiff
blob: b238d3807cea604c9afc8e66fc5670b331edb520 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ===GLSAMaker v2
#  Copyright (C) 2009-2011 Alex Legler <a3li@gentoo.org>
#  Copyright (C) 2009 Pierre-Yves Rofes <py@gentoo.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# For more information, see the LICENSE file.

require 'nokogiri'

module Glsamaker
  # Helper functions for Portage tree interaction
  module Portage
    
    # Package description helper
    class Description
      class << self
        # Tries to fetch the description for the package +atom+ from
        # an ebuild's entry (works if running on Gentoo)
        def ebuild(atom)
          raise(ArgumentError, "Invalid package atom") unless Portage.valid_atom?(atom)
          nil
        end

        def eix(atom)
          nil
        end

        # Loads a description for +atom+ from packages.gentoo.org
        def pgo(atom)
          raise(ArgumentError, "Invalid package atom") unless Portage.valid_atom?(atom)

          n = Nokogiri::XML(Glsamaker::HTTP.get("http://packages.gentoo.org/package/#{atom}"))

          node = n.css('p.description').first.children.first
          if node.type == Nokogiri::XML::Node::TEXT_NODE
            node.to_s
          else
            raise ArgumentError, "XML parse error"
          end
        end

        # Loads a description for +atom+ from Google
        def google(atom)
          nil
        end
      end
    end
    
    module_function
    # Returns the location of the portage dir, or raises an exception if it cannot be found
    def portdir
      unless File.directory? GLSAMAKER_PORTDIR
        raise "GLSAMAKER_PORTDIR is not a directory"
      end
      
      GLSAMAKER_PORTDIR
    end
    
    # Validates the atom +atom+
    def valid_atom?(atom)
      atom =~ /^[+a-zA-Z0-9_-]+\/[+a-zA-Z0-9_-]+$/
    end    
    
    # Gets a description
    def get_description(atom)
      Description.eix(atom) ||
      Description.ebuild(atom) ||
      Description.pgo(atom) ||
      Description.google(atom) ||
      "[could not get a description]"
    end
    
    # Returns package atoms that match +re+
    def find_packages(re)
      results = []
      
      Dir.chdir(portdir) do
        Dir.glob('*-*') do |cat|
          Dir.chdir(cat) do
            Dir.glob("*") do |pkg|
              pkg =~ re and results << "#{cat}/#{pkg}"
            end
          end
        end
      end
      
      results
    end
    
    # Returns an array of maintainer email addresses for the package +atom+
    def get_maintainers(atom)
      raise(ArgumentError, "Invalid package atom") unless Portage.valid_atom?(atom)
      raise(ArgumentError, "Cannot find metadata") unless File.exist? File.join(portdir, atom, 'metadata.xml')
      
      x = Nokogiri::XML(File.read(File.join(portdir, atom, 'metadata.xml')))
      
      herds = []
      maintainers = []
      
      x.xpath('/pkgmetadata/herd').each {|h| herds << h.content }
      x.xpath('/pkgmetadata/maintainer/email').each {|m| maintainers << m.content }
      
      unless herds.first == "no-herd"
        herds_xml = Nokogiri::XML(File.read(File.join(portdir, 'metadata', 'herds.xml')))
        herds_email = herds.map {|h| herds_xml.xpath("/herds/herd/name[text()='#{h}']").first.parent.xpath("./email").first.content }
        
        (maintainers + herds_email).uniq
      else
        maintainers
      end
    end
    
    # Returns information from the portage metadata cache
    # Values: :depend, :rdepend, :slot, :src_uri, :restrict, :homepage,
    # :license, :description, :keywords, :inherited, :iuse, :required_use,
    # :pdepend, :provide, :eapi, :properties, :defined_phases
    # as per portage/pym/portage/cache/metadata.py (database.auxdbkey_order)
    def get_metadata(atom, version = :latest, what = [])
      raise(ArgumentError, "Invalid package atom") unless Portage.valid_atom?(atom)
      raise(ArgumentError, "Invalid version string") if version.to_s.include? '..'
      return {} if what == []
      
      lines = [nil, :depend, :rdepend, :slot, :src_uri, :restrict, :homepage,
               :license, :description, :keywords, :inherited, :iuse, :required_use,
               :pdepend, :provide, :eapi, :properties, :defined_phases]
      cat, pkg = atom.split('/', 2)
      result = {}
      
      Dir.chdir(File.join(Glsamaker::Portage.portdir, 'metadata', 'cache', cat)) do
        if version == :latest
          f = File.open(Dir.glob("#{pkg}-[0-9]*").last, 'r')
        else
          f = File.open(File.join("#{pkg}-#{version}"), 'r')
        end
        
        while f.gets
          if what.include?(lines[$.])
            result[lines[$.]] = $_.chomp
          end
        end
        
        f.close
      end
      result
    end
  end
  
end