| 1 |
#!/usr/bin/env ruby |
| 2 |
# Purpose: run Debian package checks using lintian and report in JUnit format |
| 3 |
################################################################################ |
| 4 |
# Notes: |
| 5 |
# * for JUnit spec details see http://windyroad.org/dl/Open%20Source/JUnit.xsd |
| 6 |
# |
| 7 |
# This is loosely based on PHPUnit 4.x JUnit output an XSD for it can be found |
| 8 |
# at https://github.com/jenkinsci/xunit-plugin |
| 9 |
# |
| 10 |
# Ideas: |
| 11 |
# * integrate within Jenkins plugin (using jruby) |
| 12 |
# * integrate in Violations plugin (for further reporting options) |
| 13 |
# https://github.com/jenkinsci/violations-plugin.git |
| 14 |
################################################################################ |
| 15 |
|
| 16 |
require 'shellwords' |
| 17 |
|
| 18 |
### cmdline parsing {{{ |
| 19 |
require 'optparse' |
| 20 |
options = {} |
| 21 |
lintian_options = [] |
| 22 |
|
| 23 |
# default |
| 24 |
lintian_file = "lintian.txt" |
| 25 |
|
| 26 |
o = OptionParser.new do|opts| |
| 27 |
opts.banner = "Usage: #{$0} [<options>] <debian_package_file(s)>" |
| 28 |
|
| 29 |
options[:warnings] = false |
| 30 |
opts.on( '-w', '--warnings', 'Output lintian errors *AND* warnings' ) do |
| 31 |
options[:warnings] = true |
| 32 |
end |
| 33 |
|
| 34 |
options[:disablenotes] = false |
| 35 |
opts.on('--disable-notes', 'Disable verbose lintian output' ) do |
| 36 |
options[:disablenotes] = true |
| 37 |
end |
| 38 |
|
| 39 |
opts.on("--filename <filename>", String, "Write lintian output to <filename> (defaults to lintian.txt)") do |f| |
| 40 |
lintian_file = f |
| 41 |
end |
| 42 |
|
| 43 |
options[:skiplintian] = false |
| 44 |
opts.on("--skip-lintian", String, "filename file will be processed") do |
| 45 |
options[:skiplintian] = true |
| 46 |
end |
| 47 |
|
| 48 |
options[:disableplaintext] = false |
| 49 |
opts.on('--disable-plaintext', 'Disable recording lintian output in lintian.txt' ) do |
| 50 |
options[:disableplaintext] = true |
| 51 |
end |
| 52 |
|
| 53 |
opts.on('--lintian-opt=OPTION', 'Pass OPTION to lintian. Can be given multiple times.') do |lo| |
| 54 |
lintian_options << lo |
| 55 |
end |
| 56 |
|
| 57 |
opts.on('--mark-warnings-skipped', |
| 58 |
'Mark warnings as skipped test cases.') do |
| 59 |
options[:markwarningsskipped] = true |
| 60 |
end |
| 61 |
|
| 62 |
opts.on('--mark-as-skipped=tag1,tag2,...', Array, |
| 63 |
'Mark selected tags as skipped.') do |l| |
| 64 |
options[:markasskipped] = l |
| 65 |
end |
| 66 |
|
| 67 |
opts.on( '-h', '--help', 'Display this screen' ) do |
| 68 |
puts opts |
| 69 |
exit |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
begin o.parse! ARGV |
| 74 |
rescue OptionParser::InvalidOption => e |
| 75 |
puts e |
| 76 |
puts o |
| 77 |
exit(1) |
| 78 |
end |
| 79 |
|
| 80 |
# brrr! |
| 81 |
def usage |
| 82 |
$stderr.puts "Usage: #{$0} [<options>] <debian_package_file(s)>" |
| 83 |
exit(1) |
| 84 |
end |
| 85 |
|
| 86 |
files = ARGV |
| 87 |
usage if files.empty? |
| 88 |
### }}} |
| 89 |
|
| 90 |
if ! options[:skiplintian] then |
| 91 |
### make sure lintian is available {{{ |
| 92 |
if not system("which lintian >/dev/null 2>&1") then |
| 93 |
$stderr.puts "Error: lintian not available." |
| 94 |
exit(1) |
| 95 |
end |
| 96 |
# }}} |
| 97 |
|
| 98 |
### run lintian {{{ |
| 99 |
start = Time.now.to_f |
| 100 |
|
| 101 |
lintian_options << "--info" unless options[:disablenotes] |
| 102 |
|
| 103 |
# Ruby 1.8's IO.popen expects a string instead of an array :( |
| 104 |
lintian_cmd = (['lintian'] + lintian_options + files).collect { |v| Shellwords.escape(v) }.join(" ") |
| 105 |
$output = IO.popen(lintian_cmd) do |io| |
| 106 |
io.read |
| 107 |
end |
| 108 |
|
| 109 |
$duration = Time.now.to_f - start |
| 110 |
|
| 111 |
if ! options[:disablenotes] then |
| 112 |
File.open(lintian_file, 'w') {|f| f.write($output) } |
| 113 |
end |
| 114 |
### }}} |
| 115 |
else |
| 116 |
$duration = 0 |
| 117 |
$output = File.open(lintian_file, 'r').read |
| 118 |
end |
| 119 |
|
| 120 |
class JUnitOutput |
| 121 |
require 'rexml/formatters/transitive' |
| 122 |
require 'rexml/document' |
| 123 |
|
| 124 |
def initialize(duration) |
| 125 |
@duration = duration |
| 126 |
@document = REXML::Document::new |
| 127 |
@document << REXML::XMLDecl::new |
| 128 |
@failures = 0 |
| 129 |
@skipped = 0 |
| 130 |
@suite = @document.add_element 'testsuite', {'time' => duration, 'name' => 'lintian'} |
| 131 |
end |
| 132 |
|
| 133 |
def add_case(package, tag) |
| 134 |
tc = @suite.add_element 'testcase' |
| 135 |
tc.attributes['name'] = "#{tag}" |
| 136 |
tc.attributes['classname'] = "lintian.#{package}" |
| 137 |
tc.attributes['assertions'] = 0 |
| 138 |
@last_tag = tag |
| 139 |
@last_package = package |
| 140 |
end |
| 141 |
|
| 142 |
def mark(kind, message) |
| 143 |
tc = @suite[-1] |
| 144 |
e = tc.add_element kind |
| 145 |
e.attributes['type'] = @last_tag |
| 146 |
e.attributes['message'] = message |
| 147 |
end |
| 148 |
|
| 149 |
def mark_skipped(message = '') |
| 150 |
@skipped += 1 |
| 151 |
mark('skipped', message) |
| 152 |
end |
| 153 |
|
| 154 |
def mark_failure(message = '') |
| 155 |
@failures += 1 |
| 156 |
mark('failure', message) |
| 157 |
end |
| 158 |
|
| 159 |
def append_stdout(s) |
| 160 |
stdout = @suite[-1].get_elements('system-out')[0] |
| 161 |
stdout ||= @suite[-1].add_element 'system-out' |
| 162 |
stdout.add_text(s) |
| 163 |
end |
| 164 |
|
| 165 |
def add_success |
| 166 |
self.add_case('<all>', 'lintian-checks') |
| 167 |
end |
| 168 |
|
| 169 |
def finish |
| 170 |
self.add_success unless @suite.has_elements? |
| 171 |
tc_time = @duration / @suite.size |
| 172 |
@suite.each { |tc| tc.attributes['time'] = tc_time } |
| 173 |
@suite.attributes['tests'] = @suite.size |
| 174 |
@suite.attributes['failures'] = @failures |
| 175 |
@suite.attributes['skipped'] = @skipped |
| 176 |
@suite.attributes['errors'] = 0 |
| 177 |
@suite.attributes['assertions'] = 0 |
| 178 |
end |
| 179 |
|
| 180 |
def write |
| 181 |
self.finish |
| 182 |
formatter = REXML::Formatters::Transitive::new |
| 183 |
formatter.write(@document, $stdout) |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
junit_output = JUnitOutput::new($duration) |
| 188 |
|
| 189 |
infos = Hash.new { |hash, key| hash[key] = "" } |
| 190 |
last_tag = nil |
| 191 |
tc_open = false |
| 192 |
|
| 193 |
$output.each_line do |line| |
| 194 |
case line |
| 195 |
when /^([EW]):\s([^:]*):\s(.*)/ |
| 196 |
kind, package, note = $~.captures |
| 197 |
|
| 198 |
if tc_open then |
| 199 |
junit_output.append_stdout(infos[last_tag]) |
| 200 |
tc_open = false |
| 201 |
end |
| 202 |
|
| 203 |
tag = note.match('^[^\s]*')[0] |
| 204 |
|
| 205 |
if kind == 'E' || options[:warnings] then |
| 206 |
junit_output.add_case(package, note) |
| 207 |
if (kind == 'W' && options[:markwarningsskipped]) || |
| 208 |
(options[:markasskipped] && options[:markasskipped].include?(tag)) |
| 209 |
junit_output.mark_skipped(line.rstrip) |
| 210 |
else |
| 211 |
junit_output.mark_failure(line.rstrip) |
| 212 |
end |
| 213 |
tc_open = true |
| 214 |
end |
| 215 |
|
| 216 |
last_tag = tag |
| 217 |
|
| 218 |
# Tag descriptions are prefixed with four spaces, which differentiates them |
| 219 |
# from debug messages |
| 220 |
when /^N:\s{4}/ |
| 221 |
infos[last_tag] << line if last_tag |
| 222 |
end |
| 223 |
end |
| 224 |
|
| 225 |
junit_output.append_stdout(infos[last_tag]) if tc_open |
| 226 |
junit_output.write |
| 227 |
### }}} |
| 228 |
|
| 229 |
## END OF FILE ################################################################# |
| 230 |
# vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=2 ft=ruby |