File: how-can-i-help

package info (click to toggle)
how-can-i-help 17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 80 kB
  • sloc: ruby: 288; sh: 12; makefile: 4
file content (373 lines) | stat: -rwxr-xr-x 12,116 bytes parent folder | download
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/ruby
# encoding: utf-8
#
# Copyright © 2013-2016, Lucas Nussbaum <lucas@debian.org>
# Copyright © 2015-2016, Tomasz Nitecki <tnnn@tnnn.pl>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Exit with a return code 0 (we don't want break apt/dpkg even if an error occurred).
Signal.trap('EXIT') { exit 0 }

# Gracefully handle SIGPIPE.
Signal.trap('SIGPIPE', 'SYSTEM_DEFAULT')

require 'pp'
require 'debian'
require 'net/http'
require 'zlib'
require 'stringio'
require 'json'
require 'optparse'
require 'fileutils'
require 'time'
require 'etc'

include Debian

ENV['LC_ALL'] = 'C.UTF-8'

$quiet = false
$all = false
$old = false
$kas = false
$apt = false
$show = []
$download = true
$installed_packages = true
$proxy_url = ENV['HTTP_PROXY'] || ENV['http_proxy']

optparse = OptionParser.new do |opts|
  opts.on('-h', '--help', 'show help') do
    puts opts
    puts "\nAllowed types for '--show' option (see manual for more information):"
    puts "\twnpp, newcomer, help, no-testing, testing-autorm, rfs, O, RFA, RFH, ITA, pseudo-package"
    puts "\nTo see opportunities suitable for newcomers you should run how-can-i-help as:"
    puts "\thow-can-i-help --old --show newcomer"
    puts "\nYou can check manual for a more detailed description:"
    puts "\tman how-can-i-help"
    exit(0)
  end

  opts.on('-v', '--version', 'show version') do
    puts "Please use, e.g., 'dpkg -l how-can-i-help'"
    exit(0)
  end

  opts.on('-a', '--all', 'show new opportunities for contribution to all available Debian packages') do
    $all = true
  end

  opts.on('-o', '--old', 'show opportunities that were already shown before (will also show the new ones)') do
    $old = true
  end

  opts.on('-q', '--quiet', 'do not display header and footer') do
    $quiet = true
  end

  opts.on('-j', '--json', 'display output in JSON format') do
    $kas = true
  end

  opts.on('-p', '--apt', 'always exit with code 0; by default run by apt hook to prevent apt failures') do
    $apt = true
  end

  opts.on('-s', '--show <type>...', Array,
          'show only specific types of opportunities (types have to be separated by commas)') do |show|
    $show = show
  end

  opts.on("--[no-]download", "Enable/disable cache update when cache is populated") do |download|
    $download = download
  end

  opts.on("--[no-]installed-packages", "Enable/disable showing opportunities for packages installed locally") do |installed_packages|
    $installed_packages = installed_packages
  end

  opts.on("--autorm-days <N>", Integer, "Only show autoremovals within the next N days") do |autorm_days|
    $autorm_days = autorm_days
  end

end
optparse.parse!

# If we are not in apt mode, return to normal exit code handling.
unless $apt
  Signal.trap('EXIT') {}
end

HOME=Dir.home rescue Etc.getpwuid.dir

# Define application parameters.
unless HOME
  puts 'how-can-i-help: Unable to resolve your $HOME directory - cannot continue.'
  exit(1)
end

unless Dir.exists?(HOME)
  puts 'how-can-i-help: Your $HOME seems to point to a nonexistent location - cannot continue.'
  exit(1)
end

HELPITEMS_URL = 'https://udd.debian.org/how-can-i-help.json.gz'
CACHEDIR = "#{HOME}/.cache/how-can-i-help"
CONFIGDIR = "#{HOME}/.config/how-can-i-help"
SEEN_LOCAL = "#{CACHEDIR}/seen.json"
CACHE = "#{CACHEDIR}/how-can-i-help.json.gz"
PACKAGES = "#{CONFIGDIR}/packages"
IGNORED_TYPES = "#{CONFIGDIR}/ignored"

def system_r(s)
  system(s) or raise
end

FileUtils.mkdir_p CACHEDIR unless File.exists?(CACHEDIR)

if File::exists?(SEEN_LOCAL)
  seen = JSON::parse(IO::read(SEEN_LOCAL))
else
  seen = []
end

# Cache request data
uri = URI(HELPITEMS_URL)
request = Net::HTTP::Get.new(uri.request_uri)
if File::exists?(CACHE)
  stat = File.stat CACHE
  request['If-Modified-Since'] = stat.mtime.httpdate
end

# Dealing with proxy, authenticated or not
proxy_uri = $proxy_url.nil? ? OpenStruct.new : URI.parse($proxy_url)
proxy_user, proxy_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo
http_object = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port,
                     proxy_user, proxy_pass)
http_object.use_ssl = uri.scheme == 'https'
# proceeding get_response

unless $download == false && File::exists?(CACHE)
  begin
    response = http_object.request(request)
  rescue
    puts "how-can-i-help: Error downloading data file: #{$!}"
    exit(1)
  end
  open CACHE, 'w' do |cc|
    cc.write response.body.to_s
  end if response.is_a?(Net::HTTPSuccess)
end

gz = Zlib::GzipReader.open(CACHE)
helpitems = JSON::parse(gz.read)

# get installed packages
packages = []
if $installed_packages
  str = `dpkg-query -W -f '${Package}\n'`
  if str.respond_to?(:encode!)
    str.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  end
  packages += str.split(/\n/)
end

# add user defined packages
if File.file?(PACKAGES)
  additionals = File.read(PACKAGES).gsub(/\s+/m, ' ').strip.split(' ')
  packages += additionals
end

# add user defined ignored types
ignored_types = []
if File.file?(IGNORED_TYPES)
  selected_type = File.read(IGNORED_TYPES).gsub(/\s+/m, ' ').strip.split(' ')
  ignored_types += selected_type
end

# Show a 'gift' tag depreciation warning.
if ignored_types.include?('gift') || $show.include?('gift')
  puts "how-can-i-help: 'gift' tag is deprecated - please use 'newcomer' instead."
end

# Support for 'newcomer' tag as 'gift' UDD flag.
if ignored_types.include?('newcomer')
  ignored_types.push('gift')
end
if $show.include?('newcomer')
  $show.push('gift')
end

helpitems_filtered = []
helpitems.each do |hi|
  next if (not $old) and seen.include?(hi['hash'])
  next if !($show.empty?) && !($show.include?(hi['type']) || (hi['type'] == 'wnpp' && $show.include?(hi['wnpptype'])))
  next if $show.empty? && (ignored_types.include?(hi['type']) || (hi['type'] == 'wnpp' && ignored_types.include?(hi['wnpptype'])))
  if $all
    helpitems_filtered << hi
  elsif hi['type'] == 'wnpp'
    if packages & hi['packages'] != []
      helpitems_filtered << hi
    end
  elsif hi['type'] == 'gift'
    if packages.include?(hi['package']) || (hi['pseudo-package'] == true && !ignored_types.include?('pseudo-package'))
      helpitems_filtered << hi
    end
  elsif hi['type'] == 'help'
    if packages.include?(hi['package'])
      helpitems_filtered << hi
    end
  elsif hi['type'] == 'no-testing'
    if packages.include?(hi['package'])
      helpitems_filtered << hi
    end
  elsif hi['type'] == 'testing-autorm'
    if packages & hi['packages'] != []
      helpitems_filtered << hi
    end
  elsif hi['type'] == 'rfs'
    if packages & hi['packages'] != []
      helpitems_filtered << hi
    end
  end
end

# If we are to show JSON output and there are packages to be shown, then we generate and print it.
if ($kas) && helpitems_filtered.length > 0
    puts(JSON::pretty_generate(helpitems_filtered))
end

# Normal, human-readable output.
unless $kas

  unless $quiet
    puts '======  How can you help?  (doc: https://wiki.debian.org/how-can-i-help ) ======'
    puts
  end

  wnpp = helpitems_filtered.select { |e| e['type'] == 'wnpp' }
  gift = helpitems_filtered.select { |e| e['type'] == 'gift' && e['pseudo-package'] != true }
  infra = helpitems_filtered.select { |e| e['type'] == 'gift' && e['pseudo-package'] == true }
  help = helpitems_filtered.select { |e| e['type'] == 'help' }
  notesting = helpitems_filtered.select { |e| e['type'] == 'no-testing' }
  autoremoval = helpitems_filtered.select { |e| e['type'] == 'testing-autorm' }
  rfs = helpitems_filtered.select { |e| e['type'] == 'rfs' }

  def wnpptype(t)
    return 'O (Orphaned)' if t == 'O'
    return 'RFA (Maintainer looking for adopter)' if t == 'RFA'
    return 'RFH (Maintainer looking for help)' if t == 'RFH'
    return 'ITA (Someone working on adoption)' if t == 'ITA'
  end

  def wnppsortorder(t)
    return 0 if t == 'O'
    return 1 if t == 'RFA'
    return 2 if t == 'ITA'
    return 3 if t == 'RFH'
    return 4
  end

  def wnppcompare(a, b)
    c = wnppsortorder(a['wnpptype']) <=> wnppsortorder(b['wnpptype'])
    if c == 0
      return a['source'] <=> b['source']
    else
      return c
    end
  end

  if wnpp.length > 0
    puts $old ? 'Packages where help is needed, including orphaned ones (from WNPP):' : 'New packages where help is needed, including orphaned ones (from WNPP):'
    wnpp.sort { |a, b| wnppcompare(a,b) }.each do |r|
      puts " - #{r['source']} - https://bugs.debian.org/#{r['wnppbug']} - #{wnpptype(r['wnpptype'])}"
    end
    puts
  end

  if gift.length > 0
    puts $old ? 'Bugs affecting packages, suitable for new contributors (tagged \'newcomer\'):' : 'New bugs affecting packages, suitable for new contributors (tagged \'newcomer\'):'
    gift.sort_by { |r| [r['package'], r['bug']] }.each do |r|
      puts " - #{r['package']} - https://bugs.debian.org/#{r['bug']} - #{r['title']}"
    end
    puts
  end

  if infra.length > 0
    puts $old ? 'Bugs affecting Debian infrastructure (tagged \'newcomer\'):' : 'New bugs affecting Debian infrastructure (tagged \'newcomer\'):'
    infra.sort_by { |r| [r['package'], r['bug']] }.each do |r|
      puts " - #{r['package']} - https://bugs.debian.org/#{r['bug']} - #{r['title']}"
    end
    puts
  end

  if help.length > 0
    puts $old ? 'Bugs where assistance is requested (tagged \'help\'):' : 'New bugs where assistance is requested (tagged \'help\'):'
    help.sort_by { |r| [r['package'], r['bug']] }.each do |r|
      puts " - #{r['package']} - https://bugs.debian.org/#{r['bug']} - #{r['title']}"
    end
    puts
  end

  if notesting.length > 0
    puts $old ? 'Packages removed from Debian \'testing\' (the maintainer might need help):' : 'New packages removed from Debian \'testing\' (the maintainer might need help):'
    notesting.sort_by { |r| [r['source'], r['package']] }.each do |r|
      puts " - #{r['package']} - https://tracker.debian.org/pkg/#{r['source']}"
    end
    puts
  end

  if autoremoval.length > 0
    if defined?($autorm_days)
      autorm_date = Time.now.to_date + $autorm_days
      autorm = "until #{autorm_date.to_s} "
    end
    puts $old ? "Packages going to be removed from Debian 'testing' #{autorm}(the maintainer might need help):" : "New packages going to be removed from Debian 'testing' #{autorm}(the maintainer might need help):"
    autoremoval.sort_by { |r| [r['source'], r['package']] }.each do |r|
      next if defined?($autorm_days) && Time.at(r['removal_time']).to_date > autorm_date
      bugs = r['bugs'].map { |b| "##{b}" }
      if bugs.count == 0
        bugs = ''
      elsif bugs.count > 1
        bugs = " (bugs: #{bugs.sort.join(', ')})"
      else
        bugs = " (bug: #{bugs[0]})"
      end
      puts " - #{r['source']} - https://tracker.debian.org/pkg/#{r['source']} - removal on #{Time.at(r['removal_time']).to_date.to_s}#{bugs}"
    end
    puts
  end

  if rfs.length > 0
    puts $old ? 'Packages waiting for sponsorship (reviews/tests are also useful):' : 'New packages waiting for sponsorship (reviews/tests are also useful):'
    rfs.sort_by { |r| [r['source'], r['id']] }.each do |r|
      puts " - #{r['source']} - https://bugs.debian.org/#{r['id']} - #{r['title']}"
    end
    puts
  end

  if not $old and not $quiet
    puts '-----  Show old opportunities as well as new ones: how-can-i-help --old  -----'
  end
end

# Mark seen packages.
unless $old
  seen = helpitems.map { |hi| hi['hash'] } & seen
  seen = seen + helpitems_filtered.map { |hi| hi['hash'] }
  File::open(SEEN_LOCAL, 'w') do |fd|
    JSON::dump(seen, fd)
  end
end