blib/lib/App/cpangitify.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 97.0% 65 / 67 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package App::cpangitify;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie qw( :system );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Getopt::Long qw( GetOptions );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Pod::Usage qw( pod2usage );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Path::Class qw( file dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Git::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON::PP qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Copy::Recursive qw( rcopy );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 0 ▶use Archive::Libarchive::Extract;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use CPAN::ReleaseHistory;
21 use HTTP::Tiny;
22  
23 # ABSTRACT: Convert cpan distribution from BackPAN to a git repository
24 our $VERSION = '0.21'; # VERSION
25  
26  
27 our $ua = HTTP::Tiny->new( verify_SSL => 1 );
28 our $opt_metacpan_url;
29  
30 sub _rm_rf ($file)
31 {
32 if($file->is_dir && ! -l $file)
33 {
34 _rm_rf($_) for $file->children;
35 }
36  
37 $file->remove || die "unable to delete $file";
38 }
39  
40 our $_run_cb = sub {};
41 our $original_run = \&Git::Wrapper::RUN;
42 our $trace = 0;
43  
44 sub _run_wrapper ($self, @command)
45 {
46 my @display;
47 foreach my $arg (@command)
48 {
49 if(ref($arg) eq 'HASH')
50 {
51 foreach my $k (keys %$arg)
52 {
53 my $v = $arg->{$k};
54 push @display, "--$k";
55 push @display, $v =~ /\s/ ? "'$v'" : $v
56 if $v ne '1'; # yes there is a weird exception for this :P
57 }
58 }
59 else
60 {
61 push @display, $arg;
62 }
63 }
64 $_run_cb->($self, @display);
65 say "+ git @display" if $trace;
66 $original_run->($self, @command);
67 }
68  
69 sub author ($cpanid)
70 {
71 state $cache = {};
72  
73 unless(defined $cache->{$cpanid})
74 {
75 my $uri = URI->new($opt_metacpan_url . "v1/author/" . $cpanid);
76 my $res = $ua->get($uri);
77 unless($res->{success})
78 {
79 say "error fetching $uri";
80 say $res->{reason};
81 return 2;
82 }
83 $cache->{$cpanid} = decode_json($res->{content})
84 }
85  
86 my $email = $cache->{$cpanid}->{email};
87 $email = $email->[0] if ref($email) eq 'ARRAY';
88 sprintf "%s <%s>", $cache->{$cpanid}->{name}, $email;
89 }
90  
91 sub main ($, @args)
92 {
93 local @ARGV = @args;
94 no warnings 'redefine';
95 local *Git::Wrapper::RUN = \&_run_wrapper;
96 use warnings;
97  
98 my %skip;
99 my $opt_backpan_index_url;
100 my $opt_backpan_url = "https://cpan.metacpan.org/authors/id";
101 $opt_metacpan_url = "https://fastapi.metacpan.org/";
102 my $opt_trace = 0;
103 my $opt_output;
104 my $opt_resume;
105 my $opt_branch = 'main';
106  
107 GetOptions(
108 'backpan_index_url=s' => \$opt_backpan_index_url,
109 'backpan_url=s' => \$opt_backpan_url,
110 'metacpan_url=s' => \$opt_metacpan_url,
111 'trace' => \$opt_trace,
112 'skip=s' => sub ($version) { $skip{$_} = 1 for split /,/, $version },
113 'resume' => \$opt_resume,
114 'output|o=s' => \$opt_output,
115 'help|h' => sub { pod2usage({ -verbose => 2}) },
116 'branch|b=s' => \$opt_branch,
117 'version' => sub {
118 say 'cpangitify version ', ($App::cpangitify::VERSION // 'dev');
119 exit 1;
120 },
121 ) || pod2usage(1);
122  
123 local $trace = $opt_trace;
124  
125 my @names = @ARGV;
126 s/::/-/g for @names;
127 my %names = map { $_ => 1 } @names;
128 my $name = $names[0];
129  
130 pod2usage(1) unless $name;
131  
132 my $dest = $opt_output ? dir($opt_output)->absolute : dir()->absolute->subdir($name);
133  
134 if(-e $dest && ! $opt_resume)
135 {
136 say "already exists: $dest";
137 say "you may be able to update with the --resume option";
138 say "but any local changes to your repository will be overwritten by upstream";
139 return 2;
140 }
141  
142 say "creating/updating index...";
143 my $history = CPAN::ReleaseHistory->new(
144 maybe url => $opt_backpan_index_url
145 )->release_iterator;
146  
147 say "searching...";
148 my @rel;
149 while(my $release = $history->next_release)
150 {
151 next unless defined $release->distinfo->dist;
152 next unless $names{$release->distinfo->dist};
153 push @rel, $release;
154 }
155  
156 if($@ || @rel == 0)
157 {
158 say "no releases found for $name";
159 return 2;
160 }
161  
162 say "mkdir $dest";
163 $dest->mkpath(0,0700);
164  
165 my $git = Git::Wrapper->new($dest->stringify);
166  
167 if($opt_resume)
168 {
169 if($git->status->is_dirty)
170 {
171 die "the appear to be uncommited changes";
172 }
173 $skip{$_} = 1 for $git->tag;
174 }
175 else
176 {
177 $git->init;
178 $git->checkout( -b => $opt_branch );
179 }
180  
181 foreach my $rel (@rel)
182 {
183 my $path = $rel->path;
184 my $version = $rel->distinfo->version;
185 my $time = $rel->timestamp;
186 my $cpanid = $rel->distinfo->cpanid;
187  
188 say "$path [ $version ]";
189  
190 if($skip{$version})
191 {
192 say "skipping ...";
193 next;
194 }
195  
196 my $tmp = dir( tempdir( CLEANUP => 1 ) );
197  
198 local $CWD = $tmp->stringify;
199  
200 my $uri = URI->new(join('/', $opt_backpan_url, $path));
201 say "fetch ... $uri";
202 my $res = $ua->get($uri);
203 unless($res->{success})
204 {
205 say "error fetching $uri";
206 say $res->{reason};
207 return 2;
208 }
209  
210 say "unpack... @{[ basename $uri->path ]}";
211 my $extract = Archive::Libarchive::Extract->new(
212 memory => \$res->{content},
213 entry => sub ($e) {
214 say "- extract @{[ $e->pathname ]}" if $trace;
215 1;
216 },
217 );
218 $extract->extract( to => $CWD );
219  
220 my $source = do {
221 my @children = map { $_->absolute } dir()->children;
222 if(@children != 1)
223 {
224 say "archive doesn't contain exactly one child: @children";
225 }
226  
227 $CWD = $children[0]->stringify;
228 $children[0];
229 };
230  
231 say "merge...";
232  
233 foreach my $child ($dest->children)
234 {
235 next if $child->basename eq '.git';
236 _rm_rf($child);
237 }
238  
239 foreach my $child ($source->children)
240 {
241 next if $child->basename eq '.git';
242 if(-d $child)
243 {
244 rcopy($child, $dest->subdir($child->basename)) || die "unable to copy $child $!";
245 }
246 else
247 {
248 rcopy($child, $dest->file($child->basename)) || die "unable to copy $child $!";
249 }
250 }
251  
252 say "commit and tag...";
253 $git->add('.');
254 $git->add('-u');
255 $git->commit({
256 message => "version $version",
257 date => "$time +0000",
258 author => author($cpanid),
259 'allow-empty' => 1,
260 });
261 eval { $git->tag($version) };
262 warn $@ if $@;
263 }
264  
265 return 0;
266 }
267  
268 1;
269  
270 __END__
↑ Summary