scancpan 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. #!/usr/bin/env perl
  2. # This chunk of stuff was generated by App::FatPacker. To find the original
  3. # file's code, look for the end of this BEGIN block or the string 'FATPACK'
  4. BEGIN {
  5. my %fatpacked;
  6. $fatpacked{"MetaCPAN/API/Tiny.pm"} = <<'METACPAN_API_TINY';
  7. package MetaCPAN::API::Tiny;
  8. {
  9. $MetaCPAN::API::Tiny::VERSION = '1.131730';
  10. }
  11. use strict;
  12. use warnings;
  13. # ABSTRACT: A Tiny API client for MetaCPAN
  14. use Carp;
  15. use JSON::PP 'encode_json', 'decode_json';
  16. use HTTP::Tiny;
  17. sub new {
  18. my ($class, @args) = @_;
  19. $#_ % 2 == 0
  20. or croak 'Arguments must be provided as name/value pairs';
  21. my %params = @args;
  22. die 'ua_args must be an array reference'
  23. if $params{ua_args} && ref($params{ua_args}) ne 'ARRAY';
  24. my $self = +{
  25. base_url => $params{base_url} || 'http://api.metacpan.org/v0',
  26. ua => $params{ua} || HTTP::Tiny->new(
  27. $params{ua_args}
  28. ? @{$params{ua_args}}
  29. : (agent => 'MetaCPAN::API::Tiny/'
  30. . ($MetaCPAN::API::VERSION || 'xx'))),
  31. };
  32. return bless($self, $class);
  33. }
  34. sub _build_extra_params {
  35. my $self = shift;
  36. @_ % 2 == 0
  37. or croak 'Incorrect number of params, must be key/value';
  38. my %extra = @_;
  39. my $ua = $self->{ua};
  40. foreach my $key (keys %extra)
  41. {
  42. # The implementation in HTTP::Tiny uses + instead of %20, fix that
  43. $extra{$key} = $ua->_uri_escape($extra{$key});
  44. $extra{$key} =~ s/\+/%20/g;
  45. }
  46. my $params = join '&', map { "$_=" . $extra{$_} } sort keys %extra;
  47. return $params;
  48. }
  49. # /source/{author}/{release}/{path}
  50. sub source {
  51. my $self = shift;
  52. my %opts = @_ ? @_ : ();
  53. my $url = '';
  54. my $error = "Provide 'author' and 'release' and 'path'";
  55. %opts or croak $error;
  56. if (
  57. defined ( my $author = $opts{'author'} ) &&
  58. defined ( my $release = $opts{'release'} ) &&
  59. defined ( my $path = $opts{'path'} )
  60. ) {
  61. $url = "source/$author/$release/$path";
  62. } else {
  63. croak $error;
  64. }
  65. $url = $self->{base_url} . "/$url";
  66. my $result = $self->{ua}->get($url);
  67. $result->{'success'}
  68. or croak "Failed to fetch '$url': " . $result->{'reason'};
  69. return $result->{'content'};
  70. }
  71. # /release/{distribution}
  72. # /release/{author}/{release}
  73. sub release {
  74. my $self = shift;
  75. my %opts = @_ ? @_ : ();
  76. my $url = '';
  77. my $error = "Either provide 'distribution', or 'author' and 'release', " .
  78. "or 'search'";
  79. %opts or croak $error;
  80. my %extra_opts = ();
  81. if ( defined ( my $dist = $opts{'distribution'} ) ) {
  82. $url = "release/$dist";
  83. } elsif (
  84. defined ( my $author = $opts{'author'} ) &&
  85. defined ( my $release = $opts{'release'} )
  86. ) {
  87. $url = "release/$author/$release";
  88. } elsif ( defined ( my $search_opts = $opts{'search'} ) ) {
  89. ref $search_opts && ref $search_opts eq 'HASH'
  90. or croak $error;
  91. %extra_opts = %{$search_opts};
  92. $url = 'release/_search';
  93. } else {
  94. croak $error;
  95. }
  96. return $self->fetch( $url, %extra_opts );
  97. }
  98. # /pod/{module}
  99. # /pod/{author}/{release}/{path}
  100. sub pod {
  101. my $self = shift;
  102. my %opts = @_ ? @_ : ();
  103. my $url = '';
  104. my $error = "Either provide 'module' or 'author and 'release' and 'path'";
  105. %opts or croak $error;
  106. if ( defined ( my $module = $opts{'module'} ) ) {
  107. $url = "pod/$module";
  108. } elsif (
  109. defined ( my $author = $opts{'author'} ) &&
  110. defined ( my $release = $opts{'release'} ) &&
  111. defined ( my $path = $opts{'path'} )
  112. ) {
  113. $url = "pod/$author/$release/$path";
  114. } else {
  115. croak $error;
  116. }
  117. # check content-type
  118. my %extra = ();
  119. if ( defined ( my $type = $opts{'content-type'} ) ) {
  120. $type =~ m{^ text/ (?: html|plain|x-pod|x-markdown ) $}x
  121. or croak 'Incorrect content-type provided';
  122. $extra{headers}{'content-type'} = $type;
  123. }
  124. $url = $self->{base_url}. "/$url";
  125. my $result = $self->{ua}->get( $url, \%extra );
  126. $result->{'success'}
  127. or croak "Failed to fetch '$url': " . $result->{'reason'};
  128. return $result->{'content'};
  129. }
  130. # /module/{module}
  131. sub module {
  132. my $self = shift;
  133. my $name = shift;
  134. $name or croak 'Please provide a module name';
  135. return $self->fetch("module/$name");
  136. }
  137. # file() is a synonym of module
  138. sub file { goto &module }
  139. # /author/{author}
  140. sub author {
  141. my $self = shift;
  142. my ( $pause_id, $url, %extra_opts );
  143. if ( @_ == 1 ) {
  144. $url = 'author/' . shift;
  145. } elsif ( @_ == 2 ) {
  146. my %opts = @_;
  147. if ( defined $opts{'pauseid'} ) {
  148. $url = "author/" . $opts{'pauseid'};
  149. } elsif ( defined $opts{'search'} ) {
  150. my $search_opts = $opts{'search'};
  151. ref $search_opts && ref $search_opts eq 'HASH'
  152. or croak "'search' key must be hashref";
  153. %extra_opts = %{$search_opts};
  154. $url = 'author/_search';
  155. } else {
  156. croak 'Unknown option given';
  157. }
  158. } else {
  159. croak 'Please provide an author PAUSEID or a "search"';
  160. }
  161. return $self->fetch( $url, %extra_opts );
  162. }
  163. sub fetch {
  164. my $self = shift;
  165. my $url = shift;
  166. my $extra = $self->_build_extra_params(@_);
  167. my $base = $self->{base_url};
  168. my $req_url = $extra ? "$base/$url?$extra" : "$base/$url";
  169. my $result = $self->{ua}->get($req_url);
  170. return $self->_decode_result( $result, $req_url );
  171. }
  172. sub post {
  173. my $self = shift;
  174. my $url = shift;
  175. my $query = shift;
  176. my $base = $self->{base_url};
  177. defined $url
  178. or croak 'First argument of URL must be provided';
  179. ref $query and ref $query eq 'HASH'
  180. or croak 'Second argument of query hashref must be provided';
  181. my $query_json = encode_json( $query );
  182. my $result = $self->{ua}->request(
  183. 'POST',
  184. "$base/$url",
  185. {
  186. headers => { 'Content-Type' => 'application/json' },
  187. content => $query_json,
  188. }
  189. );
  190. return $self->_decode_result( $result, $url, $query_json );
  191. }
  192. sub _decode_result {
  193. my $self = shift;
  194. my ( $result, $url, $original ) = @_;
  195. my $decoded_result;
  196. ref $result and ref $result eq 'HASH'
  197. or croak 'First argument must be hashref';
  198. defined $url
  199. or croak 'Second argument of a URL must be provided';
  200. if ( defined ( my $success = $result->{'success'} ) ) {
  201. my $reason = $result->{'reason'} || '';
  202. $reason .= ( defined $original ? " (request: $original)" : '' );
  203. $success or croak "Failed to fetch '$url': $reason";
  204. } else {
  205. croak 'Missing success in return value';
  206. }
  207. defined ( my $content = $result->{'content'} )
  208. or croak 'Missing content in return value';
  209. eval { $decoded_result = decode_json $content; 1 }
  210. or do { croak "Couldn't decode '$content': $@" };
  211. return $decoded_result;
  212. }
  213. 1;
  214. __END__
  215. =pod
  216. =head1 NAME
  217. MetaCPAN::API::Tiny - A Tiny API client for MetaCPAN
  218. =head1 VERSION
  219. version 1.131730
  220. =head1 DESCRIPTION
  221. This is the Tiny version of L<MetaCPAN::API>. It implements a compatible API
  222. with a few notable exceptions:
  223. =over 4
  224. =item Attributes are direct hash access
  225. The attributes defined using Mo(o|u)se are now accessed via the blessed hash
  226. directly. There are no accessors defined to access this elements.
  227. =item Exception handling
  228. Instead of using Try::Tiny, raw evals are used. This could potentially cause
  229. issues, so just be aware.
  230. =item Testing
  231. Test::Fatal was replaced with an eval implementation of exception().
  232. Test::TinyMocker usage is retained, but may be absorbed since it is pure perl
  233. =back
  234. =head1 CLASS_METHODS
  235. =head2 new
  236. new is the constructor for MetaCPAN::API::Tiny. In the non-tiny version of this
  237. module, this is provided via Any::Moose built from the attributes defined. In
  238. the tiny version, we define our own constructor. It takes the same arguments
  239. and provides similar checks to MetaCPAN::API with regards to arguments passed.
  240. =head1 PUBLIC_METHODS
  241. =head2 source
  242. my $source = $mcpan->source(
  243. author => 'DOY',
  244. release => 'Moose-2.0201',
  245. path => 'lib/Moose.pm',
  246. );
  247. Searches MetaCPAN for a module or a specific release and returns the plain source.
  248. =head2 release
  249. my $result = $mcpan->release( distribution => 'Moose' );
  250. # or
  251. my $result = $mcpan->release( author => 'DOY', release => 'Moose-2.0001' );
  252. Searches MetaCPAN for a dist.
  253. You can do complex searches using 'search' parameter:
  254. # example lifted from MetaCPAN docs
  255. my $result = $mcpan->release(
  256. search => {
  257. author => "OALDERS AND ",
  258. filter => "status:latest",
  259. fields => "name",
  260. size => 1,
  261. },
  262. );
  263. =head2 pod
  264. my $result = $mcpan->pod( module => 'Moose' );
  265. # or
  266. my $result = $mcpan->pod(
  267. author => 'DOY',
  268. release => 'Moose-2.0201',
  269. path => 'lib/Moose.pm',
  270. );
  271. Searches MetaCPAN for a module or a specific release and returns the POD.
  272. =head2 module
  273. my $result = $mcpan->module('MetaCPAN::API');
  274. Searches MetaCPAN and returns a module's ".pm" file.
  275. =head2 file
  276. A synonym of L</module>
  277. =head2 author
  278. my $result1 = $mcpan->author('XSAWYERX');
  279. my $result2 = $mcpan->author( pauseid => 'XSAWYERX' );
  280. Searches MetaCPAN for a specific author.
  281. You can do complex searches using 'search' parameter:
  282. # example lifted from MetaCPAN docs
  283. my $result = $mcpan->author(
  284. search => {
  285. q => 'profile.name:twitter',
  286. size => 1,
  287. },
  288. );
  289. =head2 fetch
  290. my $result = $mcpan->fetch('/release/distribution/Moose');
  291. # with parameters
  292. my $more = $mcpan->fetch(
  293. '/release/distribution/Moose',
  294. param => 'value',
  295. );
  296. This is a helper method for API implementations. It fetches a path from MetaCPAN, decodes the JSON from the content variable and returns it.
  297. You don't really need to use it, but you can in case you want to write your own extension implementation to MetaCPAN::API.
  298. It accepts an additional hash as "GET" parameters.
  299. =head2 post
  300. # /release&content={"query":{"match_all":{}},"filter":{"prefix":{"archive":"Cache-Cache-1.06"}}}
  301. my $result = $mcpan->post(
  302. 'release',
  303. {
  304. query => { match_all => {} },
  305. filter => { prefix => { archive => 'Cache-Cache-1.06' } },
  306. },
  307. );
  308. The POST equivalent of the "fetch()" method. It gets the path and JSON request.
  309. =head1 THANKS
  310. Overall the tests and code were ripped directly from MetaCPAN::API and
  311. tiny-fied. A big thanks to Sawyer X for writing the original module.
  312. =head1 AUTHOR
  313. Nicholas R. Perez <nperez@cpan.org>
  314. =head1 COPYRIGHT AND LICENSE
  315. This software is copyright (c) 2013 by Nicholas R. Perez <nperez@cpan.org>.
  316. This is free software; you can redistribute it and/or modify it under
  317. the same terms as the Perl 5 programming language system itself.
  318. =cut
  319. METACPAN_API_TINY
  320. s/^ //mg for values %fatpacked;
  321. unshift @INC, sub {
  322. if (my $fat = $fatpacked{$_[1]}) {
  323. if ($] < 5.008) {
  324. return sub {
  325. return 0 unless length $fat;
  326. $fat =~ s/^([^\n]*\n?)//;
  327. $_ = $1;
  328. return 1;
  329. };
  330. }
  331. open my $fh, '<', \$fat
  332. or die "FatPacker error loading $_[1] (could be a perl installation issue?)";
  333. return $fh;
  334. }
  335. return
  336. };
  337. } # END OF FATPACK CODE
  338. use 5.010;
  339. use strict;
  340. use warnings;
  341. use Fatal qw(open close);
  342. use Getopt::Long;
  343. use Pod::Usage;
  344. use File::Basename;
  345. use File::Path qw(make_path);
  346. use Module::CoreList;
  347. use HTTP::Tiny;
  348. use Safe;
  349. use MetaCPAN::API::Tiny;
  350. use Digest::SHA qw(sha256_hex);
  351. use Text::Wrap;
  352. $Text::Wrap::columns = 62;
  353. # Below, 5.030 should be aligned with the version of perl actually
  354. # bundled in Buildroot:
  355. die <<"MSG" if $] < 5.030;
  356. This script needs a host perl with the same major version as Buildroot target perl.
  357. Your current host perl is:
  358. $^X
  359. version $]
  360. You may install a local one by running:
  361. perlbrew install perl-5.30.0
  362. MSG
  363. my ($help, $man, $quiet, $force, $recommend, $test, $host);
  364. my $target = 1;
  365. GetOptions( 'help|?' => \$help,
  366. 'man' => \$man,
  367. 'quiet|q' => \$quiet,
  368. 'force|f' => \$force,
  369. 'host!' => \$host,
  370. 'target!' => \$target,
  371. 'recommend' => \$recommend,
  372. 'test' => \$test
  373. ) or pod2usage(-exitval => 1);
  374. pod2usage(-exitval => 0) if $help;
  375. pod2usage(-exitval => 0, -verbose => 2) if $man;
  376. pod2usage(-exitval => 1) if scalar @ARGV == 0;
  377. my %dist; # name -> metacpan data
  378. my %need_target; # name -> 1 if target package is needed
  379. my %need_host; # name -> 1 if host package is needed
  380. my %need_dlopen; # name -> 1 if requires dynamic library
  381. my %is_xs; # name -> 1 if XS module
  382. my %deps_build; # name -> list of host dependencies
  383. my %deps_runtime; # name -> list of target dependencies
  384. my %license_files; # name -> hash of license files
  385. my %checksum; # author -> list of checksum
  386. my $mirror = 'http://cpan.metacpan.org'; # a CPAN mirror
  387. my $mcpan = MetaCPAN::API::Tiny->new(base_url => 'http://fastapi.metacpan.org/v1');
  388. my $ua = HTTP::Tiny->new();
  389. my $new_pkgs;
  390. my %white_list = (
  391. 'ExtUtils-Config' => 1,
  392. 'ExtUtils-InstallPaths' => 1,
  393. 'ExtUtils-Helpers' => 1,
  394. 'File-ShareDir-Install' => 1,
  395. 'Module-Build' => 1,
  396. 'Module-Build-Tiny' => 1,
  397. );
  398. my @info = ();
  399. sub get_checksum {
  400. my ($url) = @_;
  401. my ($path) = $url =~ m|^[^:/?#]+://[^/?#]*([^?#]*)|;
  402. my ($basename, $dirname) = fileparse( $path );
  403. unless ($checksum{$dirname}) {
  404. my $url = $mirror . $dirname . q{CHECKSUMS};
  405. my $response = $ua->get($url);
  406. $checksum{$dirname} = $response->{content};
  407. }
  408. my $chksum = Safe->new->reval($checksum{$dirname});
  409. return $chksum->{$basename}, $basename;
  410. }
  411. sub is_xs {
  412. my ($manifest) = @_;
  413. # This heuristic determines if a module is a native extension, by searching
  414. # some file extension types in the MANIFEST of the distribution.
  415. # It was inspired by http://deps.cpantesters.org/static/purity.html
  416. return $manifest =~ m/\.(swg|xs|c|h|i)[\n\s]/;
  417. }
  418. sub find_license_files {
  419. my ($manifest) = @_;
  420. my @license_files;
  421. foreach (split /\n/, $manifest) {
  422. next if m|/|;
  423. s|\s+.*$||;
  424. push @license_files, $_ if m/(ARTISTIC|COPYING|COPYRIGHT|LICENSE|LICENCE)/i;
  425. }
  426. if (scalar @license_files == 0 && $manifest =~ m/(README)[\n\s]/i) {
  427. @license_files = ($1);
  428. }
  429. if (scalar @license_files == 0 && $manifest =~ m/(README\.md)[\n\s]/i) {
  430. @license_files = ($1);
  431. }
  432. return @license_files;
  433. }
  434. sub want_test {
  435. my ($distname) = @_;
  436. return 1 if $need_dlopen{$distname} && scalar @{$deps_runtime{$distname}} > 0;
  437. }
  438. sub get_dependencies {
  439. my ($distname) = @_;
  440. my %dep = map { $_ => 1 } @{$deps_runtime{$distname}};
  441. for my $direct (@{$deps_runtime{$distname}}) {
  442. for (get_dependencies( $direct )) {
  443. $dep{$_} = 1;
  444. }
  445. }
  446. return keys %dep;
  447. }
  448. sub get_indirect_dependencies {
  449. my ($distname) = @_;
  450. my %indirect;
  451. my %direct = map { $_ => 1 } @{$deps_runtime{$distname}};
  452. for my $dep (get_dependencies( $distname )) {
  453. $indirect{$dep} = 1 unless exists $direct{$dep};
  454. }
  455. return keys %indirect;
  456. }
  457. sub fetch {
  458. my ($name, $need_target, $need_host, $top) = @_;
  459. $need_target{$name} = $need_target if $need_target;
  460. $need_host{$name} = $need_host if $need_host;
  461. unless ($dist{$name} && !$top) {
  462. say qq{fetch ${name}} unless $quiet;
  463. my $result = $mcpan->release( distribution => $name );
  464. my $main_module = $result->{main_module};
  465. push @info, qq{[$name] $main_module is a core module}
  466. if $top && Module::CoreList::is_core( $main_module, undef, $] );
  467. $dist{$name} = $result;
  468. $license_files{$name} = {};
  469. eval {
  470. my $author = $result->{author};
  471. my $release = $name . q{-} . $result->{version};
  472. my $manifest = $mcpan->source( author => $author, release => $release, path => 'MANIFEST' );
  473. $need_dlopen{$name} = $is_xs{$name} = is_xs( $manifest );
  474. foreach my $fname (find_license_files( $manifest )) {
  475. my $license = $mcpan->source( author => $author, release => $release, path => $fname );
  476. $license_files{$name}->{$fname} = sha256_hex( $license );
  477. }
  478. };
  479. if ($@) {
  480. warn $@;
  481. }
  482. my %build = ();
  483. my %runtime = ();
  484. my %optional = ();
  485. foreach my $dep (@{$result->{dependency}}) {
  486. my $modname = ${$dep}{module};
  487. next if $modname eq q{perl};
  488. next if $modname =~ m|^Alien|;
  489. next if $modname =~ m|^Win32|;
  490. next if !($test && $top) && $modname =~ m|^Test|;
  491. next if Module::CoreList::is_core( $modname, undef, $] );
  492. # we could use the host Module::CoreList data, because host perl and
  493. # target perl have the same major version
  494. next if ${$dep}{phase} eq q{develop};
  495. next if ${$dep}{phase} eq q{x_Dist_Zilla};
  496. next if !($test && $top) && ${$dep}{phase} eq q{test};
  497. my $distname = $mcpan->module( $modname )->{distribution};
  498. if (${$dep}{phase} eq q{runtime}) {
  499. if (${$dep}{relationship} eq q{requires}) {
  500. $runtime{$distname} = 1;
  501. }
  502. else {
  503. $optional{$distname} = 1 if $recommend && $top;
  504. }
  505. }
  506. else { # configure, build
  507. $build{$distname} = 1;
  508. push @info, qq{[$name] suspicious dependency on $distname}
  509. unless exists $white_list{$distname};
  510. }
  511. }
  512. $deps_build{$name} = [keys %build];
  513. $deps_runtime{$name} = [keys %runtime];
  514. foreach my $distname (@{$deps_build{$name}}) {
  515. fetch( $distname, 0, 1 );
  516. }
  517. foreach my $distname (@{$deps_runtime{$name}}) {
  518. fetch( $distname, $need_target, $need_host );
  519. $need_dlopen{$name} ||= $need_dlopen{$distname};
  520. }
  521. foreach my $distname (keys %optional) {
  522. fetch( $distname, $need_target, $need_host );
  523. }
  524. }
  525. return;
  526. }
  527. foreach my $distname (@ARGV) {
  528. # Command-line's distributions
  529. fetch( $distname, !!$target, !!$host, 1 );
  530. }
  531. say scalar keys %dist, q{ packages fetched.} unless $quiet;
  532. # Buildroot package name: lowercase
  533. sub fsname {
  534. my $name = shift;
  535. $name =~ s|_|-|g;
  536. return q{perl-} . lc $name;
  537. }
  538. # Buildroot variable name: uppercase
  539. sub brname {
  540. my $name = shift;
  541. $name =~ s|-|_|g;
  542. return uc $name;
  543. }
  544. # Buildroot requires license name as in http://spdx.org/licenses/
  545. sub brlicense {
  546. my $license = shift;
  547. $license =~ s|apache_1_1|Apache-1.1|;
  548. $license =~ s|apache_2_0|Apache-2.0|;
  549. $license =~ s|artistic_2|Artistic-2.0|;
  550. $license =~ s|artistic|Artistic-1.0|;
  551. $license =~ s|lgpl_2_1|LGPL-2.1|;
  552. $license =~ s|lgpl_3_0|LGPL-3.0|;
  553. $license =~ s|gpl_2|GPL-2.0|;
  554. $license =~ s|gpl_3|GPL-3.0|;
  555. $license =~ s|mit|MIT|;
  556. $license =~ s|mozilla_1_1|Mozilla-1.1|;
  557. $license =~ s|openssl|OpenSSL|;
  558. $license =~ s|perl_5|Artistic or GPL-1.0+|;
  559. return $license;
  560. }
  561. while (my ($distname, $dist) = each %dist) {
  562. my $fsname = fsname( $distname );
  563. my $dirname = q{package/} . $fsname;
  564. my $cfgname = $dirname . q{/Config.in};
  565. my $mkname = $dirname . q{/} . $fsname . q{.mk};
  566. my $hashname = $dirname . q{/} . $fsname . q{.hash};
  567. my $brname = brname( $fsname );
  568. my $testdir = q{support/testing/tests/package};
  569. my $testname = $testdir . q{/test_} . lc $brname . q{.py};
  570. unless (-d $dirname) {
  571. make_path $dirname;
  572. $new_pkgs = 1;
  573. }
  574. if ($need_target{$distname} && ($force || !-f $cfgname)) {
  575. $dist->{abstract} =~ s|\s+$||;
  576. $dist->{abstract} .= q{.} unless $dist->{abstract} =~ m|\.$|;
  577. my $abstract = wrap( q{}, qq{\t }, $dist->{abstract} );
  578. my $homepage = $dist->{resources}->{homepage} || qq{https://metacpan.org/release/${distname}};
  579. say qq{write ${cfgname}} unless $quiet;
  580. open my $fh, q{>}, $cfgname;
  581. say {$fh} qq{config BR2_PACKAGE_${brname}};
  582. say {$fh} qq{\tbool "${fsname}"};
  583. say {$fh} qq{\tdepends on !BR2_STATIC_LIBS} if $need_dlopen{$distname};
  584. foreach my $dep (sort @{$deps_runtime{$distname}}) {
  585. my $brdep = brname( fsname( $dep ) );
  586. say {$fh} qq{\tselect BR2_PACKAGE_${brdep} # runtime};
  587. }
  588. say {$fh} qq{\thelp};
  589. say {$fh} qq{\t ${abstract}\n} if $abstract;
  590. say {$fh} qq{\t ${homepage}};
  591. if ($need_dlopen{$distname}) {
  592. say {$fh} qq{\ncomment "${fsname} needs a toolchain w/ dynamic library"};
  593. say {$fh} qq{\tdepends on BR2_STATIC_LIBS};
  594. }
  595. close $fh;
  596. }
  597. if ($force || !-f $mkname) {
  598. my $version = $dist->{version};
  599. my ($path) = $dist->{download_url} =~ m|^[^:/?#]+://[^/?#]*([^?#]*)|;
  600. # this URL contains only the scheme, auth and path parts (but no query and fragment parts)
  601. # the scheme is not used, because the job is done by the BR download infrastructure
  602. # the auth part is not used, because we use $(BR2_CPAN_MIRROR)
  603. my ($filename, $directories, $suffix) = fileparse( $path, q{tar.gz}, q{tgz} );
  604. $directories =~ s|/$||;
  605. my @dependencies = map( { q{host-} . fsname( $_ ); } sort @{$deps_build{$distname}} );
  606. my $dependencies = join qq{ \\\n\t}, @dependencies;
  607. $dependencies = qq{\\\n\t} . $dependencies if scalar @dependencies > 1;
  608. my @host_dependencies = map { q{host-} . fsname( $_ ); } sort( @{$deps_build{$distname}},
  609. @{$deps_runtime{$distname}} );
  610. my $host_dependencies = join qq{ \\\n\t}, @host_dependencies;
  611. $host_dependencies = qq{\\\n\t} . $host_dependencies if scalar @host_dependencies > 1;
  612. my $license = brlicense( ref $dist->{license} eq 'ARRAY'
  613. ? join q{ or }, @{$dist->{license}}
  614. : $dist->{license} );
  615. my $license_files = join q{ }, sort keys %{$license_files{$distname}};
  616. if ($license_files && (!$license || $license eq q{unknown})) {
  617. push @info, qq{[$distname] undefined LICENSE, see $license_files};
  618. $license = q{???};
  619. }
  620. say qq{write ${mkname}} unless $quiet;
  621. open my $fh, q{>}, $mkname;
  622. say {$fh} qq{################################################################################};
  623. say {$fh} qq{#};
  624. say {$fh} qq{# ${fsname}};
  625. say {$fh} qq{#};
  626. say {$fh} qq{################################################################################};
  627. say {$fh} qq{};
  628. say {$fh} qq{${brname}_VERSION = ${version}};
  629. say {$fh} qq{${brname}_SOURCE = ${distname}-\$(${brname}_VERSION).${suffix}};
  630. say {$fh} qq{${brname}_SITE = \$(BR2_CPAN_MIRROR)${directories}};
  631. say {$fh} qq{${brname}_DEPENDENCIES = ${dependencies}} if $need_target{$distname} && $dependencies;
  632. say {$fh} qq{HOST_${brname}_DEPENDENCIES = ${host_dependencies}} if $need_host{$distname} && $host_dependencies;
  633. say {$fh} qq{${brname}_LICENSE = ${license}} if $license;
  634. say {$fh} qq{${brname}_LICENSE_FILES = ${license_files}} if $license_files;
  635. say {$fh} qq{${brname}_DISTNAME = ${distname}};
  636. say {$fh} qq{};
  637. say {$fh} qq{\$(eval \$(perl-package))} if $need_target{$distname};
  638. say {$fh} qq{\$(eval \$(host-perl-package))} if $need_host{$distname};
  639. close $fh;
  640. }
  641. if ($force || !-f $hashname) {
  642. my ($checksum, $filename) = get_checksum($dist->{download_url});
  643. my $md5 = $checksum->{md5};
  644. my $sha256 = $checksum->{sha256};
  645. say qq{write ${hashname}} unless $quiet;
  646. open my $fh, q{>}, $hashname;
  647. say {$fh} qq{# retrieved by scancpan from ${mirror}/};
  648. say {$fh} qq{md5 ${md5} ${filename}};
  649. say {$fh} qq{sha256 ${sha256} ${filename}};
  650. my %license_files = %{$license_files{$distname}};
  651. if (scalar keys %license_files) {
  652. say {$fh} q{};
  653. say {$fh} qq{# computed by scancpan};
  654. foreach my $license (sort keys %license_files) {
  655. my $digest = $license_files{$license};
  656. say {$fh} qq{sha256 ${digest} ${license}};
  657. }
  658. }
  659. close $fh;
  660. }
  661. if (want_test( $distname ) && ($force || !-f $testname)) {
  662. my $classname = $distname;
  663. $classname =~ s|-||g;
  664. my $modname = $distname;
  665. $modname =~ s|-|::|g;
  666. my $mark = $is_xs{$distname} ? q{ XS} : q{};
  667. my @indirect = (get_indirect_dependencies( $distname ));
  668. say qq{write ${testname}} unless $quiet;
  669. make_path $testdir unless -d $testdir;
  670. open my $fh, q{>}, $testname;
  671. say {$fh} qq{from tests.package.test_perl import TestPerlBase};
  672. say {$fh} qq{};
  673. say {$fh} qq{};
  674. say {$fh} qq{class TestPerl${classname}(TestPerlBase):};
  675. say {$fh} qq{ """};
  676. say {$fh} qq{ package:};
  677. say {$fh} qq{ ${distname}${mark}};
  678. say {$fh} qq{ direct dependencies:};
  679. foreach my $dep (sort @{$deps_runtime{$distname}}) {
  680. $mark = $is_xs{$dep} ? q{ XS} : q{};
  681. say {$fh} qq{ ${dep}${mark}};
  682. }
  683. if (scalar @indirect > 0) {
  684. say {$fh} qq{ indirect dependencies:};
  685. foreach my $dep (sort @indirect) {
  686. $mark = $is_xs{$dep} ? q{ XS} : q{};
  687. say {$fh} qq{ ${dep}${mark}};
  688. }
  689. }
  690. say {$fh} qq{ """};
  691. say {$fh} qq{};
  692. say {$fh} qq{ config = TestPerlBase.config + \\};
  693. say {$fh} qq{ """};
  694. say {$fh} qq{ BR2_PACKAGE_PERL=y};
  695. say {$fh} qq{ BR2_PACKAGE_${brname}=y};
  696. say {$fh} qq{ """};
  697. say {$fh} qq{};
  698. say {$fh} qq{ def test_run(self):};
  699. say {$fh} qq{ self.login()};
  700. foreach my $dep (sort grep { $is_xs{$_} } @indirect) {
  701. $dep =~ s|-|::|g;
  702. say {$fh} qq{ self.module_test("${dep}")};
  703. }
  704. foreach my $dep (sort grep { $is_xs{$_} } @{$deps_runtime{$distname}}) {
  705. $dep =~ s|-|::|g;
  706. say {$fh} qq{ self.module_test("${dep}")};
  707. }
  708. say {$fh} qq{ self.module_test("${modname}")};
  709. close $fh;
  710. }
  711. }
  712. if ($new_pkgs) {
  713. my %pkg;
  714. my $cfgname = q{package/Config.in};
  715. if (-f $cfgname) {
  716. open my $fh, q{<}, $cfgname;
  717. while (<$fh>) {
  718. chomp;
  719. $pkg{$_} = 1 if m|package/perl-|;
  720. }
  721. close $fh;
  722. }
  723. foreach my $distname (keys %need_target) {
  724. my $fsname = fsname( $distname );
  725. $pkg{qq{\tsource "package/${fsname}/Config.in"}} = 1;
  726. }
  727. say qq{${cfgname} must contain the following lines:};
  728. say join qq{\n}, sort keys %pkg;
  729. }
  730. say join qq{\n}, @info;
  731. __END__
  732. =head1 NAME
  733. utils/scancpan Try-Tiny Moo
  734. =head1 SYNOPSIS
  735. utils/scancpan [options] [distname ...]
  736. Options:
  737. -help
  738. -man
  739. -quiet
  740. -force
  741. -target/-notarget
  742. -host/-nohost
  743. -recommend
  744. -test
  745. =head1 OPTIONS
  746. =over 8
  747. =item B<-help>
  748. Prints a brief help message and exits.
  749. =item B<-man>
  750. Prints the manual page and exits.
  751. =item B<-quiet>
  752. Executes without output
  753. =item B<-force>
  754. Forces the overwriting of existing files.
  755. =item B<-target/-notarget>
  756. Switches package generation for the target variant (the default is C<-target>).
  757. =item B<-host/-nohost>
  758. Switches package generation for the host variant (the default is C<-nohost>).
  759. =item B<-recommend>
  760. Adds I<recommended> dependencies.
  761. =item B<-test>
  762. Adds dependencies for test.
  763. =back
  764. =head1 DESCRIPTION
  765. This script creates templates of the Buildroot package files for all the
  766. Perl/CPAN distributions required by the specified distnames. The
  767. dependencies and metadata are fetched from https://metacpan.org/.
  768. After running this script, it is necessary to check the generated files.
  769. For distributions that link against a target library, you have to add the
  770. buildroot package name for that library to the DEPENDENCIES variable.
  771. See the Buildroot documentation for details on the usage of the Perl
  772. infrastructure.
  773. The major version of the host perl must be aligned on the target one,
  774. in order to work with the right CoreList data.
  775. =head1 LICENSE
  776. Copyright (C) 2013-2019 by Francois Perrad <francois.perrad@gadz.org>
  777. This program is free software; you can redistribute it and/or modify
  778. it under the terms of the GNU General Public License as published by
  779. the Free Software Foundation; either version 2 of the License, or
  780. (at your option) any later version.
  781. This program is distributed in the hope that it will be useful,
  782. but WITHOUT ANY WARRANTY; without even the implied warranty of
  783. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  784. General Public License for more details.
  785. You should have received a copy of the GNU General Public License
  786. along with this program; if not, write to the Free Software
  787. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  788. This script is a part of Buildroot.
  789. This script requires the module C<MetaCPAN::API::Tiny> (version 1.131730)
  790. which was included at the beginning of this file by the tool C<fatpack>.
  791. See L<https://metacpan.org/release/NPEREZ/MetaCPAN-API-Tiny-1.131730>.
  792. See L<https://metacpan.org/release/App-FatPacker>.
  793. These both libraries are free software and may be distributed under the same
  794. terms as perl itself.
  795. And perl may be distributed under the terms of Artistic v1 or GPL v1 license.
  796. =cut