4 # Copyright 2003, 2004 Frank Lichtenheld <frank@lichtenheld.de>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 Deb::Versions - compare Versions of Debian packages
29 my $res = version_cmp( "1:0.2.2-2woody1", "1:0.2.3-7" );
31 my @sorted = version_sort( "1:0.2.2-2woody1", "1:0.2.3-7", "2:0.1.1" );
35 This module allows you to compare version numbers like defined
36 in the Debian policy, section 5.6.11 (L<SEE ALSO>).
38 It provides two functions:
44 version_cmp() gets two version strings as parameters and returns
45 -1, if the first is lower than the second, 0 if equal, 1 if greater.
46 You can use this function as first parameter for the sort() function.
50 version_sort() is just an usefull abbrevation for
52 sort { version_cmp( $b, $a ) } @_;
58 By default, Deb::Versions exports version_cmp() and version_sort().
62 package Deb::Versions;
68 our @ISA = qw( Exporter );
69 our @EXPORT = qw( version_cmp version_sort suites_cmp suites_sort );
71 our $VERSION = v1.0.0;
74 my ( $ver1, $ver2 ) = @_;
76 my ( $e1, $e2, $u1, $u2, $d1, $d2 );
77 my $re = qr/^(?:(\d+):)?([\w.+:~-]+?)(?:-([\w+.~]+))?$/;
79 ( $e1, $u1, $d1 ) = ( $1, $2, $3 );
82 cluck "This seems not to be a valid version number:"
87 ( $e2, $u2, $d2 ) = ( $1, $2, $3 );
90 cluck "This seems not to be a valid version number:"
95 # warn "D: <$e1><$u1><$d1> <=> <$e2><$u2><$d2>\n";
97 my $res = ($e1 <=> $e2);
99 $res = _cmp_part ( $u1, $u2 );
101 $res = _cmp_part ( $d1, $d2 );
106 return sort { version_cmp( $b, $a ) } @_;
110 my ( $v1, $v2 ) = @_;
113 while ( $v1 || $v2 ) {
118 # warn "$sp1 cmp $sp2 = "._lcmp( $sp1,$sp2)."\n";
119 if ( $r = _lcmp( $sp1, $sp2 ) ) {
126 # warn "$np1 <=> $np2 = ".($np1 <=> $np2)."\n";
127 if ( $r = ($np1 <=> $np2) ) {
139 my ( $v1, $v2 ) = @_;
141 for ( my $i = 0; $i <= length( $v1 ); $i++ ) {
142 my ( $n1, $n2 ) = ( ord( substr( $v1, $i, 1 ) ),
143 ord( substr( $v2, $i, 1 ) ) );
144 $n1 += 256 if $n1 && $n1 < 65; # letters sort earlier than non-letters
145 $n1 = -1 if $n1 == 126; # '~' sorts earlier than everything else
146 $n2 += 256 if $n2 && $n2 < 65;
147 $n2 = -1 if $n2 == 126;
148 if ( my $r = ($n1 <=> $n2) ) {
152 return length( $v1 ) <=> length( $v2 );
155 our @SUITES_SORT = qw( bo hamm slink potato woody
156 oldstable sarge stable stable-proposed-updates
157 etch etch-m68k testing testing-proposed-updates lenny
158 sid unstable experimental
159 warty hoary breezy breezy dapper edgy feisty gutsy );
160 our @ARCHIVE_SORT = qw( non-US security updates volatile backports );
161 our @PRIORITY_SORT = qw( required important standard optional extra );
163 our %suites_sort = map { $_ => ($i-=10) } @SUITES_SORT;
164 our %priority_sort = map { $_ => $i-- } @PRIORITY_SORT;
166 our %archive_sort = map { $_ => $i++ } @ARCHIVE_SORT;
169 my ($s_a, $s_b) = @_;
170 my $cmp_a = $suites_sort{$s_a};
172 $cmp_a = $suites_sort{$1} - $archive_sort{$2}
173 if $s_a =~ m;^(.+?)[/-](.*)$;o;
175 my $cmp_b = $suites_sort{$s_b};
177 $cmp_b = $suites_sort{$1} - $archive_sort{$2}
178 if $s_b =~ m;^(.+?)[/-](.*)$;o;
180 return ($cmp_a <=> $cmp_b);
184 return sort { suites_cmp( $b, $a ) } @_;
188 return ($priority_sort{$_[0]} <=> $priority_sort{$_[1]});
192 return sort { priority_cmp( $b, $a ) } @_;
201 Copyright 2003, 2004 Frank Lichtenheld <frank@lichtenheld.de>
203 This file is distributed under the terms of the GNU Public
204 License, Version 2. See the source code for more details.
208 Debian policy <URL:http://www.debian.org/doc/debian-policy/>