releasing/cbrtools/perl/LatestVer
changeset 602 3145852acc89
equal deleted inserted replaced
600:6d08f4a05d93 602:3145852acc89
       
     1 #!perl -w
       
     2 # Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 # 
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 # 
       
    12 # Contributors:
       
    13 # 
       
    14 # Description:
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 use FindBin;
       
    20 use lib "$FindBin::Bin";
       
    21 use Getopt::Long;
       
    22 use IniData;
       
    23 use EnvDb;
       
    24 use CommandController;
       
    25 
       
    26 
       
    27 #
       
    28 # Globals.
       
    29 #
       
    30 
       
    31 my $verbose = 0;
       
    32 my $iniData = IniData->New();
       
    33 my $commandController = CommandController->New($iniData, 'LatestVer');
       
    34 my $envDb; # this is only created when needed, by the EnvDb() function
       
    35 my $listAll;
       
    36 my $userComp;
       
    37 my $everyComponent;
       
    38 my $versionFilter;
       
    39 
       
    40 
       
    41 #
       
    42 # Main.
       
    43 #
       
    44 
       
    45 ProcessCommandLine();
       
    46 PrintLatestVersions();
       
    47 
       
    48 #
       
    49 # Subs.
       
    50 #
       
    51 
       
    52 sub ProcessCommandLine {
       
    53   Getopt::Long::Configure ("bundling");
       
    54   my $help;
       
    55   GetOptions('h' => \$help, 'a:i' => \$listAll, 'v+' => \$verbose, 'f=s' => \$versionFilter);
       
    56 
       
    57   if ($help) {
       
    58     Usage(0);
       
    59   }
       
    60 
       
    61   $userComp = shift @ARGV || undef;
       
    62 
       
    63   unless ($#ARGV == -1) {
       
    64     print "Error: Invalid arguments\n";
       
    65     Usage(1);
       
    66   }
       
    67 }
       
    68 
       
    69 sub Usage {
       
    70   my $exitCode = shift;
       
    71 
       
    72   Utils::PrintDeathMessage($exitCode, "\nUsage: latestver [options] [component] 
       
    73 
       
    74 options:
       
    75 
       
    76 -h   help
       
    77 -a   lists all versions in descending date order
       
    78 -a 6 list the most recent six versions
       
    79 -v   verbose output (-vv very verbose, -vvv very very verbose)
       
    80 -f <version wildcard> only show versions whose version numbers match that text\n");
       
    81 }
       
    82 
       
    83 sub PrintLatestVersions {
       
    84   # Convert the version number filter from glob-style to perl-style RE
       
    85   if (defined $versionFilter) {
       
    86 		require Text::Glob;
       
    87 		# Surprisingly complicated to convert a glob-style to a perl-style RE
       
    88 		# Hence we use a module. Not entirely convinced that Text::Glob
       
    89 		# does a particularly good job (e.g. the pattern t_make*[0] doesn't work)
       
    90 		# but it's better than my attempts, and should suffice for most cases.
       
    91 		$versionFilter = Text::Glob::glob_to_regex($versionFilter);
       
    92 	}
       
    93 
       
    94   # First work out what components we need to process
       
    95   my @compsToProcess;
       
    96   if ($userComp) {
       
    97     @compsToProcess = ($userComp);
       
    98   } else {
       
    99     # Call listcomponents with remote flag set as 0 and continue flag set as 1
       
   100     @compsToProcess = sort @{$iniData->PathData->ListComponents(0,1)};
       
   101   }
       
   102 
       
   103   # Second work out what versions we need to process
       
   104   my @compVersions; # contains lots of [ comp_name, reldata ]
       
   105   foreach my $compName (@compsToProcess) {
       
   106     foreach (@{GetRelevantRelDataObjects($compName, $versionFilter)}) {
       
   107       push @compVersions, [ $compName, $_ ];
       
   108     }
       
   109   }
       
   110 
       
   111   die "No versions were found.\n" unless (@compVersions);
       
   112 
       
   113   # Third work out how verbose to make the output, and do it!
       
   114   if ($verbose) {
       
   115     my @tableData;
       
   116     InsertTableHeadings(\@tableData);
       
   117     AddTableEntry($$_[0], \@tableData, $$_[1]) foreach (@compVersions);
       
   118     print "\n";
       
   119     $iniData->TableFormatter->PrintTable(\@tableData, 1);
       
   120   } else {
       
   121     PrintMinimalVerbosity($$_[0], $$_[1]) foreach (@compVersions);
       
   122   }
       
   123 }
       
   124 
       
   125 sub PrintMinimalVerbosity {
       
   126   my $comp = shift;
       
   127   my $reldata = shift;
       
   128 
       
   129   my $ver = $reldata->Version();
       
   130   if ($userComp) {
       
   131     print "$ver\n";
       
   132   } else {
       
   133     print "$comp: $ver\n";
       
   134   }
       
   135 }
       
   136 
       
   137 sub GetRelevantRelDataObjects {
       
   138   my $comp = shift;
       
   139   my $ver = shift;
       
   140 
       
   141   my $shownum = $listAll;
       
   142   $shownum = 1 unless defined $listAll;
       
   143 
       
   144   my $relDataObjects = RelData->OpenSet($iniData, $comp, $verbose, $ver);
       
   145   $relDataObjects ||= [];
       
   146   splice(@$relDataObjects, $shownum) unless (scalar @$relDataObjects == 0) || ($shownum==0) || ($shownum > @$relDataObjects); # if showNum==0, we got just -a so show all
       
   147   
       
   148   return $relDataObjects;
       
   149 }
       
   150 
       
   151 sub InsertTableHeadings {
       
   152   my $tableData = shift;
       
   153 
       
   154   my @headings;
       
   155   @headings = ('Version', 'Internal version', 'Project', 'Releaser', 'Date');
       
   156   if ($verbose > 1) {
       
   157     @headings = ('', @headings);
       
   158   }
       
   159   if ($verbose > 2) {
       
   160     @headings = (@headings, 'Local path', 'Remote path');
       
   161   }
       
   162   if (!$userComp) {
       
   163     @headings = ('Component', @headings);
       
   164   }
       
   165   push @$tableData, \@headings;
       
   166 }
       
   167 
       
   168 sub AddTableEntry {
       
   169   my $comp = shift;
       
   170   my $tableData = shift;
       
   171   my $relData = shift;
       
   172 
       
   173   my @fields;
       
   174 
       
   175   my $ver = $relData->Version();
       
   176   my $releaser = $relData->NotesSource()->{releaser};
       
   177   chomp $releaser;
       
   178   my $project = $iniData->PathData->ComponentProject($comp, $ver);
       
   179   @fields = ($ver, $relData->InternalVersion(), $project, $releaser, $relData->NotesSource()->{date});
       
   180 
       
   181   # We're being very verbose, so add a 'status' field
       
   182   if ($verbose > 1) {
       
   183     my $status = "";
       
   184     # "Installed" flag
       
   185     #
       
   186     my $installedversion = EnvDb()->Version($comp);
       
   187     $status .= "I" if ($installedversion && $installedversion eq $ver);
       
   188 
       
   189     # "Exported" flag
       
   190     my $remotePath||="";
       
   191     eval {
       
   192       my $remoteSite = $iniData->RemoteSite(0); # 0 = not verbose
       
   193       if ($remoteSite) {
       
   194         $remotePath = $iniData->PathData->RemoteArchivePathForExistingComponent
       
   195           ($comp, $ver, $remoteSite);
       
   196         $status .= "E" if ($remotePath && $remoteSite->FileExists("$remotePath/$comp$ver.zip") );
       
   197       }
       
   198     };
       
   199 
       
   200     unshift @fields, $status;
       
   201 
       
   202     if ($verbose > 2) {
       
   203       # We're being extravagently verbose, so add two 'location' fields which will 
       
   204       # wrap off the end of the line - sigh.
       
   205       my $localPath = '"'. $iniData->PathData->LocalArchivePathForExistingComponent
       
   206         ($comp, $ver) .'"';
       
   207       $remotePath = '"'.$remotePath.'"' if ($remotePath);
       
   208       $remotePath ||= '';
       
   209       $localPath ||= '';
       
   210       @fields = (@fields, $localPath, $remotePath);
       
   211     }
       
   212   }
       
   213 
       
   214   # If we're listing several components, stick a field on the front
       
   215   # saying which component we're talking about in this case
       
   216   if (!$userComp) {
       
   217     unshift @fields, $comp;
       
   218   }
       
   219   push @$tableData, \@fields;
       
   220 }
       
   221 
       
   222 sub EnvDb {
       
   223   $envDb ||= EnvDb->Open($iniData, $verbose);
       
   224 }
       
   225 
       
   226 __END__
       
   227 
       
   228 =head1 NAME
       
   229 
       
   230 LatestVer - Prints the latest version of a particular component.
       
   231 
       
   232 =head1 SYNOPSIS
       
   233 
       
   234   latestver [options] [component]
       
   235 
       
   236 options:
       
   237 
       
   238   -h   help
       
   239   -a   lists all versions in descending date order
       
   240   -a <n>  lists the most recent 'n' versions in descending date order
       
   241   -v   verbose output (-vv very verbose, -vvv very very verbose)
       
   242   -f <wildcard> filter the versions displayed
       
   243 
       
   244 =head1 DESCRIPTION
       
   245 
       
   246 Prints the latest version of a particular component, or all known components if the component name is omitted. Optionally prints all versions in descending date order.
       
   247 
       
   248 If verbose output is requested, the internal version number, storage area ("project"), releaser's name and release time is also displayed.
       
   249 
       
   250 If very verbose output is requested, and the remote site is set in the reltools.ini file, a "flags" column will be shown with two possible letters:
       
   251 
       
   252 =over 4
       
   253 
       
   254 =item I
       
   255 
       
   256 This version is installed on your machine.
       
   257 
       
   258 =item E
       
   259 
       
   260 This version is exported.
       
   261 
       
   262 =back
       
   263 
       
   264 Using the -vv option may significantly increase the time this tool takes to run.
       
   265 
       
   266 If very very verbose is requested the local path is also output to screen.
       
   267 
       
   268 If you specify a -f version string then only versions whose version numbers contain that wildcard will be shown. The wildcard should be a glob-style regular expression (for example "*chinese*"). It is case-insensitive.
       
   269 
       
   270 If very very verbose output is requested, two more fields will show the location of the release on the local and remote archives.
       
   271 
       
   272 =head1 KNOWN BUGS
       
   273 
       
   274 None.
       
   275 
       
   276 =head1 COPYRIGHT
       
   277 
       
   278  Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
   279  All rights reserved.
       
   280  This component and the accompanying materials are made available
       
   281  under the terms of the License "Eclipse Public License v1.0"
       
   282  which accompanies this distribution, and is available
       
   283  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
   284  
       
   285  Initial Contributors:
       
   286  Nokia Corporation - initial contribution.
       
   287  
       
   288  Contributors:
       
   289  
       
   290  Description:
       
   291  
       
   292 
       
   293 =cut