Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #50 from techman83/feature/mirror_checking
Browse files Browse the repository at this point in the history
Check archive for missed uploads - closes #49
  • Loading branch information
techman83 committed May 19, 2016
2 parents 0f5188f + d55fb4f commit 4cb1948
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
54 changes: 43 additions & 11 deletions bin/mirror-ckan
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use v5.010;
use strict;
use autodie qw(:all);
use App::KSP_CKAN::Tools::Config;
use App::KSP_CKAN::Tools::Git;
use App::KSP_CKAN::Mirror;
use Try::Tiny;
use Getopt::Long;
use File::Spec;
use File::chdir;
use File::Path qw( mkpath );

# PODNAME: mirror-ckan

Expand Down Expand Up @@ -48,24 +52,26 @@ $PROGNAME ||= 'mirror-ckan';
my $DEBUG = 0;
my $filename;

# TODO: It'd be nice to specify a path/multiple files
my $getopts_rc = GetOptions(
"version" => \&version,
"debug!" => \$DEBUG,
"ckan=s" => \$filename,

"help|?" => \&print_usage,
);

# TODO: Allow config to be specified
my $working = $ENV{HOME}."/CKAN-Webhooks/mirror";
if ( ! -d $working ) {
mkpath($working);
}
my $config = App::KSP_CKAN::Tools::Config->new(
working => $working,
debugging => $DEBUG,
);
my $mirror = App::KSP_CKAN::Mirror->new( config => $config );

$mirror->upload_ckan($filename);
# TODO: It'd be nice to specify a path/multiple files
my $getopts_rc = GetOptions(
"version" => \&version,
"debug!" => \$DEBUG,
"ckan=s" => \$filename,
"yesterday" => \&yesterday,

exit 0;
"help|?" => \&print_usage,
);

sub version {
$::VERSION ||= "Unreleased";
Expand All @@ -79,6 +85,8 @@ sub print_usage {
mirror-ckan --ckan /path/to/file.ckan : Takes a ckan file and mirrors it to the
: Internet Archive.
mirror-ckan --yesterday : Scans CKAN-meta for files commited the day
: before and attempts to archive them.
Debugging commands:
Expand All @@ -91,4 +99,28 @@ sub print_usage {
exit 1;
}

sub yesterday {
my $git = App::KSP_CKAN::Tools::Git->new(
remote => $config->CKAN_meta,
local => $config->working,
);
my @files = $git->yesterdays_diff;
local $CWD = $config->working."/".$git->working;

foreach my $file ( @files ) {
if ( $file =~ /\.ckan$/ && -e $file ) {
say "Checking mirror for $file" if $DEBUG;
try {
$mirror->upload_ckan($file);
};
}
}

exit 0;
}

# This is the default
$mirror->upload_ckan($filename);

exit 0;
__END__
21 changes: 20 additions & 1 deletion lib/App/KSP_CKAN/Tools/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Method::Signatures 20140224;
use Carp qw(croak);
use Try::Tiny;
use Git::Wrapper;
use Capture::Tiny qw(capture);
use Capture::Tiny qw(capture capture_stdout);
use File::chdir;
use File::Path qw(remove_tree mkpath);
use Moo;
Expand Down Expand Up @@ -248,4 +248,23 @@ method pull(:$ours?,:$theirs?) {
return;
}

=method yesterdays_diff
$git->yesterdays_diff;
Produces a list of files of changes since yesterday.
git diff $(git rev-list -n1 --before="yesterday" master) --name-only
=cut

# TODO: It'd be cool to be able to test this
method yesterdays_diff {
my $branch = $self->branch;
local $CWD = $self->local."/".$self->working;
my $changed = capture_stdout { system("git diff \$(git rev-list -n1 --before=\"yesterday\" $branch) --name-only") };
chomp $changed;
return split("\n", $changed);
}

1;

0 comments on commit 4cb1948

Please sign in to comment.