| 1 | package OpenInteract::PackageRepository; | ||
| 2 | |||
| 3 | # $Id: PackageRepository.pm,v 1.8 2003/03/26 02:27:17 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Exporter SPOPS::HashFile ); | ||
| 7 | use vars qw( $PKG_DB_FILE ); | ||
| 8 | use File::Copy qw( copy move ); | ||
| 9 | use File::Spec; | ||
| 10 | use Data::Dumper qw( Dumper ); | ||
| 11 | use OpenInteract::Package; | ||
| 12 | use SPOPS::Utility; | ||
| 13 | |||
| 14 | $OpenInteract::PackageRepository::VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/); | ||
| 15 | @OpenInteract::PackageRepository::EXPORT_OK = qw( $PKG_DB_FILE ); | ||
| 16 | |||
| 17 | # Define our SPOPS configuration information. Very simple. | ||
| 18 | |||
| 19 | $OpenInteract::PackageRepository::C = { | ||
| 20 | class => 'OpenInteract::PackageRepository', | ||
| 21 | name => sub { return join( '-', $_[0]->{name}, $_[0]->{version} ) }, | ||
| 22 | }; | ||
| 23 | |||
| 24 | $OpenInteract::PackageRepository::RULESET = {}; | ||
| 25 | |||
| 26 | $PKG_DB_FILE = 'conf/package_repository.perl'; | ||
| 27 | sub package_filename { return File::Spec->catfile( $_[1], $PKG_DB_FILE ) } | ||
| 28 | |||
| 29 | sub CONFIG { return $OpenInteract::PackageRepository::C }; | ||
| 30 | sub RULESET { return $OpenInteract::PackageRepository::RULESET }; | ||
| 31 | |||
| 32 | use constant DEBUG => 0; | ||
| 33 | use constant META_KEY => 'META_INF'; | ||
| 34 | |||
| 35 | # Normal SPOPS initialization. Called by SPOPS.pm when you do: | ||
| 36 | # | ||
| 37 | # OpenInteract::PackageRepository->class_initialize; | ||
| 38 | |||
| 39 | sub _class_initialize { | ||
| 40 | my ( $class, $CONFIG ) = @_; | ||
| 41 | my $count = 1; | ||
| 42 | my $C = $class->CONFIG; | ||
| 43 | $C->{field} = {}; | ||
| 44 | foreach my $field ( @{ $C->{field_list} } ) { | ||
| 45 | $C->{field}{ $field } = $count; | ||
| 46 | $count++; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | # Similar to fetch() below -- allow the user to just specify the base | ||
| 52 | # installation or website directory and add the default filename to it | ||
| 53 | # to pass to SPOPS::HashFile. | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my ( $class, $p ) = @_; | ||
| 57 | if ( ! $p->{filename} and $p->{directory} ) { | ||
| 58 | $p->{filename} = File::Spec->catfile( $p->{directory}, $PKG_DB_FILE ); | ||
| 59 | } | ||
| 60 | $p->{perm} ||= 'new'; | ||
| 61 | return $class->SUPER::new( $p ); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub initialize { | ||
| 66 | my ( $self, $p ) = @_; | ||
| 67 | $self->{ META_KEY() } = { base_dir => $p->{directory} }; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | # Backup the repository | ||
| 72 | |||
| 73 | sub backup { | ||
| 74 | my ( $self, $p ) = @_; | ||
| 75 | return unless ( -f $p->{filename} ); | ||
| 76 | my $backup_name = $self->_backup_filename( $p ); | ||
| 77 | eval { copy( $p->{filename}, $backup_name ) || die $! }; | ||
| 78 | if ( $@ ) { | ||
| 79 | _w( 0, "Cannot backup repository: $@" ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub restore_backup { | ||
| 84 | my ( $self, $p ) = @_; | ||
| 85 | return unless ( -f $p->{filename} ); | ||
| 86 | my $backup_name = $self->_backup_filename( $p ); | ||
| 87 | my $corrupt_name = join( '', $p->{filename}, 'maybebad' ); | ||
| 88 | if ( -f $corrupt_name ) { | ||
| 89 | unlink( $corrupt_name ); | ||
| 90 | } | ||
| 91 | eval { move( $p->{filename}, $corrupt_name ) }; | ||
| 92 | if ( $@ ) { | ||
| 93 | _w( 0, "Failed to move repository to restore backup: $@" ); | ||
| 94 | } | ||
| 95 | eval { move( $backup_name, $p->{filename} ) }; | ||
| 96 | if ( $@ ) { | ||
| 97 | _w( 0, "Failed to restore backup for repository: $@" ); | ||
| 98 | } | ||
| 99 | _w( 1, "Backup file restored ok" ); | ||
| 100 | |||
| 101 | my $class = ref( $self ); | ||
| 102 | my $backup_object = $class->SUPER::fetch( $p->{filename}, $p ); | ||
| 103 | while ( my ( $key, $value ) = each %{ $backup_object } ) { | ||
| 104 | $self->{ $key } = $value; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub remove_backup { | ||
| 109 | my ( $self, $p ) = @_; | ||
| 110 | return unless ( -f $p->{filename} ); | ||
| 111 | my $backup_name = $self->_backup_filename( $p ); | ||
| 112 | return unless ( -f $backup_name ); | ||
| 113 | eval { unlink( $backup_name ) || die $! }; | ||
| 114 | if ( $@ ) { | ||
| 115 | _w( 0, "Cannot remove backup file: $@" ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _backup_filename { | ||
| 120 | my ( $self, $p ) = @_; | ||
| 121 | my $extension = $p->{extension} || 'backup'; | ||
| 122 | $extension = ".$extension" unless ( $extension =~ /^\./ ); | ||
| 123 | return join( '', $p->{filename}, $extension ); | ||
| 124 | } | ||
| 125 | |||
| 126 | # Ensure that the base_dir, name and version properties are defined | ||
| 127 | # for every package in the repository. Also remove each package's | ||
| 128 | # reference to the repository object before we serialize. | ||
| 129 | |||
| 130 | sub pre_save_action { | ||
| 131 | my ( $self, $p ) = @_; | ||
| 132 | foreach my $pkg_key ( keys %{ $self } ) { | ||
| 133 | next if ( $pkg_key eq META_KEY ); | ||
| 134 | unless ( -d $self->{ $pkg_key }{base_dir} ) { | ||
| 135 | warn( "Cannot save package repository: the OpenInteract base installation ", | ||
| 136 | "directory for package ($pkg_key) is not specified or does not exist!" ); | ||
| 137 | return undef; | ||
| 138 | } | ||
| 139 | unless ( $self->{ $pkg_key }{name} and $self->{ $pkg_key }{version} ) { | ||
| 140 | warn( "Cannot save package repository: both the package 'name' and 'version' ", | ||
| 141 | "must be specified for package ($pkg_key) before saving.\n" ); | ||
| 142 | return undef; | ||
| 143 | } | ||
| 144 | delete $self->{ $pkg_key }{repository}; | ||
| 145 | } | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | # Restore all the repository references | ||
| 151 | |||
| 152 | sub post_save_action { | ||
| 153 | my ( $self, $p ) = @_; | ||
| 154 | $self->_set_repository; | ||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | # Save a reference to the repository in each package info | ||
| 160 | |||
| 161 | sub post_fetch_action { | ||
| 162 | my ( $self, $filename, $p ) = @_; | ||
| 163 | $self->_set_repository; | ||
| 164 | return 1; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | # Set the {repository} key to the repository for all the package info | ||
| 169 | # hashrefs. | ||
| 170 | |||
| 171 | sub _set_repository { | ||
| 172 | my ( $self ) = @_; | ||
| 173 | foreach my $pkg_key ( keys %{ $self } ) { | ||
| 174 | next if ( $pkg_key eq META_KEY ); | ||
| 175 | DEBUG && _w( 1, "Setting repository in package key $pkg_key" ); | ||
| 176 | $self->{ $pkg_key }{repository} = $self; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | # Allow people to just specify a directory, then pass the constructed | ||
| 182 | # filename to the SPOPS::HashFile->fetch() method | ||
| 183 | |||
| 184 | sub fetch { | ||
| 185 | my ( $class, $filename, $p ) = @_; | ||
| 186 | $filename ||= ''; | ||
| 187 | $p ||= {}; | ||
| 188 | DEBUG && _w( 1, "Trying to fetch file [$filename] with info ", Dumper( $p ) ); | ||
| 189 | $p->{perm} ||= 'write'; | ||
| 190 | return $class->SUPER::fetch( $filename, $p ) if ( $filename ); | ||
| 191 | unless ( $p->{directory} ) { | ||
| 192 | die "Cannot open package repository without a filename or directory.\n"; | ||
| 193 | } | ||
| 194 | unless ( -d $p->{directory} ) { | ||
| 195 | die "Cannot open package repository in directory", | ||
| 196 | "[$p->{directory}]: the directory does not exist.\n"; | ||
| 197 | } | ||
| 198 | $filename = $class->package_filename( $p->{directory} ); | ||
| 199 | my $object = $class->SUPER::fetch( $filename, $p ); | ||
| 200 | $object->{ META_KEY() } ||= { base_dir => $p->{directory} }; | ||
| 201 | return $object; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | # Either saves/updates package info in the repository | ||
| 206 | |||
| 207 | sub save_package { | ||
| 208 | my ( $self, $info ) = @_; | ||
| 209 | my $pkg_key = $self->_make_package_key( $info ); | ||
| 210 | delete $info->{repository}; | ||
| 211 | $self->{ $pkg_key } = $info; | ||
| 212 | $info->{repository} = $self; | ||
| 213 | return $info; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _make_package_key { | ||
| 217 | my ( $self, $info ) = @_; | ||
| 218 | return join( '-', $info->{name}, $info->{version} ); | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | sub remove_package { | ||
| 223 | my ( $self, $info ) = @_; | ||
| 224 | my $pkg_key = $self->_make_package_key( $info ); | ||
| 225 | return delete $self->{ $pkg_key }; | ||
| 226 | } | ||
| 227 | |||
| 228 | # Retrieves the newest package given only a name; so if there are | ||
| 229 | # three packages installed: | ||
| 230 | # | ||
| 231 | # FirstPackage-1.14 | ||
| 232 | # FirstPackage-1.16 | ||
| 233 | # FirstPackage-1.80 | ||
| 234 | # | ||
| 235 | # This method will return the last, since it's the latest (higher | ||
| 236 | # version) You can, however, pass in a version; if it matches a | ||
| 237 | # package version exactly, that gets returned; otherwise it's still | ||
| 238 | # the highest | ||
| 239 | |||
| 240 | sub fetch_package_by_name { | ||
| 241 | my ( $self, $p ) = @_; | ||
| 242 | my $name = lc $p->{name}; | ||
| 243 | DEBUG && _w( 1, "Trying to retrieve package $name" ); | ||
| 244 | my @match = (); | ||
| 245 | foreach my $pkg_key ( keys %{ $self } ) { | ||
| 246 | next unless ( ref $self->{ $pkg_key } eq 'HASH' ); | ||
| 247 | if ( $self->{ $pkg_key }{name} eq $name ) { | ||
| 248 | push @match, $self->{ $pkg_key }; | ||
| 249 | DEBUG && _w( 1, "Found package $pkg_key; try to match up with package $name" ); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | my $final = undef; | ||
| 253 | my $ver = 0; | ||
| 254 | foreach my $info ( @match ) { | ||
| 255 | if ( $info->{version} > $ver ) { | ||
| 256 | $final = $info; | ||
| 257 | $ver = $info->{version}; | ||
| 258 | } | ||
| 259 | DEBUG && _w( 1, "Current version for matching $info->{name}: $ver" ); | ||
| 260 | return $info if ( $p->{version} and $info->{version} == $p->{version} ); | ||
| 261 | } | ||
| 262 | |||
| 263 | # If we wanted an exact match and didn't find it, return nothing, | ||
| 264 | # otherwise return the latest version | ||
| 265 | |||
| 266 | return undef if ( $p->{version} ); | ||
| 267 | return $final; | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | # Retrieve all packages in a repository | ||
| 272 | |||
| 273 | sub fetch_all_packages { | ||
| 274 | my ( $self ) = @_; | ||
| 275 | my @package_list = (); | ||
| 276 | foreach my $pkg_key ( sort keys %{ $self } ) { | ||
| 277 | push @package_list, $self->{ $pkg_key } unless ( $pkg_key eq META_KEY ); | ||
| 278 | } | ||
| 279 | return \@package_list; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | # Find a file in a particular package -- basically just pass the | ||
| 284 | # request on to OpenInteract::Package | ||
| 285 | |||
| 286 | sub find_file { | ||
| 287 | my ( $self, $pkg_info, @files ) = @_; | ||
| 288 | my $info = $pkg_info; | ||
| 289 | unless ( ref $info eq 'HASH' ) { | ||
| 290 | $info = $self->fetch_package_by_name({ name => $pkg_info }); | ||
| 291 | } | ||
| 292 | return undef unless ( scalar keys %{ $info } ); | ||
| 293 | return OpenInteract::Package->find_file( $info, @files ); | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | # Ensure that a list of packages actually exists in whichever context | ||
| 298 | # is specified. | ||
| 299 | |||
| 300 | sub verify_package { | ||
| 301 | my ( $self, @package_names ) = @_; | ||
| 302 | my $num_names = scalar @package_names; | ||
| 303 | my @pkg_exist = (); | ||
| 304 | foreach my $pkg_name ( @package_names ) { | ||
| 305 | my $info = $self->fetch_package_by_name({ name => $pkg_name }); | ||
| 306 | DEBUG && _w( 1, sprintf( "Verify package status %-20s: %s", | ||
| 307 | $pkg_name, ( $info ) ? "exists (Version $info->{version})" : 'does not exist' ) ); | ||
| 308 | push @pkg_exist, $info if ( scalar keys %{ $info } ); | ||
| 309 | } | ||
| 310 | return $pkg_exist[0] if ( $num_names == 1 ); | ||
| 311 | return \@pkg_exist; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | sub _w { | ||
| 316 | my $lev = shift; | ||
| 317 | return unless ( DEBUG >= $lev ); | ||
| 318 | my ( $pkg, $file, $line ) = caller; | ||
| 319 | my @ci = caller(1); | ||
| 320 | warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n"; | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | # Delegate to SPOPS::Utility | ||
| 325 | |||
| 326 | sub now { | ||
| 327 | shift; | ||
| 328 | return SPOPS::Utility->now( @_ ); | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | 1; | ||
| 333 | |||
| 334 | __END__ |