Skip to content

Commit 70e51ad

Browse files
committed
External script
1 parent 80cdeec commit 70e51ad

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

bin/external.pl

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use strict;
2+
use warnings;
3+
use v5.36;
4+
5+
use Email::Sender::Simple ();
6+
use Email::Simple ();
7+
use Getopt::Long;
8+
use MetaCPAN::Logger qw< :log :dlog >;
9+
10+
use MetaCPAN::ES;
11+
use MetaCPAN::Ingest qw<
12+
>;
13+
14+
# with(
15+
# 'MetaCPAN::Script::Role::External::Cygwin',
16+
# 'MetaCPAN::Script::Role::External::Debian',
17+
# );
18+
19+
# args
20+
my ( $email_to, $external_source );
21+
GetOptions(
22+
"email_to=s" => \$email_to,
23+
"external_source=s" => \$external_source,
24+
);
25+
26+
die "wrong external source: $external\n"
27+
unless $external_source
28+
and grep { $_ eq $external_source } qw< cygwin debian >;
29+
30+
# setup
31+
my $es = MetaCPAN::ES->new( type => "author" );
32+
33+
my $ret;
34+
35+
$ret = run_cygwin() if $external_source eq 'cygwin';
36+
$ret = run_debian() if $external_source eq 'debian';
37+
38+
my $email_body = $ret->{errors_email_body};
39+
if ( $email_to and $email_body ) {
40+
my $email = Email::Simple->create(
41+
header => [
42+
'Content-Type' => 'text/plain; charset=utf-8',
43+
To => $email_to,
44+
From => '[email protected]',
45+
Subject => "Package mapping failures report for $external_source",
46+
'MIME-Version' => '1.0',
47+
],
48+
body => $email_body,
49+
);
50+
Email::Sender::Simple->send($email);
51+
52+
log_debug { "Sending email to " . $email_to . ":" };
53+
log_debug {"Email body:"};
54+
log_debug {$email_body};
55+
}
56+
57+
my $scroll = $es->scroll(
58+
type => 'distribution',
59+
scroll => '10m',
60+
body => {
61+
query => {
62+
exists => { field => "external_package." . $external_source }
63+
}
64+
},
65+
);
66+
67+
my @to_remove;
68+
69+
while ( my $s = $scroll->next ) {
70+
my $name = $s->{_source}{name};
71+
next unless $name;
72+
73+
if ( exists $dist->{$name} ) {
74+
delete $dist->{$name}
75+
if $dist->{$name} eq
76+
$s->{_source}{external_package}{$external_source};
77+
}
78+
else {
79+
push @to_remove => $name;
80+
}
81+
}
82+
83+
my $bulk = $es->bulk( type => 'distribution' );
84+
85+
for my $d ( keys %{$dist} ) {
86+
log_debug {"[$external_source] adding $d"};
87+
$bulk->update( {
88+
id => $d,
89+
doc => +{
90+
'external_package' => {
91+
$external_source => $dist->{$d}
92+
}
93+
},
94+
doc_as_upsert => 1,
95+
} );
96+
}
97+
98+
for my $d (@to_remove) {
99+
log_debug {"[$external_source] removing $d"};
100+
$bulk->update( {
101+
id => $d,
102+
doc => +{
103+
'external_package' => {
104+
$external_source => undef
105+
}
106+
}
107+
} );
108+
}
109+
110+
$bulk->flush;
111+
112+
1;
113+
114+
=pod
115+
116+
=head1 SYNOPSIS
117+
118+
# bin/external.pl --external_source SOURCE --email_to EMAIL
119+
120+
=cut

0 commit comments

Comments
 (0)