bin/fs_to_db.pl
author Vincent Tondellier <tonton+hg@team1664.org>
Sat, 12 Sep 2015 01:21:06 +0200
changeset 74 e7eda506a66f
parent 70 dfc2f094a04f
permissions -rwxr-xr-x
Require Mojolicious >= 5.81 for Mojo::Loader functional interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
#!/usr/bin/env perl
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
# This program is free software: you can redistribute it and/or modify
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
# it under the terms of the GNU General Public License as published by
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
# the Free Software Foundation, either version 3 of the License, or
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
# (at your option) any later version.
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
#
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
# This program is distributed in the hope that it will be useful,
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
# GNU General Public License for more details.
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
#
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
# You should have received a copy of the GNU General Public License
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    15
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    16
use Mojo::Base -strict;
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    17
use lib 'lib';
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    18
55
8d9daa16ccad Mojo::JSON OO api is deprecated
Vincent Tondellier <tonton+hg@team1664.org>
parents: 43
diff changeset
    19
use Mojo::JSON qw/decode_json/;
8d9daa16ccad Mojo::JSON OO api is deprecated
Vincent Tondellier <tonton+hg@team1664.org>
parents: 43
diff changeset
    20
use Mojo::Util qw/decode slurp/;
70
dfc2f094a04f Fix compatibility with Mojolicious > 6
Vincent Tondellier <tonton+hg@team1664.org>
parents: 60
diff changeset
    21
use Mojo::Loader qw/load_class/;
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
use File::Basename;
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    23
use Mojolicious;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    24
use Mojo::Home;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    25
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    26
if(scalar @ARGV == 0) {
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    27
    say "Usage: $0 <processed json files>";
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    28
    exit 1;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    29
}
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
sub load_config {
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    32
    my $app = Mojolicious->new;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    33
    $app->home(Mojo::Home->new("$FindBin::Bin/../"));
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    34
    $app->moniker("CrashTest");
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    35
    return $app->plugin('Config');
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    36
}
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    37
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    38
my $config = load_config();
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    39
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    40
my $storage_class = $config->{Storage}->{Type};
70
dfc2f094a04f Fix compatibility with Mojolicious > 6
Vincent Tondellier <tonton+hg@team1664.org>
parents: 60
diff changeset
    41
if (my $e = load_class($storage_class)) {
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    42
    die ref $e ? "Exception: $e" : 'Not found!';
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    43
}
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    44
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    45
my $storage = $storage_class->new(config => $config->{Storage});
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    46
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    47
foreach my $arg (@ARGV) {
60
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    48
    eval {
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    49
        my $pjson = decode_json(slurp $arg);
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
60
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    51
        my($filename, $dirs, $suffix) = fileparse($arg, qr/\.[^.]*/);
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
60
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    53
        my $uuid = $filename;
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    54
        $storage->_db_insert_processed_data($uuid, $pjson);
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    55
    };
7ad91b3d5562 Don't abort when a file is not loadable
Vincent Tondellier <tonton+hg@team1664.org>
parents: 55
diff changeset
    56
    warn $@ if $@;
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    57
}