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 #30 from techman83/status_tracking
Browse files Browse the repository at this point in the history
Status tracking
  • Loading branch information
techman83 committed Aug 30, 2015
2 parents 4089ae6 + db71725 commit 6acfc2c
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 132 deletions.
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ stopwords = CKAN
stopwords = netkan
stopwords = api
stopwords = KSP
stopwords = BaconLabs

[AutoPrereqs]

Expand Down
91 changes: 54 additions & 37 deletions lib/App/KSP_CKAN/NetKAN.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Method::Signatures 20140224;
use Scalar::Util::Reftype;
use File::chdir;
use Carp qw( croak );
use App::KSP_CKAN::Status;
use App::KSP_CKAN::Tools::Http;
use App::KSP_CKAN::Tools::Git;
use App::KSP_CKAN::Tools::NetKAN;
Expand Down Expand Up @@ -43,33 +44,55 @@ has 'config' => ( is => 'ro', required => 1, isa => $Ref );
has '_http' => ( is => 'ro', lazy => 1, builder => 1 );
has '_CKAN_meta' => ( is => 'ro', lazy => 1, builder => 1 );
has '_NetKAN' => ( is => 'ro', lazy => 1, builder => 1 );
has '_status' => ( is => 'rw', lazy => 1, builder => 1 );

method _build__http {
return App::KSP_CKAN::Tools::Http->new();
}

method _build__CKAN_meta {
return App::KSP_CKAN::Tools::Git->new(
remote => $self->config->CKAN_meta,
local => $self->config->working,
clean => 1,
remote => $self->config->CKAN_meta,
local => $self->config->working,
clean => 1,
);
}

method _build__NetKAN {
return App::KSP_CKAN::Tools::Git->new(
remote => $self->config->NetKAN,
local => $self->config->working,
clean => 1,
remote => $self->config->NetKAN,
local => $self->config->working,
clean => 1,
);
}

method _build__status {
return App::KSP_CKAN::Status->new(
config => $self->config,
);
}

method _mirror_files {
my $config = $self->config;

# netkan.exe
$self->_http->mirror(
url => $self->config->netkan_exe,
path => $self->config->working."/netkan.exe",
exe => 1,
url => $config->netkan_exe,
path => $config->working."/netkan.exe",
exe => 1,
);

# ckan-validate.py
$self->_http->mirror(
url => $config->ckan_validate,
path => $config->working."/ckan-validate.py",
exe => 1,
);

# CKAN.schema
$self->_http->mirror(
url => $config->ckan_schema,
path => $config->working."/CKAN.schema",
);

return;
Expand All @@ -81,38 +104,26 @@ method _inflate_all(:$rescan = 1) {
$self->_NetKAN->pull;
local $CWD = $self->config->working."/".$self->_NetKAN->working;
foreach my $file (glob("NetKAN/*.netkan")) {

# TODO: We're already passing in the config, is it really
# necessary to pass in each of the other things as attributes?
my $config = $self->config;
my $netkan = App::KSP_CKAN::Tools::NetKAN->new(
config => $self->config,
netkan => $self->config->working."/netkan.exe",
cache => $self->config->working."/cache",
token => $self->config->GH_token,
file => $file,
ckan_meta => $self->config->working."/".$self->_CKAN_meta->working,
rescan => $rescan,
config => $config,
netkan => $config->working."/netkan.exe",
cache => $config->working."/cache",
token => $config->GH_token,
file => $file,
ckan_meta => $self->_CKAN_meta,
status => $self->_status,
rescan => $rescan,
);
$netkan->inflate;
}
return;
}

method _commit {
$self->_CKAN_meta->add;
my @changes = $self->_CKAN_meta->changed;

foreach my $changed (@changes) {
my $file = $self->config->working."/".$self->_CKAN_meta->working."/".$changed;
if ( $self->validate($file) ) {
$self->warn("Failed to Parse $changed");
$self->_CKAN_meta->reset(file => $file);
}
else {
$self->info("Commiting $changed");
$self->_CKAN_meta->commit(
file => $file,
message => "NetKAN generated mods - $changed",
);
}
}
method _push {
$self->_CKAN_meta->pull(ours => 1);
$self->_CKAN_meta->push;
return;
Expand All @@ -128,7 +139,10 @@ it into CKAN-meta (or whichever repository is configured)
method full_index {
$self->_mirror_files;
$self->_inflate_all;
$self->_commit;
if ( ! $self->is_debug() ) {
$self->_push;
$self->_status->write_json;
}
return;
}

Expand All @@ -145,10 +159,13 @@ inflate metadata when required.
method lite_index {
$self->_mirror_files;
$self->_inflate_all( rescan => 0 );
$self->_commit;
if ( ! $self->is_debug() ) {
$self->_push;
$self->_status->write_json;
}
return;
}

with('App::KSP_CKAN::Roles::Logger','App::KSP_CKAN::Roles::Validate');
with('App::KSP_CKAN::Roles::Logger');

1;
24 changes: 4 additions & 20 deletions lib/App/KSP_CKAN/Roles/Validate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,13 @@ use Moo::Role;
=head1 DESCRIPTION
Currently lightly wraps ckan_validate.py
Currently lightly wraps ckan_validate.py. Requires ckan_validate.py
and CKAN.schema be in '$config->working'. Also requires consuming
class to have the Logger role.
=cut

requires '_http', 'config', '_mirror_files', 'warn';

after _mirror_files => sub {
my $self = shift;

# ckan-validate.py
$self->_http->mirror(
url => $self->config->ckan_validate,
path => $self->config->working."/ckan-validate.py",
exe => 1,
);

# CKAN.schema
$self->_http->mirror(
url => $self->config->ckan_schema,
path => $self->config->working."/CKAN.schema",
exe => 1,
);
};
requires 'config', 'warn';

=method validate
Expand Down
124 changes: 124 additions & 0 deletions lib/App/KSP_CKAN/Status.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package App::KSP_CKAN::Status;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use Carp qw( croak );
use File::Slurper qw(read_text write_text);
use Try::Tiny;
use JSON;
use File::Path qw(mkpath);
use App::KSP_CKAN::Status::NetKAN;
use Moo;
use MooX::HandlesVia;
use namespace::clean;

# ABSTRACT: NetKAN Status Tracker

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS
use App::KSP_CKAN::Status;
my $status = App::KSP_CKAN::Status->new(
config => $config,
);
=head1 DESCRIPTION
Status object for tracking status of at least initially, NetKAN
indexing.
=cut

my $Ref = sub {
croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config");
};

has 'config' => ( is => 'ro', required => 1, isa => $Ref );
has 'status_file' => ( is => 'ro', default => sub { "netkan.json" } );
has 'status_path' => ( is => 'ro', lazy => 1, builder => 1 );
has '_status_file' => ( is => 'ro', lazy => 1, builder => 1 );
has '_data' => ( is => 'ro', lazy => 1, builder => 1 );
has '_json' => ( is => 'ro', lazy => 1, builder => 1 );

has 'status' => (
is => 'rw',
handles_via => 'Hash',
handles => {
get_val => 'get',
set_val => 'set',
all_keys => 'keys'
},
default => sub { { } },
);

method _build__json {
return JSON->new->allow_blessed(1)->convert_blessed(1);
}

method _build_status_path {
return $self->config->working."/status";
}

method _build__status_file {
if ( ! -d $self->status_path ) {
mkpath($self->status_path);
}

return $self->status_path."/".$self->status_file;
}

method _build__data {
my $raw;
if (! -e $self->_status_file) {
$raw = "{}";
} else {
$raw = read_text($self->_status_file);
}

my $data;
try {
$data = $self->_json->decode($raw);
} catch {
$self->logdie("JSON Status failed to parse");
};

return $data;
}

=method get_status
my $netkan_status = $status->get_status( "BaconLabs" );
Will return a reference to the "BaconLabs" status object. Creating
it if necessary or loading it from the available data if it exists.
=cut

method get_status($name) {
if ($self->_data->{$name}) {
$self->set_val($name, App::KSP_CKAN::Status::NetKAN->new($self->_data->{$name}));
} else {
$self->set_val($name, App::KSP_CKAN::Status::NetKAN->new(name => $name));
}
return $self->get_val($name);
}

=method write_json
$status->write_json
Writes our status file out to disk.
=cut

method write_json {
my $json = $self->_json->encode($self->status);
write_text($self->_status_file, $json);
}

1;
Loading

0 comments on commit 6acfc2c

Please sign in to comment.