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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# ===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
# Checks if there are any ebuilds for the +atom+
def has_ebuilds?(atom)
return false unless valid_atom? atom
Dir.chdir("#{portdir}/#{atom}") do
Dir.glob('*.ebuild') do |ebuild|
return true
end
end
false
rescue Errno::ENOENT
false
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, :iuse, :required_use,
# :pdepend, :provide, :eapi, :properties, :defined_phases
# as per portage/pym/portage/cache/metadata.py (database.auxdbkey_order)
#
# @param [String] atom Package atom (without version, see next parameter)
# @param [String] version Desired version, tries to use the last available one (as decided by a rather stupid technique)
# @return [Hash{Symbol => String, Array}] A hash with all available metadata (see above for keys)
def get_metadata(atom, version = :latest)
raise(ArgumentError, "Invalid package atom") unless Portage.valid_atom?(atom)
raise(ArgumentError, "Invalid version string") if version.to_s.include? '..'
items = {
'DEFINED_PHASES' => :defined_phases,
'DEPEND' => :depend,
'DESCRIPTION' => :description,
'EAPI' => :eapi,
'HOMEPAGE' => :homepage,
'IUSE' => :iuse,
'KEYWORDS' => :keywords,
'LICENSE' => :license,
'PDEPEND' => :pdepend,
'PROPERTIES' => :properties,
'RDEPEND' => :rdepend,
'RESTRICT' => :restrict,
'REQUIRED_USE' => :required_use,
'SLOT' => :slot,
'SRC_URI' => :src_uri
}
valid_keys = items.keys
# List of metadata items to split at space
split_keys = ['SRC_URI', 'IUSE', 'KEYWORDS', 'PROPERTIES', 'DEFINED_PHASES']
cat, pkg = atom.split('/', 2)
r = Regexp.compile('^(\\w+)=([^\n]*)')
result = {}
Dir.chdir(File.join(Glsamaker::Portage.portdir, 'metadata', 'md5-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 (match = r.match($_)) != nil and valid_keys.include? match[1]
if split_keys.include? match[1]
result[items[match[1]]] = match[2].split(' ')
else
result[items[match[1]]] = match[2]
end
end
end
f.close
end
result
end
end
end
|