App-Glacier

 view release on metacpan or  search on metacpan

lib/App/Glacier/Command.pm  view on Meta::CPAN

    }

    $self->{_glacier} = new App::Glacier::Bre($self->config->as_hash('glacier'));
    if ($self->{_glacier}->lasterr) {
	$self->abend(EX_CONFIG, $self->{_glacier}->last_error_message);
    }
    return $self;
}

# Produce a semi-flat clone of $orig, blessing it into $class.
# The clone is semi-flat, because it shares the parsed configuration and
# the glacier object with the $orig.
sub clone {
    my ($class, $orig) = @_;
    my $self = $class->SUPER::clone($orig);
    $self->{_config} = $orig->config; 
    $self->{_glacier} = $orig->{_glacier};
    $self->{_jobdb} = $orig->{_jobdb};
    $self
}

sub option {
    my ($self, $opt, $val) = @_;
    if (defined($val)) {
	$self->{_options}{$opt} = $val;
    }
    return $self->{_options}{$opt};
}

sub touchdir {
    my ($self, $dir) = @_;
    unless (-d $dir) {
        make_path($dir, {error=>\my $err});
        if (@$err) {
            for my $diag (@$err) {
                my ($file, $message) = %$diag;
                $file = $dir if ($file eq '');
                $self->error("error creating $file: $message");
            }
            exit(EX_CANTCREAT);
        }
    }
}

sub jobdb {
    my $self = shift;
    unless ($self->{_jobdb}) {
        my $be = $self->cfget(qw(database job backend));
	$self->{_jobdb} = new App::Glacier::Roster(
	    $be,
	    $self->config->as_hash(qw(database job))
	);
    }
    return $self->{_jobdb};
}

sub describe_vault {
    my ($self, $vault_name) = @_;
    my $res = $self->glacier->Describe_vault($vault_name);
    if ($self->glacier->lasterr) {
	if ($self->glacier->lasterr('code') == 404) {
	    return undef;
	} else {
	    $self->abend(EX_FAILURE, "can't list vault: ",
			 $self->glacier->last_error_message);
	}
    }
    return timestamp_deserialize($res);
}

sub directory {
    my ($self, $vault_name) = @_;
    unless (exists($self->{_dir}{$vault_name})) {
	my $be = $self->cfget(qw(database inv backend));
	$self->{_dir}{$vault_name} =
	    new App::Glacier::Directory(
		$be,
		$vault_name,
		$self->glacier,
		$self->config->as_hash(qw(database inv))
	    );
    }
    return $self->{_dir}{$vault_name};
}

sub config { shift->{_config} }

sub glacier { shift->{_glacier} }

sub cfget {
    my ($self, @path) = @_;
    return $self->config->get(@path);
}

sub cf_transfer_param {
    my ($self, $type, $param) = @_;
    return $self->cfget('transfer', $type, $param)
	   || $self->cfget('transfer', $param);
}

sub run {
    my $self = shift;
    $self->abend(EX_SOFTWARE, "command not implemented");
}

sub getyn {
    my $self = shift;
    my $in;
    do {
	print "$self->{_progname}: @_? ";
	STDOUT->flush();
	$in = <STDIN>;
	$in =~ s/^\s+//;
    } while ($in !~ /^[yYnN]/);
        return $in =~ /^[yY]/;
}

sub set_time_style_option {
    my ($self, $style) = @_;
    
    eval {
	use App::Glacier::DateTime;
	my $x = new App::Glacier::DateTime(year=>1970);
	$x->canned_format($style);
    };
    if ($@) {
	$self->abend(EX_USAGE, "unrecognized time style: $style");
    }
    $self->{_options}{time_style} = $style;
}

sub format_date_time {
    my ($self, $obj, $field) = @_;
    return $obj->{$field}->canned_format($self->{_options}{time_style});
}

sub archive_cache_filename {
    my ($self, $vault_name, $archive_id) = @_;
    return File::Spec->catfile($self->cfget(qw(transfer download cachedir)),
			       $vault_name,
			       $archive_id);
}

sub check_job {
    my ($self, $key, $descr, $vault) = @_;

    $self->debug(2, "$descr->{JobId} $descr->{Action} $vault");
    if ($descr->{StatusCode} eq 'Failed') {
	$self->debug(1,
		     "deleting failed $key $vault "
		     . ($descr->{JobDescription} || $descr->{Action})
		     . ' '
		     . $descr->{JobId});
	$self->jobdb()->delete($key) unless $self->dry_run;
	return;
    }

    my $res = $self->glacier->Describe_job($vault, $descr->{JobId});
    if ($self->glacier->lasterr) {
	if ($self->glacier->lasterr('code') == 404) {
	    $self->debug(1,
			 "deleting expired $key $vault "
			 . ($descr->{JobDescription} || $descr->{Action})
			 . ' '
			 . $descr->{JobId});
	    App::Glacier::Job->fromdb($self, $vault, $key, $res)->delete()
		unless $self->dry_run;
	} else {
	    $self->error("can't describe job $descr->{JobId}: ",
			 $self->glacier->last_error_message);
	}
	return;
    } elsif (ref($res) ne 'HASH') {
	croak "describe_job returned wrong datatype (".ref($res).") for \"$descr->{JobId}\"";
    }
    return $res;
}

1;



( run in 1.708 second using v1.01-cache-2.11-cpan-13bb782fe5a )