blib/lib/Linux/KernelSort.pm

filter: Statementstmt 97.5% 81 / 83 Branchbran 94.4% 34 / 36 Conditioncond 75.0% 15 / 20 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 7 / 7 Podpod 80.0% 4 / 5 totaltotal 90.8% 148 / 163 CC 27 SCAR 21.4
CC
27
Cov
94%
compare
13.2
rank
8.0
sort
5.0
CRAP 8.5
↑ Summary
1 =head1 NAME
2  
3 Linux::KernelSort - Perl extension for sorting and comparing Linux
4 kernel versions. The expected kernel version naming convention is
5 the same naming convetion demonstrated by http://www.kernel.org.
6 NOTE: Currently, only the 2.6.x series of kernels (including -rc's,
7 -git's, and -mm's) are properly evaluated.
8  
9 =head1 SYNOPSIS
10  
11 use Linux::KernelSort;
12 my $kernel = new Linux::KernelSort;
13  
14 int $ret;
15 my $version1 = "2.6.19";
16 my $version2 = "2.6.19-rc2-git7";
17 $ret = $kernel->compare($version1, $version2);
18  
19 if ($ret == 0) {
20 print "$version1 and $version2 are the same version";
21 } elsif ($ret > 0) {
22 print "$version1 is newer than $version2";
23 } else {
24 print "$version1 is older than $version2";
25 }
26  
27 my @kernel_list = [ '2.6.15',
28 '2.6.18',
29 '2.6.18-rc2',
30 '2.6.18-rc2-git2',
31 '2.6.18-mm1',
32 '2.6.18-rc2-mm1' ];
33  
34 my @sorted_list = $kernel->sort($kernel_list);
35  
36 print "@sorted_list";
37  
38 =head1 DESCRIPTION
39  
40 Linux::KernelSort is intended to sort a list of kernel versions into
41 ascending order. It also provides the capability to compare
42 two kernel versions and determine if one version is newer, older,
43 or the same as the other version.
44  
45 =head1 FUNCTIONS
46  
47 =cut
48  
49 package Linux::KernelSort;
50  
51 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 our $VERSION = '0.01';
55  
56 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
57 1 my $class = shift;
58 1 my $self = {};
59 1 $self->{debug} = 1;
60 1 bless ($self, $class);
61 1 return $self;
62 }
63  
64 =head2 version_check()
65 Purpose: Verify the version is valid and follows the
66 proper naming convention demonstrated by
67 http://www.kernel.org
68 Input: A string containing the kernel version
69 Return: 0 if version is valid
70 1 if version is invalid
71  
72 =cut
73  
74 119 ▶sub version_check {
Subroutine version_check: called CC=4 SCAR=14.3
Pod documented
75 119 my $self = shift;
76 119 ▶ my $version = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
1190
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
77  
78 119 ▶ if ( $version !~ m/^\d+\.\d+\.\d+(-rc\d+)?(-git\d+)?(-scsi-misc\d+)?(-scsi-rc-fixes\d+)?(-mm\d+)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /^\d+\.\d+\.\d+(-rc\d+)?(-git\d+)?(-scsi-misc\d+)?(-scsi-rc-fixes\d+)?(-mm\d+)?$/) 16 103 119
79 0 ▶ if ( $self->{debug} ) { print "Invalid Kernel Version: $version\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 16 16
80 16 return 1;
81 }
82  
83 103 return 0;
84 }
85  
86 =head2 rank()
87  
88 Purpose: Generate a ranking for a given kernel version
89 Input: A string containing the kernel version which
90 follows the proper naming convention demonstrated
91 by http://www.kernel.org
92 Return: Kernel ranking
93  
94 =cut
95  
96 116 ▶sub rank {
Subroutine rank: called CC=8 SCAR=20.8
Pod documented
97 116 my $self = shift;
98 116 ▶ my $version = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
1160
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
99  
100 116 ▶ if ( $self->version_check($version) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version_check($version)) 15 101 116
101 15 return undef;
102 }
103  
104 101 $version =~ s/\.//g;
105 101 $version =~ m/^(\d+).*/;
106 101 my $rank = $1;
107  
108 101 ▶ if ( $version =~ m/-rc(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /-rc(\d+)/) { } 48 53 101
109 48 my $rc = $1;
110 48 $rank = $rank - 1;
111 48 $rank = $rank . ".$rc";
112 } else {
113 53 $rank = $rank . ".0";
114 }
115  
116 101 ▶ if ( $version =~ m/-git(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /-git(\d+)/) { } 20 81 101
117 20 my $git = $1;
118 20 $rank = $rank . ".$git"
119 } else {
120 81 $rank = $rank . ".0";
121 }
122  
123 101 ▶ if ( $version =~ m/-scsi-misc(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /-scsi-misc(\d+)/) { } 1 100 101
124 1 my $scsi_misc = $1;
125 1 $rank = $rank . ".$scsi_misc"
126 } else {
127 100 $rank = $rank . ".0";
128 }
129  
130 101 ▶ if ( $version =~ m/-scsi-rc-fixes(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /-scsi-rc-fixes(\d+)/) { } 1 100 101
131 1 my $rc_fixes = $1;
132 1 $rank = $rank . ".$rc_fixes"
133 } else {
134 100 $rank = $rank . ".0";
135 }
136  
137 101 ▶ if ( $version =~ m/-mm(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /-mm(\d+)/) { } 37 64 101
138 37 my $mm = $1;
139 37 $rank = $rank . ".$mm";
140 } else {
141 64 $rank = $rank . ".0"
142 }
143  
144 101 return $rank;
145 }
146  
147 =head2 compare()
148  
149 Purpose: Compare two kernel versions
150 Input: Strings ($kernel1, $kernel2) each containing a
151 kernel version which follows the proper naming
152 conventaion demonstrated by http://www.kernel.org
153 Return -1 if $kernel1 < $kernel2 (ie $kernel1 is older than $kernel2)
154 0 if $kernel1 == $kernel2 (ie $kernel1 is the same version as $kernel2)
155 1 if $kernel1 > $kernel2 (ie $kernel1 is newer than $kernel2)
156  
157 =cut
158  
159 54 ▶sub compare {
Subroutine compare: called CC=13 SCAR=25.8
Pod documented
160 54 my $self = shift;
161 54 ▶ my $kernel1 = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
540
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
162 54 ▶ my $kernel2 = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
540
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
163  
164 54 my $rank1 = $self->rank($kernel1);
165 54 my $rank2 = $self->rank($kernel2);
166  
167 54 ▶ if ( !$rank1 || !$rank2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rank1 and $rank2) 11 43 54
Condition100%3/3
$rank1 and $rank2
!ll&&!rl&&r
7443
MC/DC100%2/2
$rank1 and $rank2
$rank1 $rank2
Truth table100%3/3
A and B
A= $rank1B= $rank2
ABresult
0X0
100
111
168 0 ▶ if ( $self->{debug} ) { print "Unable to properly compare kernel versions: $kernel1, $kernel2\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 11 11
169  
170 11 ▶ if ( !$rank1 && !$rank2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $rank1 || $rank2) { } 3 8 11
elsif (not $rank1) { } 4 4 8
Condition100%3/3
$rank1 || $rank2
l!l&&r!l&&!r
443
MC/DC100%2/2
$rank1 || $rank2
$rank1 $rank2
Truth table100%3/3
A || B
A= $rank1B= $rank2
ABresult
1X1
011
000
171 3 return 0;
172 } elsif ( !$rank1 ) {
173 4 return -1;
174 } else {
175 4 return 1;
176 }
177 }
178  
179 43 ▶ while (length($rank1) && length($rank2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rank1 and length $rank2) 109 1 110
Condition66%2/3
length $rank1 and length $rank2
!ll&&!rl&&r
10109
MC/DC50%1/2
length $rank1 and length $rank2
length $rank1 length $rank2
Truth table66%2/3
A and B
A= length $rank1B= length $rank2
ABresult
0X0
100
111
180 109 $rank1 =~ m/^(\d+)\.?(.*)/;
181 109 my $value1 = $1;
182 109 $rank1 = $2;
183  
184 109 $rank2 =~ m/^(\d+)\.?(.*)/;
185 109 my $value2 = $1;
186 109 $rank2 = $2;
187  
188 109 ▶ if ($value1 == $value2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value1 == $value2) { } 67 42 109
elsif ($value1 < $value2) { } 23 19 42
189 67 next;
190 } elsif ($value1 < $value2) {
191 23 return -1;
192 } else {
193 19 return 1;
194 }
195 }
196  
197 1 return 0;
198 }
199  
200 =head2 sort()
201  
202 Purpose: Sort a list of kernel versions in ascending order.
203 Uses shell sort algorithm.
204 Input: Array of strings containing kernel versions which
205 follows the proper naming convention demonstrated
206 by http://www.kernel.org
207 Return: Sorted array
208  
209 =cut
210  
211 2 ▶sub sort {
Subroutine sort: called CC=5 SCAR=16.1
Pod documented
212 2 my $self = shift;
213 2 my (@kernels) = @_;
214  
215 2 my $size = @kernels;
216 2 ▶ for (my $gap = int($size/2); $gap > 0; $gap = int($gap/2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gap > 0) 5 2 7
217 5 ▶ for (my $i = $gap; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 37 5 42
218 37 ▶ for (my $j = $i-$gap; ($j >= 0) && ($self->compare($kernels[$j], $kernels[$j+$gap]) > 0); $j -= $gap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0 and $self->compare($kernels[$j], $kernels[$j + $gap]) > 0) 21 37 58
Condition100%3/3
$j >= 0 and $self->compare($kernels[$j], $kernels[$j + $gap]) > 0
!ll&&!rl&&r
102721
MC/DC100%2/2
$j >= 0 and $self->compare($kernels[$j], $kernels[$j + $gap]) > 0
$j >= 0 $self->compare($kernels[$j], $kernels[$j + $gap]) > 0
Truth table100%3/3
A and B
A= $j >= 0B= $self->compare($kernels[$j], $kernels[$j + $gap]) > 0
ABresult
0X0
100
111
219 21 my $temp = $kernels[$j];
220 21 $kernels[$j] = $kernels[$j+$gap];
221 21 $kernels[$j+$gap] = $temp;
222 }
223 }
224 }
225  
226 2 return @kernels;
227 }
228  
229 =head1 AUTHOR
230  
231 Leann Ogasawara <lt>ogasawara@osdl.org<gt>
232  
233 =head1 COPYRIGHT AND LICENSE
234  
235 Linux-KernelSort is Copyright (c) 2006, by Leann Ogasawara.
236 All rights reserved. You may distribute this code under the terms
237 of either the GNU General Public License or the Artistic License,
238 as specified in the Perl README file.
239  
240 =cut
241  
242 1 1;
243  
244 __END__
↑ Summary