IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 13250


Ignore:
Timestamp:
May 4, 2007, 1:36:04 PM (19 years ago)
Author:
Paul Price
Message:

Adding new functions to support Nebulous.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-Config/lib/PS/IPP/Config.pm

    r13238 r13250  
    11# Copyright (c) 2006  Paul Price, Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.39 2007-05-04 19:41:23 eugene Exp $
     3# $Id: Config.pm,v 1.40 2007-05-04 23:36:04 price Exp $
    44
    55package PS::IPP::Config;
     
    1111
    1212use Carp qw( carp );
    13 use File::Spec;
    14 use PS::IPP::Metadata::Config 0.07;
    15 use Getopt::Long qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV
    16 use URI;
     13use File::Spec 3.19;
     14use PS::IPP::Metadata::Config 1.00;
     15use Getopt::Long 2.35 qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV
     16use URI 1.35;
     17use Nebulous::Client 0.02;
    1718
    1819use base qw( Class::Accessor::Fast Exporter );
     
    2021
    2122our @EXPORT_OK = qw(
    22     $PS_EXIT_SUCCESS
    23     $PS_EXIT_UNKNOWN_ERROR
    24     $PS_EXIT_SYS_ERROR
    25     $PS_EXIT_CONFIG_ERROR
    26     $PS_EXIT_PROG_ERROR
    27     $PS_EXIT_DATA_ERROR
    28     $PS_EXIT_TIMEOUT_ERROR
    29     metadataLookupStr
    30     metadataLookupMD
    31 );
     23                    $PS_EXIT_SUCCESS
     24                    $PS_EXIT_UNKNOWN_ERROR
     25                    $PS_EXIT_SYS_ERROR
     26                    $PS_EXIT_CONFIG_ERROR
     27                    $PS_EXIT_PROG_ERROR
     28                    $PS_EXIT_DATA_ERROR
     29                    $PS_EXIT_TIMEOUT_ERROR
     30                    metadataLookupStr
     31                    metadataLookupMD
     32                    caturi
     33                    );
    3234
    3335our $PS_EXIT_SUCCESS = 0;
     
    160162}
    161163
     164# Concatenate elements of a URI
     165sub caturi
     166{
     167    my $base = shift;           # Base name (might be "path://SOMETHING" or "neb://SOMETHING")
     168    my @segments = @_;          # Path segments
     169
     170    my $old = URI->new( $base ) or die "Unable to parse URI: $base\n"; # URI
     171    $old = $old->canonical();   # Clean up
     172    my $scheme = $old->scheme(); # Scheme, e.g., file, path
     173    unshift @segments, $old->path_segments();
     174
     175    for (my $i = 0; $i < scalar @segments; $i++) {
     176        splice( @segments, $i--, 1 ) unless $segments[$i] =~ /\S+/;
     177    }
     178
     179    my $new = URI->new();       # URI to return
     180    $new->scheme( $scheme );
     181    $new->authority( $old->authority() );
     182
     183    # Catch "neb:", "neb:/" and "neb://" for the base name
     184    my $opaque = $old->opaque();
     185    unshift( @segments, '', '' ) if $opaque eq '' or $opaque eq '/' or $opaque eq '//';
     186    $new->opaque( '/') if $old->opaque() eq '//';
     187
     188    $new->path_segments( @segments );
     189
     190    return $new->canonical()->as_string();
     191}
    162192
    163193# select a datapath from config.site
     
    182212}
    183213
     214# Start up Nebulous
     215sub _neb_start
     216{
     217    my $self = shift;           # Configuration object
     218
     219    return 1 if defined $self->{nebulous}; # Already started
     220
     221    my $server = metadataLookupStr( $self->{_ipprc}, 'NEB_SERVER' ); # Nebulous server
     222    unless (defined $server) {
     223        carp "Unable to find NEB_SERVER in camera configuration file.";
     224        exit($PS_EXIT_CONFIG_ERROR);
     225    }
     226
     227    my $neb = Nebulous::Client->new( proxy => $server );
     228    unless (defined $neb) {
     229        carp "Unable to find NEB_SERVER in camera configuration file.";
     230        exit($PS_EXIT_CONFIG_ERROR);
     231    }
     232
     233    $self->{nebulous} = $neb;
     234
     235    return 1;
     236}
     237
     238
     239# Resolve a URI to a file name
     240sub file_resolve
     241{
     242    my $self = shift;           # Configuration object
     243    my $name = shift;           # File name to check
     244
     245    my $uri = URI->new( $name );# URI parser
     246    my $scheme = $uri->scheme();# Scheme for file name
     247
     248    return $name unless defined $scheme; # Probably a file name instead of a URI
     249
     250    if ($scheme eq 'neb') {
     251        $name = _strip_scheme( $name );
     252        $self->_neb_start();
     253        my $neb = $self->{nebulous}; # Nebulous handle
     254        return $neb->find( $name );
     255    }
     256    if ($scheme eq 'path' or $scheme eq 'file') {
     257        return $self->convert_filename_absolute( $name );
     258    }
     259
     260    return $name;
     261}
     262
     263# Create and open file
     264sub file_create_open
     265{
     266    my $self = shift;           # Configuration object
     267    my $name = shift;           # File name to check
     268
     269    $self->file_prepare( $name );
     270
     271    my $uri = URI->new( $name );# URI parser
     272    my $scheme = $uri->scheme();# Scheme for file name
     273    if (defined $scheme) {
     274        if ($scheme eq 'neb') {
     275            $name = _strip_scheme( $name );
     276            $self->_neb_start();
     277            return $self->{nebulous}->open_create( $name );
     278        }
     279        if ($scheme eq 'path' or $scheme eq 'file') {
     280            $name = $self->convert_filename_absolute( $name );
     281        }
     282    }
     283
     284    if (-f $name) {
     285        carp "Unable to create file $name --- file exists.";
     286        exit($PS_EXIT_SYS_ERROR);
     287    }
     288
     289    my $fh;
     290    unless (open $fh, '>', $name) {
     291        carp "Unable to create file $name --- $!";
     292        exit($PS_EXIT_SYS_ERROR);
     293    }
     294    return $fh;
     295}
     296
     297# Create a file (intended principally to abstract Nebulous files)
     298sub file_create
     299{
     300    my $self = shift;           # Configuration object
     301    my $name = shift;           # File name to check
     302
     303    $self->file_prepare( $name );
     304
     305    my $uri = URI->new( $name );# URI parser
     306    my $scheme = $uri->scheme();# Scheme for file name
     307    if (defined $scheme and $scheme eq 'neb') {
     308        $name = _strip_scheme( $name );
     309        $self->_neb_start();
     310        return $self->{nebulous}->create( $name );
     311    }
     312
     313    return $name;
     314}
     315
     316# Copy a file
     317sub file_copy
     318{
     319    my $self = shift;           # Configuration object
     320    my $source = shift;         # Name of source file
     321    my $target = shift;         # Name of target file
     322
     323    $self->file_prepare( $target );
     324
     325    my $uri = URI->new( $target );# URI parser
     326    my $scheme = $uri->scheme();# Scheme for file name
     327    if (defined $scheme) {
     328        if ($scheme eq 'neb') {
     329            $target = _strip_scheme( $target );
     330            $self->_neb_start();
     331            $target = $self->{nebulous}->create( $target );
     332        } else {
     333            $target = $self->convert_filename_absolute( $target );
     334        }
     335    }
     336
     337    $source = $self->file_resolve( $source );
     338
     339    system "cp $source $target";
     340}
     341
     342# Strip the scheme (e.g., "neb://") off a URI name
     343sub _strip_scheme
     344{
     345    my $name = shift;           # File name of interest
     346    $name =~ s|^\S+:/*||;
     347    return $name;
     348}
     349
     350# Strip the filename (the last element of the URI), returning the scheme and directory part
     351sub _strip_filename
     352{
     353    my $name = shift;           # File name of interest
     354    $name =~ s|/[\w.-]*?$||;
     355    return $name;
     356}
     357
     358# Prepare to receive a new file --- create the directory, if appropriate.  Return the appropriate filename
     359# Does not register anything with Nebulous
     360sub file_prepare
     361{
     362    my $self = shift;           # Configuration object
     363    my $name = shift;           # File name for which to prepare
     364    my $workdir = shift;        # Working directory
     365    my $template = shift;       # Template filename from which to get working directory if
     366
     367    if (defined $workdir) {
     368        $name = caturi( $workdir, $name );
     369    } elsif (defined $template) {
     370        # Take directory from template and apply to name
     371        my $dir = _strip_filename( $template );
     372        $name = caturi( $dir, $name );
     373    }
     374
     375    my $uri = URI->new( $name ); # URI parser
     376    my $scheme = $uri->scheme(); # Scheme for file name
     377    return $name if defined $scheme and $scheme eq 'neb'; # Nothing to be done: Nebulous handles it all
     378
     379    # Might need to create a directory
     380    $name = $self->convert_filename_absolute( $name );
     381    my ( $vol, $dirs, $file ) = File::Spec->splitpath( $name );
     382    system "mkdir -p $dirs";
     383
     384    return $name;
     385}
     386
     387
     388# Convert a relative filename (e.g., "path://PATH/file") to an absolute (e.g., "/path/to/file")
    184389sub convert_filename_absolute
    185390{
     
    221426}
    222427
     428# Convert a relative filename (e.g., "/path/to/file") to an absolute (e.g., "path://PATH/file")
    223429sub convert_filename_relative
    224430{
     
    229435        carp "Programming error";
    230436        exit($PS_EXIT_PROG_ERROR);
     437    }
     438
     439    # First, check to see if it's already in a relative form
     440    my $scheme = URI->new( $name )->scheme();
     441    if ($scheme eq 'path' or $scheme eq 'file') {
     442        # We may as well search for a 'better' path
     443        $name = $self->convert_filename_absolute( $name );
     444    } elsif ($scheme eq 'neb') {
     445        # No chance of changing anything --- move along
     446        return $name;
    231447    }
    232448
Note: See TracChangeset for help on using the changeset viewer.