Version.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. # Copyright © Colin Watson <cjwatson@debian.org>
  2. # Copyright © Ian Jackson <ijackson@chiark.greenend.org.uk>
  3. # Copyright © 2007 Don Armstrong <don@donarmstrong.com>.
  4. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  5. #
  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.
  10. #
  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.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. package Dpkg::Version;
  19. use strict;
  20. use warnings;
  21. our $VERSION = '1.01';
  22. our @EXPORT = qw(
  23. version_compare
  24. version_compare_relation
  25. version_normalize_relation
  26. version_compare_string
  27. version_compare_part
  28. version_split_digits
  29. version_check
  30. REL_LT
  31. REL_LE
  32. REL_EQ
  33. REL_GE
  34. REL_GT
  35. );
  36. use Exporter qw(import);
  37. use Carp;
  38. use Dpkg::Gettext;
  39. use Dpkg::ErrorHandling;
  40. use constant {
  41. REL_LT => '<<',
  42. REL_LE => '<=',
  43. REL_EQ => '=',
  44. REL_GE => '>=',
  45. REL_GT => '>>',
  46. };
  47. use overload
  48. '<=>' => \&_comparison,
  49. 'cmp' => \&_comparison,
  50. '""' => sub { return $_[0]->as_string(); },
  51. 'bool' => sub { return $_[0]->as_string() if $_[0]->is_valid(); },
  52. 'fallback' => 1;
  53. =encoding utf8
  54. =head1 NAME
  55. Dpkg::Version - handling and comparing dpkg-style version numbers
  56. =head1 DESCRIPTION
  57. The Dpkg::Version module provides pure-Perl routines to compare
  58. dpkg-style version numbers (as used in Debian packages) and also
  59. an object oriented interface overriding perl operators
  60. to do the right thing when you compare Dpkg::Version object between
  61. them.
  62. =head1 METHODS
  63. =over 4
  64. =item $v = Dpkg::Version->new($version, %opts)
  65. Create a new Dpkg::Version object corresponding to the version indicated in
  66. the string (scalar) $version. By default it will accepts any string
  67. and consider it as a valid version. If you pass the option "check => 1",
  68. it will return undef if the version is invalid (see version_check for
  69. details).
  70. You can always call $v->is_valid() later on to verify that the version is
  71. valid.
  72. =cut
  73. sub new {
  74. my ($this, $ver, %opts) = @_;
  75. my $class = ref($this) || $this;
  76. $ver = "$ver" if ref($ver); # Try to stringify objects
  77. if ($opts{check}) {
  78. return unless version_check($ver);
  79. }
  80. my $self = {};
  81. if ($ver =~ /^([^:]*):(.+)$/) {
  82. $self->{epoch} = $1;
  83. $ver = $2;
  84. } else {
  85. $self->{epoch} = 0;
  86. $self->{no_epoch} = 1;
  87. }
  88. if ($ver =~ /(.*)-(.*)$/) {
  89. $self->{version} = $1;
  90. $self->{revision} = $2;
  91. } else {
  92. $self->{version} = $ver;
  93. $self->{revision} = 0;
  94. $self->{no_revision} = 1;
  95. }
  96. return bless $self, $class;
  97. }
  98. =item boolean evaluation
  99. When the Dpkg::Version object is used in a boolean evaluation (for example
  100. in "if ($v)" or "$v || 'default'") it returns its string representation
  101. if the version stored is valid ($v->is_valid()) and undef otherwise.
  102. =item $v->is_valid()
  103. Returns true if the version is valid, false otherwise.
  104. =cut
  105. sub is_valid {
  106. my $self = shift;
  107. return scalar version_check($self);
  108. }
  109. =item $v->epoch(), $v->version(), $v->revision()
  110. Returns the corresponding part of the full version string.
  111. =cut
  112. sub epoch {
  113. my $self = shift;
  114. return $self->{epoch};
  115. }
  116. sub version {
  117. my $self = shift;
  118. return $self->{version};
  119. }
  120. sub revision {
  121. my $self = shift;
  122. return $self->{revision};
  123. }
  124. =item $v->is_native()
  125. Returns true if the version is native, false if it has a revision.
  126. =cut
  127. sub is_native {
  128. my $self = shift;
  129. return $self->{no_revision};
  130. }
  131. =item $v1 <=> $v2, $v1 < $v2, $v1 <= $v2, $v1 > $v2, $v1 >= $v2
  132. Numerical comparison of various versions numbers. One of the two operands
  133. needs to be a Dpkg::Version, the other one can be anything provided that
  134. its string representation is a version number.
  135. =cut
  136. sub _comparison {
  137. my ($a, $b, $inverted) = @_;
  138. if (not ref($b) or not $b->isa('Dpkg::Version')) {
  139. $b = Dpkg::Version->new($b);
  140. }
  141. ($a, $b) = ($b, $a) if $inverted;
  142. my $r = version_compare_part($a->epoch(), $b->epoch());
  143. return $r if $r;
  144. $r = version_compare_part($a->version(), $b->version());
  145. return $r if $r;
  146. return version_compare_part($a->revision(), $b->revision());
  147. }
  148. =item "$v", $v->as_string(), $v->as_string(%options)
  149. Accepts an optional option hash reference, affecting the string conversion.
  150. Options:
  151. =over 8
  152. =item omit_epoch (defaults to 0)
  153. Omit the epoch, if present, in the output string.
  154. =item omit_revision (defaults to 0)
  155. Omit the revision, if present, in the output string.
  156. =back
  157. Returns the string representation of the version number.
  158. =cut
  159. sub as_string {
  160. my ($self, %opts) = @_;
  161. my $no_epoch = $opts{omit_epoch} || $self->{no_epoch};
  162. my $no_revision = $opts{omit_revision} || $self->{no_revision};
  163. my $str = '';
  164. $str .= $self->{epoch} . ':' unless $no_epoch;
  165. $str .= $self->{version};
  166. $str .= '-' . $self->{revision} unless $no_revision;
  167. return $str;
  168. }
  169. =back
  170. =head1 FUNCTIONS
  171. All the functions are exported by default.
  172. =over 4
  173. =item version_compare($a, $b)
  174. Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
  175. is later than $b.
  176. If $a or $b are not valid version numbers, it dies with an error.
  177. =cut
  178. sub version_compare($$) {
  179. my ($a, $b) = @_;
  180. my $va = Dpkg::Version->new($a, check => 1);
  181. defined($va) || error(g_('%s is not a valid version'), "$a");
  182. my $vb = Dpkg::Version->new($b, check => 1);
  183. defined($vb) || error(g_('%s is not a valid version'), "$b");
  184. return $va <=> $vb;
  185. }
  186. =item version_compare_relation($a, $rel, $b)
  187. Returns the result (0 or 1) of the given comparison operation. This
  188. function is implemented on top of version_compare().
  189. Allowed values for $rel are the exported constants REL_GT, REL_GE,
  190. REL_EQ, REL_LE, REL_LT. Use version_normalize_relation() if you
  191. have an input string containing the operator.
  192. =cut
  193. sub version_compare_relation($$$) {
  194. my ($a, $op, $b) = @_;
  195. my $res = version_compare($a, $b);
  196. if ($op eq REL_GT) {
  197. return $res > 0;
  198. } elsif ($op eq REL_GE) {
  199. return $res >= 0;
  200. } elsif ($op eq REL_EQ) {
  201. return $res == 0;
  202. } elsif ($op eq REL_LE) {
  203. return $res <= 0;
  204. } elsif ($op eq REL_LT) {
  205. return $res < 0;
  206. } else {
  207. croak "unsupported relation for version_compare_relation(): '$op'";
  208. }
  209. }
  210. =item $rel = version_normalize_relation($rel_string)
  211. Returns the normalized constant of the relation $rel (a value
  212. among REL_GT, REL_GE, REL_EQ, REL_LE and REL_LT). Supported
  213. relations names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=",
  214. "=", "<=", "<<". ">" and "<" are also supported but should not be used as
  215. they are obsolete aliases of ">=" and "<=".
  216. =cut
  217. sub version_normalize_relation($) {
  218. my $op = shift;
  219. warning('relation %s is deprecated: use %s or %s',
  220. $op, "$op$op", "$op=") if ($op eq '>' or $op eq '<');
  221. if ($op eq '>>' or $op eq 'gt') {
  222. return REL_GT;
  223. } elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') {
  224. return REL_GE;
  225. } elsif ($op eq '=' or $op eq 'eq') {
  226. return REL_EQ;
  227. } elsif ($op eq '<=' or $op eq 'le' or $op eq '<') {
  228. return REL_LE;
  229. } elsif ($op eq '<<' or $op eq 'lt') {
  230. return REL_LT;
  231. } else {
  232. croak "bad relation '$op'";
  233. }
  234. }
  235. =item version_compare_string($a, $b)
  236. String comparison function used for comparing non-numerical parts of version
  237. numbers. Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
  238. is later than $b.
  239. The "~" character always sort lower than anything else. Digits sort lower
  240. than non-digits. Among remaining characters alphabetic characters (A-Z, a-z)
  241. sort lower than the other ones. Within each range, the ASCII decimal value
  242. of the character is used to sort between characters.
  243. =cut
  244. sub _version_order {
  245. my $x = shift;
  246. if ($x eq '~') {
  247. return -1;
  248. } elsif ($x =~ /^\d$/) {
  249. return $x * 1 + 1;
  250. } elsif ($x =~ /^[A-Za-z]$/) {
  251. return ord($x);
  252. } else {
  253. return ord($x) + 256;
  254. }
  255. }
  256. sub version_compare_string($$) {
  257. my @a = map { _version_order($_) } split(//, shift);
  258. my @b = map { _version_order($_) } split(//, shift);
  259. while (1) {
  260. my ($a, $b) = (shift @a, shift @b);
  261. return 0 if not defined($a) and not defined($b);
  262. $a ||= 0; # Default order for "no character"
  263. $b ||= 0;
  264. return 1 if $a > $b;
  265. return -1 if $a < $b;
  266. }
  267. }
  268. =item version_compare_part($a, $b)
  269. Compare two corresponding sub-parts of a version number (either upstream
  270. version or debian revision).
  271. Each parameter is split by version_split_digits() and resulting items
  272. are compared together. As soon as a difference happens, it returns -1 if
  273. $a is earlier than $b, 0 if they are equal and 1 if $a is later than $b.
  274. =cut
  275. sub version_compare_part($$) {
  276. my @a = version_split_digits(shift);
  277. my @b = version_split_digits(shift);
  278. while (1) {
  279. my ($a, $b) = (shift @a, shift @b);
  280. return 0 if not defined($a) and not defined($b);
  281. $a ||= 0; # Default value for lack of version
  282. $b ||= 0;
  283. if ($a =~ /^\d+$/ and $b =~ /^\d+$/) {
  284. # Numerical comparison
  285. my $cmp = $a <=> $b;
  286. return $cmp if $cmp;
  287. } else {
  288. # String comparison
  289. my $cmp = version_compare_string($a, $b);
  290. return $cmp if $cmp;
  291. }
  292. }
  293. }
  294. =item @items = version_split_digits($version)
  295. Splits a string in items that are each entirely composed either
  296. of digits or of non-digits. For instance for "1.024~beta1+svn234" it would
  297. return ("1", ".", "024", "~beta", "1", "+svn", "234").
  298. =cut
  299. sub version_split_digits($) {
  300. my $version = shift;
  301. return split /(?<=\d)(?=\D)|(?<=\D)(?=\d)/, $version;
  302. }
  303. =item ($ok, $msg) = version_check($version)
  304. =item $ok = version_check($version)
  305. Checks the validity of $version as a version number. Returns 1 in $ok
  306. if the version is valid, 0 otherwise. In the latter case, $msg
  307. contains a description of the problem with the $version scalar.
  308. =cut
  309. sub version_check($) {
  310. my $version = shift;
  311. my $str;
  312. if (defined $version) {
  313. $str = "$version";
  314. $version = Dpkg::Version->new($str) unless ref($version);
  315. }
  316. if (not defined($str) or not length($str)) {
  317. my $msg = g_('version number cannot be empty');
  318. return (0, $msg) if wantarray;
  319. return 0;
  320. }
  321. if (not defined $version->epoch() or not length $version->epoch()) {
  322. my $msg = sprintf(g_('epoch part of the version number cannot be empty'));
  323. return (0, $msg) if wantarray;
  324. return 0;
  325. }
  326. if (not defined $version->version() or not length $version->version()) {
  327. my $msg = g_('upstream version cannot be empty');
  328. return (0, $msg) if wantarray;
  329. return 0;
  330. }
  331. if (not defined $version->revision() or not length $version->revision()) {
  332. my $msg = sprintf(g_('revision cannot be empty'));
  333. return (0, $msg) if wantarray;
  334. return 0;
  335. }
  336. if ($version->version() =~ m/^[^\d]/) {
  337. my $msg = g_('version number does not start with digit');
  338. return (0, $msg) if wantarray;
  339. return 0;
  340. }
  341. if ($str =~ m/([^-+:.0-9a-zA-Z~])/o) {
  342. my $msg = sprintf g_("version number contains illegal character '%s'"), $1;
  343. return (0, $msg) if wantarray;
  344. return 0;
  345. }
  346. if ($version->epoch() !~ /^\d*$/) {
  347. my $msg = sprintf(g_('epoch part of the version number ' .
  348. "is not a number: '%s'"), $version->epoch());
  349. return (0, $msg) if wantarray;
  350. return 0;
  351. }
  352. return (1, '') if wantarray;
  353. return 1;
  354. }
  355. =back
  356. =head1 CHANGES
  357. =head2 Version 1.01 (dpkg 1.17.0)
  358. New argument: Accept an options argument in $v->as_string().
  359. New method: $v->is_native().
  360. =head2 Version 1.00 (dpkg 1.15.6)
  361. Mark the module as public.
  362. =cut
  363. 1;