File: debci-status

package info (click to toggle)
debci 3.13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: ruby: 6,516; sh: 2,437; javascript: 100; makefile: 92; perl: 11
file content (99 lines) | stat: -rwxr-xr-x 2,402 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/ruby

require 'json'
require 'optparse'

require 'debci'
require 'debci/package_status'

options = {
  all: false,
  json: false,
  status_file: false,
  field: 'status',
  arch: Debci.config.arch,
  suite: Debci.config.suite,
}

OptionParser.new do |opts|
  opts.banner = 'Usage: debci status [OPTIONS] [PACKAGE]'
  opts.separator 'Options:'

  opts.on('-a ARCH', '--arch ARCH', 'Sets architecture to act on') do |arch|
    options[:arch] = arch
  end

  opts.on('-s SUITE', '--suite SUITE', 'Sets suite to act on') do |suite|
    options[:suite] = suite
  end

  opts.on('-l', '--all', 'show status for all packages') do
    options[:all] = true
  end

  opts.on('-j', '--json', 'outputs JSON') do
    options[:json] = true
  end

  opts.on('--status-file', 'outputs the full status file (implies --json)') do
    options[:status_file] = true
    options[:json] = true
  end

  opts.on('-f FIELD', '--field FIELD', 'displays FIELD from the status file (default: status)') do |f|
    options[:field] = f
  end
end.parse!

if !options[:all] && ARGV.empty?
  puts "debci-status: when not using -l/--all, one or more PACKAGEs have to be specified."
  exit 1
end

def get_status_file(pkg)
  # FIXME duplicates logic found elsewhere :-/
  prefix = pkg.sub(/^((lib)?.).*/, '\1')
  File.join(Debci.config.packages_dir, prefix, pkg, 'latest.json')
end

def read_status_file(pkg)
  status_file = get_status_file(pkg)
  if File.exist?(status_file)
    JSON.parse(File.read(status_file))
  else
    { 'package' => pkg, options[:field] => nil }
  end
end

packages = options[:all] ? Debci::Package.all.map(&:name) : ARGV

def format_field(v)
  v || 'unknown'
end

results = Debci::PackageStatus.includes(:package, :job).where(
  'packages.name': packages,
  suite: options[:suite],
  arch: options[:arch],
).group_by { |status| status.package.name }

if options[:json]
  output = []
else
  max_length = packages.map(&:length).max
  fmt = "%-#{max_length}s %s"
end

packages.each do |pkg|
  status = results[pkg]&.first
  if status && options[:status_file]
    output << results[pkg].flatten.map(&:job)
  elsif options[:json]
    output << { "package" => pkg,
                "status" => format_field(status && status.job[options[:field]]), }
  else
    puts fmt % [pkg, format_field(status && status.job[options[:field]])]
  end
end

puts JSON.pretty_generate(output.flatten.as_json) if options[:json]