From: Chris W. <la...@us...> - 2005-03-09 20:26:37
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Manage/Website In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29942/lib/OpenInteract2/Manage/Website Modified Files: InstallPackage.pm Log Message: OIN-72: be able to install an OI2 package from a CPAN dist Index: InstallPackage.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Manage/Website/InstallPackage.pm,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** InstallPackage.pm 28 Feb 2005 02:03:10 -0000 1.16 --- InstallPackage.pm 9 Mar 2005 20:26:24 -0000 1.17 *************** *** 5,10 **** use strict; use base qw( OpenInteract2::Manage::Website ); ! use File::Spec::Functions qw( catfile ); ! use OpenInteract2::Context qw( CTX ); use OpenInteract2::Repository; --- 5,11 ---- use strict; use base qw( OpenInteract2::Manage::Website ); ! use File::Spec::Functions qw( catdir catfile ); ! use OpenInteract2::Context qw( CTX ); ! use OpenInteract2::Exception qw( oi_error ); use OpenInteract2::Repository; *************** *** 28,36 **** description => 'Package distribution filename to install to website', ! is_required => 'yes', }, }; } sub setup_task { my ( $self ) = @_; --- 29,59 ---- description => 'Package distribution filename to install to website', ! is_required => 'no', ! do_validate => 'yes', ! }, ! package_class => { ! description => ! 'Package distribution application class that can perform installation', ! is_required => 'no', ! do_validate => 'yes', }, }; } + sub validate_param { + my ( $self, $name, $value ) = @_; + if ( $name eq 'package_class' ) { + my $pkg_file = $self->param( 'package_file' ); + unless ( $value or $pkg_file ) { + return "Either 'package_class' or 'package_file' must be defined."; + } + return; + } + elsif ( $name eq 'package_file' ) { + return unless ( $value ); + } + return $self->SUPER::validate_param( $name, $value ); + } + sub setup_task { my ( $self ) = @_; *************** *** 43,102 **** sub run_task { my ( $self ) = @_; - my $package_file = $self->param( 'package_file' ); my %status = ( action => 'install package', - filename => $package_file, ); my $package = OpenInteract2::Package->new({ package_file => $package_file }); my $full_package_name = $package->full_name; ! my $repository = CTX->repository; ! my $rep_package = $repository->fetch_package( $package->name ); ! if ( $rep_package && $rep_package->version == $package->version ) { ! $status{is_ok} = 'yes'; ! $status{package} = $package->name; ! $status{version} = $package->version; ! $status{message} = ! sprintf( 'Package %s-%s not upgraded, ' . ! 'this version already installed', ! $package->name, $package->version ); } else { ! my $installed_package = eval { ! OpenInteract2::Package->install({ ! package_file => $package_file, ! repository => CTX->repository ! }) }; if ( $@ ) { ! $status{is_ok} = 'no'; ! $status{message} = "Error: $@"; ! } ! else { ! $status{is_ok} = 'yes'; ! $status{package} = $installed_package->name; ! $status{version} = $installed_package->version; ! $status{message} = ! sprintf( 'Installed package %s-%s to website %s', ! $installed_package->name, $installed_package->version, ! $self->param( 'website_dir' ) ); ! eval { ! $self->_create_temp_lib_refresh( $installed_package->name ) ! }; ! if ( $@ ) { ! $status{message} .= "\nNOTE: Could not create temp lib refresh " . ! "file, so you may need to delete it manually."; ! } } - $self->param( package => $installed_package ); } $self->notify_observers( progress => "Finished with installation of $full_package_name" ); ! $self->_add_status( \%status ); ! return; } # Let the site know that a new package has been installed by creating # the 'refresh' file for the temporary library --- 66,194 ---- sub run_task { my ( $self ) = @_; my %status = ( action => 'install package', ); + if ( my $package_file = $self->param( 'package_file' ) ) { + $self->_install_file( $package_file, \%status ); + } + elsif ( my $app_class = $self->param( 'package_class' ) ) { + $self->_install_app( $app_class, \%status ); + } + $self->_add_status( \%status ); + } + + sub _install_file { + my ( $self, $package_file, $status ) = @_; + $status->{filename} = $package_file; + my $package = OpenInteract2::Package->new({ package_file => $package_file }); + my $is_installed = $self->_check_package_exists( + $package->name, $package->version, $status ); + return if ( $is_installed ); + my $full_package_name = $package->full_name; ! my $installed_package = eval { ! OpenInteract2::Package->install({ ! package_file => $package_file, ! repository => CTX->repository ! }) ! }; ! if ( $@ ) { ! $status->{is_ok} = 'no'; ! $status->{message} = "Error: $@"; } else { ! $status->{is_ok} = 'yes'; ! $status->{package} = $installed_package->name; ! $status->{version} = $installed_package->version; ! $status->{message} = ! sprintf( 'Installed package %s-%s to website %s', ! $installed_package->name, $installed_package->version, ! $self->param( 'website_dir' ) ); ! eval { ! $self->_create_temp_lib_refresh( $installed_package->name ) }; if ( $@ ) { ! $status->{message} .= "\nNOTE: Could not create temp lib refresh " . ! "file, so you may need to delete it manually."; } } + $self->param( package => $installed_package ); $self->notify_observers( progress => "Finished with installation of $full_package_name" ); + } ! sub _install_app { ! my ( $self, $app_class, $status ) = @_; ! $status->{class} = $app_class; ! eval "require $app_class"; ! if ( $@ ) { ! $status->{is_ok} = 'no'; ! $status->{message} = "Failed to require '$app_class': $@"; ! return; ! } ! my $app = $app_class->new(); ! my $name = $app->name; ! my $version = $app->version; ! my $is_installed = $self->_check_package_exists( ! $name, $version, $status ); ! return if ( $is_installed ); ! ! my $brick_name = $app->get_brick_name(); ! unless ( $brick_name ) { ! oi_error "Cannot install from class $app_class - it does not have ", ! "method 'get_brick_name()' defined."; ! } ! my $brick = OpenInteract2::Brick->new( $brick_name ); ! ! my $base_pkg_dir = CTX->lookup_directory( 'package' ); ! my $pkg_dir = catdir( $base_pkg_dir, "$name-$version" ); ! ! $brick->copy_all_resources_to( $pkg_dir ); ! my $repository = CTX->repository; ! ! # the package directory is stocked, so instantiate ! my $package = OpenInteract2::Package->new({ ! directory => $pkg_dir, ! repository => $repository, ! }); ! ! # ...and copy the conf/ files to the site as working copies ! $package->copy_configuration_to_website(); ! ! $repository->add_package( $package ); ! ! $status->{is_ok} = 'yes'; ! $status->{package} = $name; ! $status->{version} = $version; ! $status->{message} = sprintf( ! 'Installed package %s-%s to website %s', ! $name, $version, $self->param( 'website_dir' ) ! ); ! ! $self->notify_observers( ! progress => "Finished with installation of $name-$version" ); } + sub _check_package_exists { + my ( $self, $name, $version, $status ) = @_; + my $repository = CTX->repository; + my $rep_package = $repository->fetch_package( $name ); + if ( $rep_package && $rep_package->version == $version ) { + $status->{is_ok} = 'yes'; + $status->{package} = $name; + $status->{version} = $version; + $status->{message} = + sprintf( 'Package %s-%s not upgraded, ' . + 'this version already installed', + $name, $version ); + return 1; + } + return 0; + } + + # Let the site know that a new package has been installed by creating # the 'refresh' file for the temporary library |