script/import-itol.pl untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 97 CC 5 SCAR 24.8
CC
5
Cov
0%
dump_id
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 12.0
1 #!/usr/bin/env perl
2 # PODNAME: import-itol.pl
3 # ABSTRACT: Upload trees and associate metadata files to iTOL
4 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
5  
6 use Modern::Perl '2011';
7 use autodie;
8  
9 use Getopt::Euclid qw(:vars);
10 use Smart::Comments '###';
11  
12 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
13 use File::Basename;
14 use File::Find::Rule;
15 use HTTP::Request::Common;
16 use LWP::UserAgent;
17  
18  
19 my $upload_url = "https://itol.embl.de/batch_uploader.cgi";
20  
21 # optionally setup tree id outfile
22 my $out;
23 if ($ARGV_tree_id_out) {
24 open $out, '>', $ARGV_tree_id_out;
25 say {$out} '# ' . join "\t", qw ( file id );
26 }
27  
28 FILE:
29 for my $infile (@ARGV_infiles) {
30 ### Processing: $infile
31  
32 # write ZIP archive file
33 my $zip = Archive::Zip->new();
34 my ($basename, $dir, $suffix) = fileparse($infile, qr{\.[^.]*}xms);
35 my $zipfile = "$basename.zip";
36  
37 my $newname = "$basename.tree"; # iTOL wants a .tree suffix
38 symlink($infile, $newname); # TODO: improve this
39 $zip->addFile($newname);
40  
41 $zip->addFile($_) for File::Find::Rule->file()
42 ->maxdepth(1)->name("$basename\-*.txt")->in($dir);
43  
44 ### Storing ZIP file: $zipfile
45 unless ( $zip->writeToFileNamed($zipfile) == AZ_OK ) {
46 warn <<"EOT";
47 Warning: cannot ZIP archive file; skipping!
48 EOT
49 next FILE;
50 }
51  
52 # delete the symlink
53 unlink($newname); # TODO: improve this
54  
55 # prepare the data
56 my %data_for;
57 $data_for{ 'zipFile' } = [ $zipfile ];
58 $data_for{ 'treeName' } = $basename;
59 $data_for{ 'APIkey' } = $ARGV_api_key;
60 $data_for{ 'projectName' } = $ARGV_project;
61 $data_for{ 'treeDescription' } = $ARGV_description if $ARGV_description;
62  
63 # submit the data
64 my $ua = LWP::UserAgent->new();
65 $ua->agent("iTOLbatchUploader4.0");
66 my $request = POST $upload_url,
67 Content_Type => 'form-data', Content => [ %data_for ];
68 my $response = $ua->request($request);
69  
70 dump_id($response, $basename);
71 }
72  
73  
74 # TODO: use standard BMC error message scheme
75 sub dump_id {
76 my $response = shift;
77 my $file = shift;
78  
79 if ( $response->is_success() ) {
80 my @res = split /\n/xms, $response->content;
81  
82 # check for an upload error
83 if ($res[-1] =~ /^ERR/xms) {
84 warn <<"EOT";
85 Warning: upload failed; iTOL returned the following error message:
86 $res[$#res]
87 EOT
88 }
89  
90 # upload without warnings, id on first line
91 if ($res[0] =~ /^SUCCESS: \s (\S+)/xms) {
92 my $tree_id = $1;
93 print <<"EOT";
94 Note: Upload successful; your tree can be accessed through the following id:
95 $tree_id
96 EOT
97 say {$out} join "\t", $file, $tree_id if $out;
98 }
99 }
100  
101 else {
102 warn <<"EOT";
103 Warning: iTOL returned a web server error; full message follows:
104 EOT
105 print $response->as_string;
106 }
107  
108 return;
109 }
110  
111 __END__