/
Tiny.pm6
129 lines (101 loc) · 3.42 KB
/
Tiny.pm6
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
use v6;
unit class CPAN::Uploader::Tiny:ver<0.0.2>;
use CPAN::Uploader::Tiny::MultiPart;
use HTTP::Tinyish;
has $.url;
has $.user;
has $.password;
has $.agent;
submethod BUILD(:$!url, :$!user, :$!password, :$!agent) {
$!url ||= %*ENV<CPAN_UPLOADER_UPLOAD_URI> || 'https://pause.perl.org/pause/authenquery';
$!agent ||= do {
my $name = self.^name;
my $ver = self.^ver.Str;
"perl6 $name/$ver";
};
}
method new-from-config($file) {
my %config = self!read-config($file);
self.new(user => %config<user>, password => %config<password>);
}
method !read-config($file) {
die "missing $file" unless $file.IO.e;
my $is-plain = ?try {
my $content = $file.IO.slurp(:!bin);
$content !~~ / 'BEGIN PGP MESSAGE' /;
};
if $is-plain {
self!read-plain-config(:$file);
} else {
self!read-encrypted-config(:$file);
}
}
method !read-plain-config(:$file, :@line) {
@line ||= $file.IO.lines;
my %config = gather for @line -> $line {
if $line ~~ /^ $<key>=(\S+) \s+ $<value>=(\S.*)/ {
take $<key>.Str => $<value>.Str;
}
};
%config<user> or die "missing user in $file";
%config<password> or die "missing password in $file";
%config;
}
method !read-encrypted-config(:$file) {
my $proc = run "gpg", "-qd", "--no-tty", $file, :out, :err;
if $proc.exitcode != 0 {
my $err = $proc.err.slurp-rest;
my $exitcode = $proc.exitcode;
die $err ?? $err !! "gpg failed, exitcode = $exitcode";
}
my @line = $proc.out.lines;
self!read-plain-config(:$file, :@line);
}
method upload($tarball, :$subdirectory = "Perl6", :$async) {
my $url = $!url.subst('//', "//{$.user}:{$.password}@");
my $multi = CPAN::Uploader::Tiny::MultiPart.new;
$multi.add-content('HIDDENNAME', $!user);
$multi.add-content('CAN_MULTIPART', "1");
$multi.add-content('pause99_add_uri_uri', '');
$multi.add-content('pause99_add_uri_subdirtext', $subdirectory) if $subdirectory;
$multi.add-content('SUBMIT_pause99_add_uri_httpupload', ' Upload this file from my disk ');
$multi.add-file('pause99_add_uri_httpupload',
filename => $tarball.IO.basename,
content => $tarball.IO.slurp(:bin),
content-type => 'application/gzip',
);
my ($boundary, $content) = $multi.finalize;
my %option =
headers => {
content-type => "multipart/form-data; boundary=$boundary",
},
content => $content,
;
my &cb = sub (%res) {
return True if %res<success>;
die "%res<status> %res<reason>, $!url";
};
if $async {
return HTTP::Tinyish.new(:$!agent, :async).post($url, |%option).then: -> $p { &cb($p.result) };
} else {
return &cb( HTTP::Tinyish.new(:$!agent).post($url, |%option) );
}
}
=begin pod
=head1 NAME
CPAN::Uploader::Tiny - Upload tarballs to CPAN
=head1 SYNOPSIS
use CPAN::Uploader::Tiny;
my $uploader = CPAN::Uploader::Tiny.new-from-config($*HOME.add: '.pause');
$uploader.upload("Your-Perl6-Module-0.0.1.tar.gz");
=head1 DESCRIPTION
CPAN::Uploader::Tiny uploads tarballs to CPAN.
=head1 SEE ALSO
L<https://github.com/rjbs/CPAN-Uploader>
L<https://github.com/Leont/cpan-upload-tiny>
=head1 AUTHOR
Shoichi Kaji <skaji@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2017 Shoichi Kaji
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod