lib/Test2/Harness/UI/CPANImporter.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 139 CC 8 SCAR 27.9
CC
8
Cov
0%
run
42.0
init
6.0
download
6.0
CRAP 16.2
1 package Test2::Harness::UI::CPANImporter;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use Carp qw/croak/;
8  
9 use HTTP::Tiny;
10 use File::Temp qw/tempdir/;
11 use IO::Uncompress::Gunzip qw/gunzip/;
12  
13 use Test2::Harness::UI::Util::HashBase qw/-config -dir/;
14 use Test2::Harness::UI::UUID qw/uuid_inflate/;
15  
16 sub init {
17 my $self = shift;
18  
19 croak "'config' is a required attribute"
20 unless $self->{+CONFIG};
21  
22 my $dir = tempdir();
23 $self->{+DIR} = $dir;
24 }
25  
26 sub run {
27 my $self = shift;
28  
29 my $batch = int(time);
30  
31 my $schema = $self->{+CONFIG}->schema;
32 my $details = $self->download('02packages.details.txt');
33 my $perms = $self->download('06perms.txt');
34  
35 my $mod_pkg_map = {};
36 open(my $dfh, '<', $details) or die "Could not open $details for reading: $!";
37 my $header = 1;
38 while(my $line = <$dfh>) {
39 chomp($line);
40 next if $header && $line;
41 $header = 0;
42 next unless $line;
43  
44 my ($mod, $ver, $pkg) = split /\s+/, $line;
45 $pkg =~ s{^.*/}{}g;
46 $pkg =~ s{-[^A-Za-z_].*$}{}g;
47  
48 $mod_pkg_map->{$mod} = $pkg;
49 }
50 close($dfh);
51  
52 open(my $pfh, '<', $perms) or die "Could not open $perms for reading: $!";
53 $header = 1;
54 while(my $line = <$pfh>) {
55 chomp($line);
56 next if $header && $line;
57 $header = 0;
58  
59 my ($mod, $id) = split /,/, $line;
60  
61 if (my $email = $schema->resultset('Email')->find({local => $id, domain => 'cpan.org'})) {
62 next unless $email->verified;
63 my $user = $email->user;
64  
65 my $project_name = $mod_pkg_map->{$mod} or next;
66 my $project = $schema->resultset('Project')->find_or_create({name => $project_name});
67  
68 $schema->resultset('Permission')->update_or_create({
69 project_id => uuid_inflate($project->project_id),
70 user_id => uuid_inflate($user->user_id),
71 updated => \'NOW()',
72 cpan_batch => $batch,
73 }) or die "Could not add permissions for $id on $project";
74 }
75 }
76 close($pfh);
77  
78 my $dbh = $self->{+CONFIG}->connect;
79 my $sth = $dbh->prepare('DELETE FROM permissions WHERE cpan_batch IS NOT NULL AND cpan_batch != ?');
80 $sth->execute($batch) or die $sth->errstr;
81 }
82  
83 sub download {
84 my $self = shift;
85 my ($file) = @_;
86  
87 my $path = $self->{+DIR} . '/' . $file;
88  
89 my $resp = HTTP::Tiny->new->get("http://www.cpan.org/modules/${file}.gz");
90 die "Could not download ${file}.gz" unless $resp->{success};
91 open(my $fh, '>', $path) or die "Could not open $path for writing: $!";
92 gunzip(\($resp->{content}) => $fh);
93 close($fh);
94  
95 return $path;
96 }
97  
98 1;
99  
100 __END__