Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/portupgrade
Path: blob/master/lib/pkgtools/portinfo.rb
102 views
1
# vim: set sts=2 sw=2 ts=8 et:
2
#
3
# Copyright (c) 2001-2004 Akinori MUSHA <[email protected]>
4
# Copyright (c) 2006-2008 Sergey Matveychuk <[email protected]>
5
# Copyright (c) 2009-2012 Stanislav Sedov <[email protected]>
6
#
7
# All rights reserved.
8
#
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions
11
# are met:
12
# 1. Redistributions of source code must retain the above copyright
13
# notice, this list of conditions and the following disclaimer.
14
# 2. Redistributions in binary form must reproduce the above copyright
15
# notice, this list of conditions and the following disclaimer in the
16
# documentation and/or other materials provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
# SUCH DAMAGE.
29
#
30
31
require 'pkgtools/portsdb'
32
require 'pkgtools/pkginfo'
33
34
class PortInfo
35
include Comparable
36
37
FIELDS = [ :pkgname, :origin, :prefix, :comment, :descr_file,
38
:maintainer, :categories, :build_depends, :run_depends, :www,
39
:extract_depends, :patch_depends, :fetch_depends ]
40
LIST_FIELDS = [ :categories, :build_depends, :run_depends,
41
:extract_depends, :patch_depends, :fetch_depends ]
42
PORTS_DIR_FIELDS = [ :origin, :descr_file ]
43
NFIELDS = FIELDS.size
44
FIELD_SEPARATOR = '|'
45
46
def initialize(line)
47
line.is_a?(String) or raise ArgumentError,
48
"You must specify a one line text of port info."
49
50
values = line.chomp.split(FIELD_SEPARATOR, -1)
51
52
if values.size < NFIELDS || values[NFIELDS - 1].index(FIELD_SEPARATOR)
53
raise ArgumentError, "Port info line must consist of #{NFIELDS} fields."
54
end
55
56
@attr = {}
57
58
ports_dir = nil
59
60
FIELDS.each_with_index do |field, i|
61
value = values[i]
62
63
case field
64
when :pkgname
65
begin
66
value = PkgInfo.new(value)
67
rescue => e
68
raise ArgumentError, e.message
69
end
70
when :origin
71
if value.sub!(%r`^(.*)/([^/]+/[^/]+)$`, '\\2')
72
ports_dir = $1
73
else
74
raise ArgumentError, "#{@attr[:pkgname]}: #{value}: malformed origin"
75
end
76
when :descr_file
77
value.sub!(%r`^#{Regexp.quote(ports_dir)}/`, '')
78
when *LIST_FIELDS
79
value = value.split
80
else
81
if value.empty?
82
value = nil
83
end
84
end
85
86
@attr[field] = value
87
end
88
end
89
90
FIELDS.each do |field|
91
module_eval %`
92
def #{field.to_s}
93
@attr[#{field.inspect}]
94
end
95
`
96
end
97
98
def to_s(ports_dir = PortsDB.instance.ports_dir)
99
FIELDS.collect { |field|
100
value = @attr[field]
101
102
if value.nil?
103
''
104
else
105
case field
106
when :pkgname
107
value = value.to_s
108
when :origin, :descr_file
109
value = File.join(ports_dir, value)
110
when *LIST_FIELDS
111
value.join ' '
112
else
113
value
114
end
115
end
116
}.join(FIELD_SEPARATOR) + "\n"
117
end
118
119
def <=>(other)
120
other_name = nil
121
122
case other
123
when PortInfo
124
return origin <=> other.origin
125
when PkgInfo
126
return origin <=> other.origin
127
when String
128
return origin <=> other
129
else
130
a, b = other.coerce(self)
131
132
return a <=> b
133
end
134
end
135
136
def category()
137
categories().first
138
end
139
140
def all_depends()
141
build_depends | run_depends | extract_depends | patch_depends | fetch_depends
142
end
143
144
def required_depends()
145
build_depends | run_depends
146
end
147
148
def self.match?(pattern, origin)
149
if pattern.is_a?(String)
150
File.fnmatch?(pattern, origin, File::FNM_PATHNAME)
151
else
152
pattern === origin ? true : false
153
end
154
end
155
156
def match?(pattern)
157
PortInfo.match?(pattern, @attr[:origin]) ||
158
@attr[:pkgname].match?(pattern)
159
end
160
161
def portdir()
162
PortsDB.instance.portdir origin()
163
end
164
165
def exist?()
166
PortsDB.instance.exist? origin()
167
end
168
169
def masters()
170
PortsDB.instance.masters origin()
171
end
172
end
173
174