head	1.9;
access;
symbols;
locks; strict;
comment	@# @;


1.9
date	2001.07.11.01.50.53;	author stack;	state Exp;
branches;
next	1.8;

1.8
date	2001.07.10.14.13.50;	author stack;	state Exp;
branches;
next	1.7;

1.7
date	2001.07.10.03.21.03;	author stack;	state Exp;
branches;
next	1.6;

1.6
date	2001.07.10.02.31.39;	author stack;	state Exp;
branches;
next	1.5;

1.5
date	2001.07.09.23.31.12;	author stack;	state Exp;
branches;
next	1.4;

1.4
date	2001.07.09.22.31.58;	author stack;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.09.21.33.34;	author stack;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.06.21.04.22;	author stack;	state Exp;
branches;
next	1.1;

1.1
date	2001.07.06.16.26.06;	author stack;	state Exp;
branches;
next	;


desc
@@


1.9
log
@Add TODO labels where script needs work.
@
text
@#!/usr/bin/perl -w
##
## Script to do in-situ upgrades.
##
## Based on shell script by evan that follows an outline done by EDK at 
## http://helm.tigris.org/migration/upgrade.html (This script does 
## steps A-E from that document).
##
## Script has been marked w/ 'TODO' where it (obviously) needs work and 
## functionality added.
##
## $Id: in-situ_upgrade.pl,v 1.8 2001/07/10 14:13:50 stack Exp $
##


## Contructor.
##
## Test that the SANDBOX env.sh has been sourced and that the sandbox
## makes sense. 
##
BEGIN
{
    if( ! $ENV{SANDBOX} )
    {
        die "ERROR: \$ENV{SANDBOX} undefined. Have you sourced env.sh.\n";
    }

    if( ! -d "$ENV{SANDBOX}" 
        || ! -f "$ENV{SANDBOX}/scripts/env.sh"
        || ! -x "$ENV{SANDBOX}/scripts/env_check" )
    {
        die "ERROR: $ENV{SANDBOX} does not point to a valid tigris install.\n";
    }

    my $error = `$ENV{SANDBOX}/scripts/env_check`;
    die "ERROR: $ENV{SANDBOX}/scripts/env_check failed: $error." if( $? );
}

## Destructor
##
# END { }


use Getopt::Long;
use File::Basename;
use File::Copy;
use Cwd;
use English;
use Carp;
use strict;


## Global variables
##
$::scriptName = basename( $0 );
$::sandbox = $ENV{SANDBOX};


## Program main
##
main( );

sub main
{
    my ( $localMods, $newRevision ) = cmdline_args( );
    if( defined( $localMods ) )
    { 
        checkForLocalMods( );
    }
    else
    {
        upgrade( $newRevision );
    }
}

## Check for local modifications in the sandbox we're updating.
##
## Step A. B. and C. from http://helm.tigris.org/migration/upgrade.html
##
sub checkForLocalMods
{
    checkLocalModsInConfFiles( );
    # TODO checkLocalModsInCVSFiles( );
}

## Look for mods in '.in' and 'conf' files.
## 
## Steps B. and C. from http://helm.tigris.org/migration/upgrade.html
##
sub checkLocalModsInConfFiles
{
    return unless( checkConfigureSupportForLocalMods( ) );

    my $silent = undef;
    my $sandbox_tmp = makeTmpDirectory( );

    my $configure_script = "$::sandbox/scripts/configure.pl";
    my $configure_dir = "$sandbox_tmp/configure_output";
    runConfigure( $configure_script, $configure_dir, $silent = 1 );

    my $reconfigure_script = "$::sandbox/scripts/reconfigure.pl";
    my $reconfigure_dir = "$sandbox_tmp/reconfigure_output";
    runConfigure( $reconfigure_script, $reconfigure_dir, $silent = 0 );

    # TODO: Complete
    #
    print "TO BE COMPLETED.\n";
}

## checkConfigureSupportForLocalMods
##
## If sandbox is old, then the version of configure.pl and reconfigure.pl 
## will not support checking for local revision. If this version of tigris
## does NOT support 'checking for local modifications', configure.pl will 
## return a string like the following: 'Usage: ./configure.pl [-b SANDBOX_DIR]'
##
## Returns true if we do support checking for local mods in '.in' originated
## files and in conf files.
##
sub checkConfigureSupportForLocalMods
{
    my $result = 0;

    my $output = `$::sandbox/scripts/configure.pl -h`;
    if( $output =~ m|Usage: (.*)configure.pl \[-b SANDBOX_DIR\]| )
    {
        print "INFO: 'Check local mods in .in and conf files' UNSUPPORTED.\n";
    }
    else
    {
        $result = 1;
    }

    return $result;
}

## Run configure commands saving output to alternate location.
## Note, reconfigure.pl doesn't support '--silent' flag.
##
sub runConfigure
{
    my( $configure_script, $configure_dir, $silent ) = @@_;
    makeDirectory( $configure_dir );
    my @@args = ( "$configure_script", 
        "--output-base=$configure_dir" );
    push( @@args, "--silent" ) if( $silent );
    system( @@args ) == 0 or die "System @@args failed: $?";
}

## Make a directory or fail in the attempt.
## Return name of directory we made (Convenience).
##
sub makeDirectory
{
    my( $directory ) = @@_;
    -d $directory 
        or mkdir( $directory, 0777 ) 
        or die "Failed making dir $directory: $!";
    return $directory;
}

## Make temporary directory for use by this script while running.
## 
## Returns name of directory we made.
##
sub makeTmpDirectory
{
    $::scriptName =~ m|(.*)\.[^\.]*$|; # Strip suffix.
    my $baseName = "${1}_output";
    makeDirectory( "$::sandbox/tmp" );
    return makeDirectory( "$::sandbox/tmp/$baseName" );
}

## Run the in-situ upgrade.
## 
## Save off the current env.sh.  Then for the current dir and each subdir, 
## do a cvs update -Pd to new version.  Restore env.sh
##
## TODO: Catch output into a log file.
## TODO: Allow specification of CVS USER.
##
## Step E. from http://helm.tigris.org/migration/upgrade.html.
##
sub upgrade
{
    my( $newRevision ) = @@_;

    my $oldRevision = getCurrentRevision( ); 
    my $sandbox_tmp = makeTmpDirectory( );
    $::scriptName =~ m|(.*)\.[^\.]*$|;
    my $env_sh = "env.sh.$1.$$";
    my $saved_env = "$sandbox_tmp/$env_sh";
    my @@args = ( "cvs", "-d", ":pserver:guest\@@cvs.tigris.org:/cvs", "update", 
        "-dP", "-r$newRevision" ); 
    my $instance = $ENV{INSTANCE};
    my $instanceDir = "$::sandbox/$instance";

    # Do some basic sanity tests.
    #
    die "No INSTANCE dir: $instanceDir" unless(  -d "$instanceDir" ); 
    preprocess105( ) if( $oldRevision =~ m|TIGRIS_1_0_5.*| );

    move( "$::sandbox/scripts/env.sh", "$saved_env" )
        or die "Move of env.sh to $sandbox_tmp failed: $!";
    updateDir( 1, $::sandbox, \@@args, $instance );
    move( "$saved_env", "$::sandbox/scripts/" )
        or die "Move of $saved_env failed: $!";

    print <<"FINISHED";
===============================================================================
COMPLETED upgrade from $oldRevision to $newRevision

NEXT: Compare the saved off env.sh w/ the freshly checked out one.

 % diff $::sandbox/scripts/$env_sh $::sandbox/scripts/env.sh 

Look for new tokens in env.sh that are not in $env_sh. Reconcile, then 
replace env.sh with $env_sh.
===============================================================================
FINISHED
}

## Do special processing of TIGRIS_1_0_5 version before running update.
##
sub preprocess105
{
    my $dirToRemove = undef;

    # Remove the ./helm/data/cvs/repository/CVSROOT/Emptydir and the 
    # lock directory.  They don't have CVS info so causes cvs to exit.
    #
    $dirToRemove = "$::sandbox/helm/data/cvs/repository/CVSROOT/Emptydir";
    if( -d $dirToRemove )
    {
        rmdir( $dirToRemove ) or die( "Failed to remove $dirToRemove" );
    }
    $dirToRemove = "$::sandbox/helm/data/cvs/lock";
    if( -d $dirToRemove )
    {
        rmdir( $dirToRemove ) or die( "Failed to remove $dirToRemove" );
    }
}

## Run cvs update in current directory
## Don't run cvs update in dirs that do not have CVS subdirs.
##
sub updateDir
{
    my ( $recurse, $startDir, $argsRef, $dirToExclude ) = @@_;
    my $originalDir = cwd( );
    chdir( $startDir );
    print "RUNNING @@$argsRef in ", cwd( ), ".\n";

    my @@argsLocal = ( @@$argsRef, "-l" );
    system( @@argsLocal ) == 0 
	or die "System @@argsLocal failed in $startDir: $?";

    opendir( DIRS, "$startDir" ) or die "Failed to opendir $startDir: $!";
    my @@dirs = readdir( DIRS );
    close( DIRS );
    DIRS: foreach my $dir ( @@dirs )
    {
        next DIRS if( $dir =~ m/^\.|^CVS$/ );
        next DIRS if( $dirToExclude and $dir =~ m/$dirToExclude/ );
        next DIRS if( ! -d "$startDir/$dir/CVS" );
        chdir( "$dir" );
        if( $recurse > 0 ) 
        {
            updateDir( $recurse - 1, "$startDir/$dir", $argsRef, undef );
        }
        else 
        {
            system( @@$argsRef ) == 0 
                or die "System @@$argsRef failed in $startDir/$dir: $?";
            chdir( ".." );
        }
    }     
    chdir( $originalDir );
}

## Get current tigris revision.
## Read it from SANDBOX/conf/version.properties.
##
## Step D. from http://helm.tigris.org/migration/upgrade.html
## 
sub getCurrentRevision
{
    my $currentVersion = undef;
    my $version = "$::sandbox/conf/version.properties";

    open( FILEHANDLE, $version ) or die "Cannot open $version for read: $!";
    READING: while( <FILEHANDLE> ) 
    {
        if( $_ =~ m|^TIGRIS_VERSION=(.*)| )
        {
            my $version = $1;
            $version =~ s|\.|_|g;
            $currentVersion = "TIGRIS_$version";
            last READING;
        }
    }
    close( FILEHANDLE );

    return $currentVersion;
}

## Process command-line arguments.
## Does not return if user asked for help. 
## Returns cmd-line processing results.
##
sub cmdline_args
{
    my $optHelp = undef;
    my $optLocalMods = undef;
    my $optNewRevision = undef;

    my $result = GetOptions( "h" => \$optHelp,
                                "help" => \$optHelp,
                                "c" => \$optLocalMods,
                                "check" => \$optLocalMods,
                                "u=s" => \$optNewRevision,
                                "upgrade=s" => \$optNewRevision );        

    $result || die "GetOptions error.";

    if( $#ARGV > -1 )
    {
        print "ERROR: Unexpected arguments: @@ARGV.\n";
        usage( );
    }

    usage( ) if( defined( $optHelp ) );

    # Must specify either local-mod check or run upgrade, not both 
    # or neither.
    #
    if( not ( defined( $optLocalMods ) or defined( $optNewRevision ) )
        or ( defined( $optLocalMods ) and defined( $optNewRevision ) ) )
    {
        print( "ERROR: Specify either '--check' or '--upgrade'.\n" );
        usage( );
    }

    if( defined( $optNewRevision ) )
    { 
        unless( $optNewRevision =~ m|^TIGRIS_[0-9]+_[0-9]+_[0-9]+[A-Z]*[0-9]*$| ) 
        {
            print "ERROR: $optNewRevision is incorrectly formatted.\n";
            usage( );
        }
    }

    return ( $optLocalMods, $optNewRevision );
}


## Usage
##
sub usage
{
    
    print <<"SCRIPT_USAGE";
Usage: $::scriptName ((-h|--help)|(-c|--check)|(-u NEW|--upgrade=NEW))

Options:
 -h,--help             Prints this message
 -c,--check            Check for local-modifications, if any.
 -u NEW,--upgrade=NEW   Upgrade in-situ to designated version.  Version 
                       ('NEW') expected format is 'TIGRIS_n_n_n' where 'n' 
                       is a one or more digits.

This script is used for in-situ upgrades of tigris.  Use it to first check 
for local modifications and then to run the actual in-situ upgrade. For an 
outline of what this script does, see in-situ upgrade page at 
http://helm.tigris.org/migration/upgrade.html.

SCRIPT_USAGE
    
    exit( 0 );
}
@


1.8
log
@Add in some T105 preprocessing.  Clean up of 'success' note.
@
text
@d9 2
a10 5
## 
## NOTES:
## + Fails if there is a conflict during checkout.  This is good.  But so 
## you can see the failure, must let the cvs checkout be verbose.  Means 
## that the output is messy.  Save it to a file and examine.
d12 1
a12 1
## $Id: in-situ_upgrade.pl,v 1.7 2001/07/10 03:21:03 stack Exp $
d83 1
a83 1
    # checkLocalModsInCVSFiles( );
d105 1
a105 1
    # TODO
d180 1
a180 1
## TODO: Allow specification of USER.
d190 2
a191 1
    my $env_sh = "env.sh.$$";
@


1.7
log
@Make compare to 'CVS' tighter so the CVS.mozilla gets updated.
Print out better fail strings that list where dir fail happened in.
@
text
@d15 1
a15 1
## $Id: in-situ_upgrade.pl,v 1.6 2001/07/10 02:31:39 stack Exp $
d193 2
a194 1
    my $saved_env = "$sandbox_tmp/env.sh.$$";
d200 2
d203 1
d207 1
a207 1
    updateDir( 2, $::sandbox, \@@args, $instance );
a210 1
    my $tmpStr = basename( $saved_env ); 
d215 6
a220 2
NEXT: Compare ./scripts/$tmpStr to ./scripts/env.sh looking for new tokens
in env.sh not in $tmpStr. Reconcile, then replace env.sh w/ $tmpStr.
d225 20
@


1.6
log
@Test for CVS presence in all subdirs before doing cvs update in dir.
@
text
@d15 1
a15 1
## $Id: in-situ_upgrade.pl,v 1.5 2001/07/09 23:31:12 stack Exp $
d190 1
d194 2
a197 2
    my @@args = ( "cvs", "-d", ":pserver:guest\@@cvs.tigris.org:/cvs", "update", 
        "-dP", "-r$newRevision" ); 
d200 1
d230 2
a231 1
    system( @@argsLocal ) == 0 or die "System @@argsLocal failed: $?";
d238 1
a238 1
        next DIRS if( $dir =~ m/^\.|CVS/ );
d249 1
a249 1
                or die "System @@$argsRef failed in $dir: $?";
@


1.5
log
@Have the script keep going even if cvs report error.
@
text
@d15 1
a15 1
## $Id: in-situ_upgrade.pl,v 1.4 2001/07/09 22:31:58 stack Exp $
a189 3

    my $result = 0;
    my $resultStr = '';
d192 12
a203 5
    my $instance = getINSTANCE( );
    my $originalDir = cwd( );
    my @@args = ( "cvs", "-d", ":pserver:guest\@@cvs.tigris.org:/cvs", 
        "update", "-dP", "-r$newRevision" ); 
    my @@argsLocal = ( @@args, "-l" );
d205 4
a208 5
    # Do top-level sandbox directory first.
    #
    chdir( $::sandbox );
    print "RUNNING @@argsLocal in ", cwd( ), ".\n";
    system( @@argsLocal ); # Fix this.  == 0 or die "System @@argsLocal failed: $?";
d210 4
a213 30
    # Do each of the sandbox sub-directories in turn.
    #
    opendir( SANDBOX_DIRS, "$::sandbox" )
        or die "Failed to opendir $::sandbox: $!";
    my @@sandbox_dirs = readdir( SANDBOX_DIRS );
    close( SANDBOX_DIRS );
    my $user = $ENV{USER};
    die "USER is not defined." unless( $user );
    move( "$::sandbox/scripts/env.sh", "$sandbox_tmp" )
        or die "Move of env.sh to $sandbox_tmp failed: $!";
    DIRS: foreach my $dir ( @@sandbox_dirs )
    {
        next DIRS if( $dir =~ m/^\.|CVS|tmp|$instance/ );
        my $dir_fullpath = "$::sandbox/$dir";
        next DIRS if( ! -d "$dir_fullpath/CVS" );
        chdir( "$dir_fullpath" );
        print "RUNNING @@args in $dir_fullpath.\n";
        $result = system( @@args );
        if( $result )
        {
            $resultStr = "FAILED: System @@args in $dir_fullpath"
                . " (Were there conflicts?): $?";
            # Keep going.  Fix.  last DIRS;
        }
    }     
    move( "$sandbox_tmp/env.sh", "$::sandbox/scripts/env.sh" )
        or die "Move of env.sh from $sandbox_tmp failed: $!";
    chdir( $originalDir );
    die $resultStr if( $result );
    print "COMPLETED upgrade from $oldRevision to $newRevision\n";
d216 3
a218 1
## Get INSTANCE
d220 1
a220 1
sub getINSTANCE
d222 22
a243 6
    my $instance = ''; 
    my $env = "$::sandbox/scripts/env.sh";
    open( FILEHANDLE, $env ) or die "Cannot open $env for read: $!";
    READING: while( <FILEHANDLE> ) 
    {
        if( m|^INSTANCE=(.*)| )
d245 3
a247 3
            $instance = $1;
            $instance =~ s|^[\'\"](.*)[\'\"]|$1|; # Strip quotes if any.
            last READING;
d249 2
a250 4
    }
    close( FILEHANDLE ); 
    die "No INSTANCE defined in $env" unless( $instance );
    return $instance;
@


1.4
log
@Make guest do all cvs work.
@
text
@d15 1
a15 1
## $Id: in-situ_upgrade.pl,v 1.3 2001/07/09 21:33:34 stack Exp $
d205 1
a205 1
    system( @@argsLocal ) == 0 or die "System @@argsLocal failed: $?";
d229 1
a229 1
            last DIRS;
@


1.3
log
@Make cvs logging verbose.  Allow version strings w/ RC1 type endings.
@
text
@d15 1
a15 1
## $Id: in-situ_upgrade.pl,v 1.2 2001/07/06 21:04:22 stack Exp $
d197 3
a199 3
    my @@args = undef;
    my @@argsCVS = ( "cvs" );
    my @@argsUpdate = ( "update", "-dP", "-r$newRevision" );
d204 2
a205 3
    @@args = ( @@argsCVS, @@argsUpdate, "-l" ); 
    print "RUNNING @@args in ", cwd( ), ".\n";
    system( @@args ) == 0 or die "System @@args failed: $?";
a214 1
    @@args = ( @@argsCVS, "-d", ":pserver:$user\@@cvs.tigris.org:/cvs", @@argsUpdate ); 
@


1.2
log
@Will now do rough upgrade. Still not complete.
@
text
@d9 7
a15 1
## $Id: in-situ_upgrade.pl,v 1.1 2001/07/06 16:26:06 stack Exp $
d198 1
a198 1
    my @@argsCVS = ( "cvs", "-Q" );
d205 1
a205 1
    print "Running @@args in ", cwd( ), ".\n";
d225 1
a225 1
        print "Running @@args in $dir_fullpath.\n";
d229 2
a230 1
            $resultStr = "System @@args failed: $?";
d327 1
a327 1
        unless( $optNewRevision =~ m|^TIGRIS_[0-9]+_[0-9]+_[0-9]+$| ) 
a356 10

Example usage: 

 % $::scriptName --upgrade=TIGRIS_1_0_7

or

 % $::scriptName --check

This script is INCOMPLETE.
@


1.1
log
@Script to do in-situ migrations.  Follows the outline given here:
http://helm.tigris.org/migration/upgrade.html.  Not yet complete.
@
text
@d9 1
a9 1
## $Id: getoptions.pl,v 1.2 2001/06/21 01:15:40 stack Exp $
d44 1
d94 1
a94 1
    my $configure_script = "$ENV{SANDBOX}/scripts/configure.pl";
d98 1
a98 1
    my $reconfigure_script = "$ENV{SANDBOX}/scripts/reconfigure.pl";
d102 2
d121 1
a121 1
    my $output = `$ENV{SANDBOX}/scripts/configure.pl -h`;
d167 2
a168 1
    return makeDirectory( "$ENV{SANDBOX}/tmp/$baseName" );
d176 3
d184 3
d189 5
a193 3
    
    move( "$ENV{SANDBOX}/scripts/env.sh", "$sandbox_tmp" )
        or die "Move of env.sh to $sandbox_tmp failed: $!";
d195 1
a195 7
     
    # Then for each dir in sandbox w/ a CVS dir EXCEPT INSTANCE, run  
    #         ( cvs update -dP -r$CVS_NEW_TAG -l ;
    #      for dir in * ; do if [ -d "$dir/CVS" ] && [ $dir != "$INSTANCE" ]; then
    #            echo "UPDATING $dir" ;
    #            cd $dir ; cvs -d :pserver:$USER@@cvs.tigris.org:/cvs update -dP -r$CVS_NEW_TAG ; cd .. ; fi
    #      done ) 2>&1 
d197 4
d202 26
a227 1
    move( "$sandbox_tmp/env.sh", "$ENV{SANDBOX}/scripts/env.sh" )
d229 4
d234 19
a252 1
    print "UNIMPLEMENTED upgrade from $oldRevision to $newRevision\n";
d296 1
a296 1
                                "update=s" => \$optNewRevision );        
d314 1
a314 1
        print( "ERROR: Specify either '--check' or '--update'.\n" );
d337 1
a337 1
Usage: $::scriptName ((-h|--help)|(-c|--check)|(-u NEW|--update=NEW))
d342 1
a342 1
 -u NEW,--update=NEW   Upgrade in-situ to designated version.  Version 
d353 1
a353 1
 % $::scriptName --update=TIGRIS_1_0_7
@

