daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Jul 22nd, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env perl
  2.  
  3. use warnings;
  4. use strict;
  5. use 5.010;
  6.  
  7. my @CMPMODS = ( 'CPAN::Version' => 'CPN' => sub {
  8.                     return CPAN::Version->vcmp( @_ );
  9.                 },
  10.                 'version' => 'VRM' => sub {
  11.                     return version->parse( $_[0] ) cmp version->parse( $_[1] );
  12.                 },
  13.                 'ALPM' => 'PAC' => \&ALPM::Package::vercmp,
  14.                );
  15.  
  16. # Zero-pads version strings to make pacman happy.
  17. sub archify_verstr
  18. {
  19.     my ( $ver ) = @_;
  20.  
  21.     return archify_verstr( $1 ) . $2 if $ver =~ /\A([^_]+)(_.*)\z/;
  22.  
  23.     # Bye bye letters.
  24.     $ver =~ tr/.0-9//cd;
  25.  
  26.     # This makes perl's vercmp match pacmans's.
  27.     # return "$ver.0" if $ver =~ /\A\d+[.]\d+\z/;
  28.  
  29.     # Decimal versions are padded to 6 decimal spaces (if needed).
  30.     return $ver . q{0} x (6 - length $1) if $ver =~ /\A\d+[.](\d+)\z/;
  31.  
  32.     return $ver;
  33. }
  34.  
  35. sub cpan_to_pacman
  36. {
  37.   my $ver = shift;
  38.   return 'undef' if not defined($ver);
  39.  
  40.   # Check if the version is parsable via the "version" module. If not, return
  41.   # "undef".
  42.   if ($ver !~ m/^$version::STRICT$/ and $ver !~ m/^$version::LAX$/)
  43.   {
  44.     # print STDERR "Invalid version string: $ver\n";
  45.     return 'undef';
  46.   }
  47.   $ver = version->parse($ver)->numify;
  48.  
  49.   # Underscores are considered "alpha" versions. Replace them with "a" so that
  50.   # Pacman treats them as alpha versions too.
  51.   $ver =~ s/_/a\./;
  52.  
  53.   # Versions that expand to 6 decimal places should be broken up into 2 groups
  54.   # of 3 digits. See the "Decimal Versions" section of the "version::Internals"
  55.   # POD.
  56.   $ver =~ s/(?<=\.)(\d{3})(\d{3})$/${1}.${2}/;
  57.  
  58.   return $ver;
  59. }
  60.  
  61.  
  62. my @CONVERSIONS = ( 'ORIG' => sub { shift },
  63.                     'PAD0' => \&archify_verstr,
  64.                     'XYNE' => \&cpan_to_pacman,
  65.                    );
  66.  
  67. my $LINEFMT;
  68.  
  69. sub load_cmpmods
  70. {
  71.     my (@labels, @loaded);
  72.  
  73.     LOAD_LOOP:
  74.     while ( @CMPMODS ) {
  75.         my ( $modname, $label, $cmp_ref ) = splice @CMPMODS, 0, 3;
  76.         eval "require $modname" or next LOAD_LOOP;
  77.         push @labels, $label;
  78.         push @loaded, $cmp_ref;
  79.     }
  80.  
  81.     return (\@loaded, \@labels);
  82. }
  83.  
  84. #---HELPER FUNCTION---
  85. sub cmpchar
  86. {
  87.     my ($cmp) = @_;
  88.     return $cmp unless $cmp =~ /\A-?\d+\z/;
  89.     return ( $cmp  < 0 ? q{<}  :
  90.              $cmp == 0 ? q{=}  :
  91.              $cmp  > 0 ? q{>}  :
  92.              die );
  93. }
  94.  
  95. sub print_cmps
  96. {
  97.     my ($leftver, $rightver, $cmps_ref) = @_;
  98.  
  99.     my @cmpresults = ( map { cmpchar( $_ ) }
  100.                        map { eval { $_->( $leftver, $rightver ) } // '?' }
  101.                        @$cmps_ref );
  102.  
  103.     my $cmps_match = 1;
  104.     my $first = $cmpresults[0];
  105.     CHECK_LOOP:
  106.     for my $i ( 1 .. $#cmpresults ) {
  107.         unless ( $cmpresults[ $i ] eq $first ) {
  108.             $cmps_match = 0;
  109.             last CHECK_LOOP;
  110.         }
  111.     }
  112.  
  113.     printf $LINEFMT, $leftver, $rightver, @cmpresults,
  114.         ( $cmps_match ? q{:)} : q{:(} );
  115. }
  116.  
  117. #-----------------------------------------------------------------------------
  118. # SCRIPT START
  119.  
  120. die <<"END_USAGE" if grep { /-{1,2}h(?:elp)?/ } @ARGV;
  121.  
  122. Usage:
  123.   $0 <name of version list file>
  124.     OR
  125.   cat <name of version list file> | $0
  126.  
  127. Version list files are text files with versions separated by
  128. whitespace.  Every two versions that are read are compared
  129. together. One comparison is done with the origin version strings and
  130. the other is with the "normalized" version. You can supply paths to
  131. version files as arguments or pipe/type them into STDIN.
  132.  
  133. -juster
  134.  
  135. END_USAGE
  136.  
  137. # Load what comparators (?) we are able to.
  138. my ($cmps_ref, $lbls_ref) = load_cmpmods();
  139.  
  140. # Print simple column headers to tell which is which.
  141. printf "%43s %s | EQ?\n", q{|}, ( join q{ }, @$lbls_ref );
  142.  
  143. # Line format is different depending on the number of results we can get.
  144. $LINEFMT = qq{%15s cmp %-15s |  } .
  145.     ( join q{   }, ( q{%1s} ) x scalar @$lbls_ref )
  146.     . qq{  | %s\n};
  147.  
  148. my @verstrs;
  149. while( <> ) {
  150.     push @verstrs, split;
  151.  
  152.     while ( @verstrs > 1 ) {
  153.         my ($leftver, $rightver) = splice @verstrs, 0, 2;
  154.  
  155.         my ($cname, $csub_ref);
  156.         my $i = 0;
  157.         while ( $i <= $#CONVERSIONS ) {
  158.             $cname    = $CONVERSIONS[ $i++ ];
  159.             $csub_ref = $CONVERSIONS[ $i++ ];
  160.             print "$cname: ";
  161.             print_cmps( $csub_ref->( $leftver ),
  162.                         $csub_ref->( $rightver ),
  163.                         $cmps_ref );
  164.         }
  165.     }
  166. }
  167.  
  168. if ( @verstrs ) {
  169.     warn <<"END_WARN"
  170. Warning: The last version (@verstrs) had no versions following it to compare.
  171. END_WARN
  172. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top