forked from USGCRP/gcis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-report.pl
More file actions
executable file
·247 lines (186 loc) · 5.22 KB
/
import-report.pl
File metadata and controls
executable file
·247 lines (186 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env perl
use Getopt::Long qw/GetOptions/;
use Pod::Usage qw/pod2usage/;
use Gcis::Client;
use Gcis::Exim;
use YAML;
use Data::Dumper;
use strict;
use v5.14;
# local $YAML::Indent = 2;
GetOptions(
'url=s' => \(my $url),
'log_file=s' => \(my $log_file = '/tmp/gcis-import.log'),
'log_level=s' => \(my $log_level = "info"),
'input=s' => \(my $input),
'map_file=s' => \(my $map_file),
'max_import=i' => \(my $max_import = 100),
'local=s' => \(my $local = '.'),
'not_all' => \(my $not_all),
'dry_run|n' => \(my $dry_run),
'help|?' => sub { pod2usage(verbose => 2) },
) or die pod2usage(verbose => 1);
pod2usage(msg => "missing url", verbose => 1) unless $url;
my $n = 0;
&main;
sub main {
say " importing a report";
say " url : $url";
say " log file : $log_file";
say " log level : $log_level";
say " input : $input" if $input;
say " map file : $map_file" if $map_file;
say " max import : $max_import";
say " local : $local";
say " not all" if $not_all;
say " dry run" if $dry_run;
my $s = shift;
my $a = Exim->new();
my $b = $dry_run ? Exim->new($url)
: Exim->new($url, 'update');
$b->not_all if $not_all;
$b->local($local);
my $map;
my $map = $map_file ? Exim->new() : '';
my $logger = Mojo::Log->new($log_file eq '-' ? () : (path => $log_file));
$logger->level($log_level);
$b->logger($logger);
$b->logger_info("starting: ".$url);
$a->load($input);
if ($map) {
$map->load($map_file);
$map->set_up_map($a, $url);
}
$b->{report_uri} = $a->{report_uri};
# main steps:
#
# 1. import/map organizations/people
# 2. import/map journals/publications (except actual report)
# 3. import/map datasets
# 4. import/link report, chapters, figures, images, tables, arrrays,
# findings, references, activities
# 5. import/link files
# 6. link contributors, parents
import_resource($b, $a, $_) for qw(
organizations
people
journals
publications
datasets
);
say " importing report";
$b->import_report($a);
say "";
import_resource($b, $a, $_) for qw(
chapters
images
figures
arrays
tables
findings
references
activities
);
for (qw(
reports
chapters
figures
images
tables
arrays
findings
publications
journals
datasets
)) {
import_files($b, $a, $_);
link_resource($b, $a, $_);
}
$b->logger_info("done");
return;
}
sub import_resource {
my ($b, $a, $types) = @_;
my $type = $b->single_type($types);
say " importing $types\n";
my $sub = \&{"Exim::import_".$type};
for (keys %{ $a->{$types} }) {
$n++;
last if $n > $max_import;
my $obj = $a->{$types}->{$_};
say " obj $n : $obj->{uri}";
$b->$sub($obj);
say "";
}
return;
}
sub import_files {
my ($b, $a, $types) = @_;
my $type = $b->single_type($types);
say " importing files for $types\n";
my $report_uri = $a->{report_uri};
for (keys %{ $a->{$types} }) {
$n++;
last if $n > $max_import;
my $obj = $a->{$types}->{$_};
say " files for $n : $obj->{uri}";
$b->import_files($type, $obj, $a);
say "";
}
return;
}
sub link_resource {
my ($b, $a, $types) = @_;
my $type = $b->single_type($types);
say " linking $types\n";
my $report_uri = $a->{report_uri};
for (keys %{ $a->{$types} }) {
$n++;
last if $n > $max_import;
my $obj = $a->{$types}->{$_};
say " link $n : $obj->{uri}";
$b->link_contributors($type, $obj, $a->{contributors});
$b->link_parents($type, $report_uri, $obj);
say "";
}
return;
}
1;
=head1 NAME
import-report.pl -- import report from source to destination
=head1 DESCRIPTION
import-report.pl imports an entire report with all of the dependent
information. The report source is a yaml file (see export-report.pl).
The destination is a gcis instance.
If a mapping file is provided, the import is done after the mapping
from source to destination is done.
Any new files associated with the report must already be downloaded
in a local directory.
=head1 SYNOPSIS
./import-report.pl [OPTIONS]
=head1 OPTIONS
=over
=item B<--url>
GCIS url, e.g. http://data-stage.globalchange.gov
=item B<--log_file>
Log file (/tmp/gcis-import.log)
=item B<--log_level>
Log level (see Mojo::Log)
=item B<--input>
Input (source) report (yaml file, defaults to STDIN)
=item B<--map_File>
Input mapping file (yaml file, defaults to NULL)
=item B<--max_import>
Maximum number of items to import (defaults to 100)
=item B<--local>
Directory where the report files are located (defaults to ".", see get-files.pl)
=item B<--not_all>
Set to only export first set of items (opposite of "?all=1")
=item B<--dry_run or --n>
Set to perform dry run (no update)
=back
=head1 EXAMPLES
# import a report
./import-report.pl --url=http://datas-dev-front.joss.ucar.edu
--input=report.yaml
=cut