Class: Pkg_noisrev::FbsdPackage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pkg_noisrev/fbsdpackage.rb

Defined Under Namespace

Classes: OnePackage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_dir, ports_dir) ⇒ FbsdPackage

Returns a new instance of FbsdPackage.



35
36
37
38
39
40
41
42
43
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 35

def initialize(db_dir, ports_dir)
  @db_dir = db_dir
  @ports_dir = ports_dir
  
  @data = []
  @data_massage = false
  
  @queue = FbsdPackage.dir_collect(@db_dir)
end

Instance Attribute Details

#db_dirObject (readonly)

Returns the value of attribute db_dir.



33
34
35
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 33

def db_dir
  @db_dir
end

#ports_dirObject (readonly)

Returns the value of attribute ports_dir.



33
34
35
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 33

def ports_dir
  @ports_dir
end

Class Method Details

.dir_collect(d) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 104

def self.dir_collect(d)
  q = Queue.new
  Dir.glob("#{d}/*").reject {|i| !File.directory?(i) }.map do |i|
    q.push File.basename(i)
  end
  fail "no package records in #{d}" unless q.size > 0
  q
end

.origin(db_dir, name) ⇒ Object

Return something like [‘foo/bar’, 3]



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
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 114

def self.origin(db_dir, name)
  contents = File.read "#{db_dir}/#{name}/+CONTENTS"
  db_required_by = "#{db_dir}/#{name}/+REQUIRED_BY"
  
  category = nil
  origin = nil
  has_dep = contents.match(/^\s*@pkgdep /)
  
  # Set a package category
  #
  # 0--Root (No dependencies, not depended on)
  # 1--Trunk (No dependencies, are depended on)
  # 2--Branch (Have dependencies, are depended on)
  # 3--Leaf (Have dependencies, not depended on)
  if File.size?(db_required_by)
    category = 1
    category = 2 if has_dep
  else
    category = 0
    category = 3 if has_dep
  end
  
  origin = $1 if contents.match(/^\s*@comment\s+ORIGIN:(.+)$/)
  [origin, category]
end

.parse_name(name) ⇒ Object



98
99
100
101
102
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 98

def self.parse_name(name)
  t = name.split '-'
  return [name, '0'] if t.size < 2
  [t[0..-2].join('-'), t.last]
end


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 185

def self.print_like_portmaster(ports_dir, packages)
  moved = FbsdPort.moved(ports_dir)
    
  root = []
  trunk = []
  branch = []
  leaf = []
  godknowswhat = []
  packages.sort.each {|i|
    case i.category
    when 0
      root << i
    when 1
      trunk << i
    when 2
      branch << i
    when 3
      leaf << i
    else
      godknowswhat << i
    end
  }

  outofsync = 0
  p = ->(category, data) {
    return if data.size == 0
    puts "* #{OnePackage::CATEGORY[category]}, #{data.size}"
    data.each {|i|
      puts i.name + '-' + i.ver
      if i.ports_ver
        if FbsdPackageVersion.version_cmp(i.ver, i.ports_ver) != 0
          puts "  => Ports have another version: #{i.ports_ver}"
          outofsync += 1
        end
      else
        outofsync += 1
        # check missing package in MOVED db
        if m = moved[i.origin]
          if m.movedto == ""
            puts "  => Deleted at #{m.date}: #{m.why}"
          else
            puts "  => Moved at #{m.date} to #{m.movedto}: #{m.why}"
          end
        else
          # this is your hand made package or your ports are old
          puts "  => Not found in ports"
        end
      end
    }
    puts ""
  }

  puts ""
  p.call 0, root
  p.call 1, trunk
  p.call 2, branch
  p.call 3, leaf
  p.call 4, godknowswhat

  puts "Total #{packages.size}, out of sync #{outofsync}."
end

Instance Method Details

#<=>(other) ⇒ Object

by size



55
56
57
58
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 55

def <=>(other)
  fail "call analyze method first" unless @data_massage
  @data.size <=> other.size
end

#analyze(log = nil) ⇒ Object



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
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 60

def analyze(log = nil)
  pkg_total = @queue.size
  $stdout.puts "#{pkg_total} total: left% processed/okays/failures | thread number: ok/failed\n"
  $stdout.flush
  
  thread_pool = []
  4.times {|i|
    thread_pool[i] = MyThread.new(i, i) {
      @queue.size.times {
        item = @queue.pop(true) rescue break
        r = FbsdPackage.parse_name item
        origin = nil
        category = nil
        begin
          origin, category = FbsdPackage.origin @db_dir, item
          fail "cannot extract the origin for #{name}" unless origin
          
          pver = FbsdPort.ver @ports_dir, origin
          @data << OnePackage.new(r.first, r.last, origin, pver, category)
        rescue
          MyThread.current.stat.failed += 1
          @data << OnePackage.new(r.first, r.last, origin, nil, category)
          log.error "#{$!}" if log
        else
          MyThread.current.stat.ok += 1
        end
      }
    }
  }

  stat = Spectator.new thread_pool, pkg_total, 1
  stat.alarm # print the statistics every 1 second
  
  thread_pool.each(&:join)
  @data_massage = true
  stat.alarm_finish
end

#each(&block) ⇒ Object



49
50
51
52
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 49

def each(&block)
  fail "call analyze method first" unless @data_massage
  @data.each{ |i| block.call i }
end


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
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 140

def print(mode, filter)
  p = ->(item) {
    cond = '='
    if item.ports_ver
      case FbsdPackageVersion.version_cmp(item.ver, item.ports_ver)
      when -1
        cond = '<'
      when 1
          cond = '>'
      end
    else
      cond = '?'
    end
    puts "%21s %s %-21s %s" % [item.ver, cond, item.ports_ver, item.name]
  }

  packages = []
  #
  # filter packages
  #
  case filter
  when ""
    # all, no filter
    packages = @data.sort
  when 'outofsync'
    packages = @data.reject {|i|
      FbsdPackageVersion.version_cmp(i.ver,
                                     (i.ports_ver ? i.ports_ver : "0")) == 0
    }.sort
  when 'missing'
    packages = @data.reject {|i| i.ports_ver }.sort
  else
    fail "invalid filter: #{filter}"
  end

  #
  # print packages
  #
  if mode == 'likeportmaster'
    FbsdPackage.print_like_portmaster @ports_dir, packages
  else
    packages.each {|idx| p.call(idx) }
  end
end

#sizeObject



45
46
47
# File 'lib/pkg_noisrev/fbsdpackage.rb', line 45

def size
  @data.size
end