Debian Codegraph @

texk/texlive/linked_scripts/ctan-o-mat/ctan-o-mat.pl

raw
1 #!/usr/bin/perl -w
2 ##-----------------------------------------------------------------------------
3 ## This file is part of ctan-o-mat.
4 ## This program is distributed under BSD-like license. See file LICENSE
5 ##
6 ## (c) 2016-2017 Gerd Neugebauer
7 ##
8 ## Net: [email protected]
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the terms of a 3-clause BSD-like license as stated in the file
12 ## LICENSE contained in this distribution.
13 ##
14 ## You should have received a copy of the LICENSE along with this program; if
15 ## not, see the repository under https://github.com/ge-ne/ctan-o-mat.
16 ##
17 ##-----------------------------------------------------------------------------
18
19 =head1 NAME
20
21 ctan-o-mat - Validate and upload a package for CTAN
22
23 =head1 SYNOPSIS
24
25 ctan-o-mat [options] [<package configuration>]
26
27 =head1 DESCRIPTION
28
29 This program can be used to automate the upload of a package to the
30 Comprehensive TeX Archive Network (https://www.ctan.org). The description
31 of the package is taken from a configuration file. Thus it can be updated
32 easily without the need to fill a Web form with the same old information
33 again and again.
34
35 The provided information is validated in any case. If the validation
36 succeeds and not only the validation is requested then the provided
37 archive file is placed in the incoming area of the CTAN for further
38 processing by the CTAN team.
39
40 In any case any finding during the validation is reported at the end
41 of the processing. Note that the validation is the default and a
42 official submission has to be requested by the an appropriate command
43 line option.
44
45 B<ctan-o-mat> requires an Internet connection to the CTAN server. Even the
46 validation retrieves the known attributes and the basic constraints
47 from the server.
48
49
50 =head1 CONFIGURATION
51
52 The default configuration is read from a file with the same name as
53 the current directory an the extension .pkg. This file name can be
54 overwritten on the command line.
55
56 The configuration depends on the features currently supported by the
57 CTAN server.
58 Since these features can change over time the configuration is not
59 hard-coded in B<ctan-o-mat>. You can request an empty template of the
60 configuration via the command line parameter C<--init>.
61
62
63 =head1 OPTIONS
64
65 =over 4
66
67 =item -h
68
69 =item --help
70
71 Print this short summary about the usage and exit the program.
72
73 =item -i
74
75 =item --init
76
77 Create an empty template for a configuration.
78
79 =item --list licenses
80
81 List the known licenses of CTAN to the standard output stream.
82 Each license is represented as one line. The line contains the fields
83 key, name, free indicator. Those fields are separated by tab characters.
84 Afterwards the program terminates without processing any further arguments.
85
86 =item --config <package configuration>
87
88 =item --pkg <package configuration>
89
90 =item --package <package configuration>
91
92 Set the package configuration file.
93
94 =item -s
95
96 =item --submit
97
98 Upload the submission, validate it and officially submit it to CTAN it the
99 validation succeeds.
100
101 =item -v
102
103 =item --verbose
104
105 Print some more information during the processing (verbose mode).
106
107 =item --version
108
109 Print the version number of this program and exit.
110
111 =item --validate
112
113 =item -n
114
115 =item --noaction
116
117 Do not perform the final upload. The package is validated and the
118 resulting messages are printed.
119
120 =item <package>
121
122 This parameter is the name of a package configuration
123 (see section CONFIGURATION) contained in a file.
124 If not set otherwise the package configuration defaults to the
125 name of the current directory with C<.pkg> appended.
126
127 =back
128
129
130 =head1 ENVIRONMENT
131
132 The following environment variables are recognized by B<ctan-o-mat>.
133
134 =over 4
135
136 =item CTAN_O_MAT_URL
137
138 The value is the URL prefix for the CTAN server to be contacted. The default
139 is C<https://ctan.org/submit>. The complete URL is constructed by appending
140 C<validate>, C<upload>, or C<fields> to use the respective CTAN REST API.
141
142 =back
143
144
145 =head1 CONNECTING VIA PROXIES
146
147 If you need to connect to the Internet via a proxy then this can be achieved
148 by setting some environment variables before running B<ctan-o-mat>.
149 To redirect the request via the proxy simply define an environment variable
150 C<http_proxy> to point to the proxy host -- including protocol and port as
151 required. Note that the name of the environment variable is supposed to be in
152 B<lower> case.
153
154
155 =head1 AUTHOR
156
157 Gerd Neugebauer ([email protected])
158
159 =head1 BUGS
160
161 =over 4
162
163 =item *
164
165 The program can not be used without a working connection to the
166 Internet.
167
168 =back
169
170 =cut
171
172 use strict;
173 use FileHandle;
174 use File::Basename;
175 use Cwd;
176
177 use constant VERSION => '1.2';
178
179 #------------------------------------------------------------------------------
180 # Function: usage
181 # Arguments: none
182 # Returns: nothing
183 # Description: Print the POD to stderr and exit
184 #
185 sub usage {
186 use Pod::Text;
187 Pod::Text->new()
188 ->parse_from_filehandle( new FileHandle( $0, 'r' ), \*STDERR );
189 exit(0);
190 }
191
192 #------------------------------------------------------------------------------
193 # Variable: $verbose
194 # Description: The verbosity indicator.
195 #
196 my $verbose = 0;
197
198 #------------------------------------------------------------------------------
199 # Variable: $debug
200 # Description: The debug indicator.
201 #
202 my $debug = 0;
203
204 #------------------------------------------------------------------------------
205 # Variable: $submit
206 # Description: The validation or submit indicator.
207 #
208 my $submit = undef;
209
210 #------------------------------------------------------------------------------
211 # Variable: $cfg
212 # Description: The name of the configuration file.
213 #
214 my $cfg = undef;
215
216 #------------------------------------------------------------------------------
217 # Variable: $CTAN_URL
218 # Description: The base URL for requesting information from the CTAN server.
219 #
220 my $CTAN_URL = $ENV{'CTAN_O_MAT_URL'} || 'https://ctan.org';
221 $CTAN_URL .= '/' if not $CTAN_URL =~ m/\/$/;
222
223 use Getopt::Long;
224 GetOptions(
225 "config=s" => \$cfg,
226 "debug" => \$debug,
227 "h|help" => \&usage,
228 "i|init:s" => sub {
229 local $_ = pkg_name_or_fallback( $_[1], '' );
230 ( new CTAN::Pkg() )->add( pkg => $_ )
231 ->write( new CTAN::Upload::Fields() );
232 exit(0);
233 },
234 "list=s" => sub {
235 if ( $_[1] eq 'licenses' ) {
236 new CTAN::Licenses()->print();
237 }
238 else {
239 print STDERR "*** Unknown entity $_[1]\n";
240 }
241 exit(0);
242 },
243 "n|noaction" => sub { $submit = undef; },
244 "pkg=s" => \$cfg,
245 "package=s" => \$cfg,
246 "submit|upload" => sub { $submit = 1; },
247 "validate" => sub { $submit = undef; },
248 "v|verbose" => \$verbose,
249 "version" => sub {
250 print STDOUT VERSION, "\n";
251 exit(0);
252 },
253 );
254
255 new CTAN::Pkg()
256 ->read( pkg_name_or_fallback( $ARGV[0] || $cfg, '.pkg' ) )
257 ->upload($submit);
258
259 #------------------------------------------------------------------------------
260 # Function: pkg_name_or_fallback
261 # Arguments: $value the value
262 # $ext the extension to append
263 # Description: Construct a fallback from the current directory if $value is
264 # not defined.
265 #
266 sub pkg_name_or_fallback {
267 my ( $value, $ext ) = @_;
268 if ( not defined $value or $value eq '' ) {
269 $value = cwd();
270 $value =~ s|.*[/\\]||;
271 $value = $value . $ext;
272 }
273 return $value;
274 }
275
276 ###############################################################################
277
278 package JSON::Parser;
279
280 #------------------------------------------------------------------------------
281 # Constructor: new
282 # Description: This is the constructor
283 #
284 sub new {
285 my $proto = shift;
286 my $class = ref($proto) || $proto;
287 my $this = {};
288
289 return bless $this, $class;
290 }
291
292 #------------------------------------------------------------------------------
293 # Method: parse
294 # Arguments:
295 # $json the JSON list with the messages
296 # Description: Parse the input string for a JSON object and retrun the Perl
297 # representation of it.
298 #
299 sub parse {
300 my ( $this, $json ) = @_;
301
302 my ( $result, $remainder ) = $this->scan($json);
303 chomp $remainder;
304 if ( $remainder ne '' ) {
305 die "*** Unprocessed JSON: $remainder\n";
306 }
307 return $result;
308 }
309
310 #------------------------------------------------------------------------------
311 # Method: scan
312 # Arguments:
313 # $json the JSON list with the messages
314 # Description: Scan the input string for the next token
315 #
316 sub scan {
317 my ( $this, $json ) = @_;
318 local $_ = $json;
319
320 s/^\s+//;
321 if ( m/^\[\s*/ ) {
322 my @a = ();
323 $_ = $';
324 while ( not m/^\]/ ) {
325 my ( $el, $remainder ) = $this->scan($_);
326 push @a, $el;
327 $_ = $remainder;
328 s/^\s*,\s*//;
329 }
330 $_ = substr( $_, 1 );
331 return ( \@a, $_ );
332 }
333 elsif ( m/^\{\s*/ ) {
334 my %a = ();
335 $_ = $';
336 while ( not m/^\}/ ) {
337 my ( $key, $remainder ) = $this->scan($_);
338 $_ = $remainder;
339 s/^\s*:\s*//;
340 my ( $val, $remainder2 ) = $this->scan($_);
341 $_ = $remainder2;
342 $a{$key} = $val;
343 s/^\s*,\s*//;
344 }
345 $_ = substr( $_, 1 );
346 return ( \%a, $_ );
347 }
348 elsif ( $_ =~ m/^"/ ) {
349 $_ = $';
350 my $s = '';
351 while ( m/(\\.|")/ ) {
352 $s .= $`;
353 $_ = $';
354 if ( $& eq '"' ) {
355 return ( $s, $_ );
356 }
357 if ( $& eq '\\n' ) {
358 $s .= "\n";
359 }
360 elsif ( $& eq '\\"' ) {
361 $s .= '"';
362 }
363 elsif ( $& eq '\\t' ) {
364 $s .= "\t";
365 }
366 elsif ( $& eq '\\\\' ) {
367 $s .= "\\";
368 }
369 elsif ( $& eq '\\r' ) {
370 $s .= "\r";
371 }
372 elsif ( $& eq '\\b' ) {
373 $s .= "\b";
374 }
375 else {
376 $s .= "\\";
377 }
378 }
379 die "*** Missing end of string\n";
380 }
381 elsif ( m/^([0-9]+|[a-z]+)/i ) {
382 $_ = $';
383 $_ = $&;
384 return ( $_, $_ );
385 }
386
387 die "*** Parse error at: $_\n";
388 }
389
390 ###############################################################################
391 package CTAN::Upload::Fields;
392
393 use LWP::UserAgent;
394 use LWP::Protocol::https;
395 use HTTP::Request::Common;
396
397 #------------------------------------------------------------------------------
398 # Variable: @parameter
399 # Description: The list of fields.
400 #
401 my @parameter = (); # FIXME
402
403 #------------------------------------------------------------------------------
404 # Constructor: new
405 # Description: This is the constructor
406 #
407 sub new {
408 my $proto = shift;
409 my $class = ref($proto) || $proto;
410 my $this = {};
411 bless $this, $class;
412 return $this->_load();
413 }
414
415 #------------------------------------------------------------------------------
416 # Method: _load
417 # Arguments: none
418 # Description: Retrieve a list of currently supported fields from the
419 # CTAN server.
420 #
421 sub _load {
422 my $this = shift;
423 my $url = $CTAN_URL . 'submit/fields';
424
425 print STDERR "--- Retrieving fields from CTAN..." if $::verbose;
426 print STDERR $url, "\n" if $debug;
427 my $response;
428 eval {
429 my $ua = LWP::UserAgent->new();
430 my $request = GET $url;
431 print STDERR "done\n" if $::verbose;
432 $response = $ua->request($request);
433 };
434
435 die CTAN::ErrorHandler::format(
436 $response->decoded_content, $response->status_line
437 ),
438 "\n"
439 if not $response->is_success;
440
441 local $_ = $response->decoded_content;
442 print STDERR $response->decoded_content, "\n\n" if $debug;
443 while (m/\"([a-z0-9]+)\":\{([^{}]*)\}/i) {
444 my $f = $1;
445 my %a = ();
446 $_ = $';
447 my $attr = $2;
448 while ( $attr =~ m/\"([a-z0-9]+)\":([a-z0-9]+|"[^"]*")/i ) {
449 $attr = $';
450 $a{$1} = $2;
451 $a{$1} =~ s/(^"|"$)//g;
452 }
453 $this->{$f} = \%a;
454 push @CTAN::Upload::Fields::parameter, $f;
455 }
456 return $this;
457 }
458
459 ###############################################################################
460 package CTAN::Licenses;
461
462 use LWP::UserAgent;
463 use LWP::Protocol::https;
464 use HTTP::Request::Common;
465
466 use Data::Dumper;
467
468 #------------------------------------------------------------------------------
469 # Constructor: new
470 # Description: This is the constructor
471 #
472 sub new {
473 my $proto = shift;
474 my $class = ref($proto) || $proto;
475 my $this = [];
476 bless $this, $class;
477 return $this->_load();
478 }
479
480 #------------------------------------------------------------------------------
481 # Method: _load
482 # Arguments: none
483 # Description: Retrieve a list of currently supported licenses from the
484 # CTAN server.
485 #
486 sub _load {
487 my $this = shift;
488 my $url = $CTAN_URL . 'json/1.3/licenses';
489
490 print STDERR "--- Retrieving licenses from CTAN..." if $verbose;
491 print STDERR $url, "\t" if $debug;
492 my $response;
493 eval {
494 my $ua = LWP::UserAgent->new();
495 my $request = GET $url;
496 print STDERR "done\n" if $verbose;
497 $response = $ua->request($request);
498 };
499
500 die CTAN::ErrorHandler::format(
501 $response->decoded_content, $response->status_line
502 ),
503 "\n"
504 if not $@ and not $response->is_success;
505
506 print STDERR "done\n" if $verbose;
507 local $_ = $response->decoded_content;
508
509 eval {
510 $this->[0] = new JSON::Parser()->parse($_);
511 };
512 if ($@) {
513 s/^[0-9]+ */*** /;
514 die $_;
515 }
516
517 return $this;
518 }
519
520 #------------------------------------------------------------------------------
521 # Method: print
522 # Arguments: none
523 # Description: Print the licenses to stdout.
524 #
525 sub print {
526 my $this = shift;
527 local $_ = $this->[0];
528 my @a = @$_;
529
530 foreach (@a) {
531 print $_->{key}, "\t", $_->{name}, "\t";
532 if ( $_->{free} eq 'true' ) {
533 print "free\n";
534 }
535 else {
536 print "non-free\n";
537 }
538 }
539 }
540
541 ###############################################################################
542 package CTAN::ErrorHandler;
543
544 #------------------------------------------------------------------------------
545 # Method: format
546 # Arguments:
547 # $json the JSON list with the messages
548 # $fallback the fallback message if the first parameter is empty
549 # Description: format the JSON error message
550 #
551 sub format {
552 local $_ = shift;
553 if ( $_ eq '' ) {
554 return shift;
555 }
556 if (m/^(<!DOCTYPE html>|<html)/i) {
557 return "Unexpected HTML response found under $CTAN_URL";
558 }
559
560 my $json;
561 eval {
562 $json = new JSON::Parser()->parse($_);
563 };
564 if ($@) {
565 s/^[0-9]+ */*** /;
566 die $_;
567 }
568 return join( "\n", map { join( ': ', @$_ ) } @$json );
569 }
570
571 ###############################################################################
572 package CTAN::Pkg;
573
574 use LWP::UserAgent;
575 use LWP::Protocol::https;
576 use HTTP::Request::Common;
577
578 #------------------------------------------------------------------------------
579 # Constructor: new
580 # Description: This is the constructor
581 #
582 sub new {
583 my $proto = shift;
584 my $class = ref($proto) || $proto;
585 my $this = [];
586 return bless $this, $class;
587 }
588
589 #------------------------------------------------------------------------------
590 # Method: add
591 # Arguments: arbitrary many key/value pairs
592 # Description:
593 # This function adds a key/value pair to the object.
594 #
595 sub add {
596 my $this = shift;
597 my ( $key, $val );
598 $key = shift;
599 $val = shift;
600 while ( defined $key and defined $val ) {
601 if ( $key eq 'file' ) {
602 push @$this, $key => [$val];
603 }
604 else { push @$this, $key => $val; }
605 $key = shift;
606 $val = shift;
607 }
608 return $this;
609 }
610
611 #------------------------------------------------------------------------------
612 # Method: read
613 # Arguments: $file the file name to be read
614 # Description:
615 # This function parses a configuration file in (La)TeX form and returns
616 # it as hash-like list.
617 #
618 sub read {
619 my ( $this, $file ) = @_;
620 die "*** Configuration file missing.\n" if not defined $file;
621
622 my $fields = new CTAN::Upload::Fields();
623 my $fd = new FileHandle($file)
624 || die "*** Configuration file `$file' could not be read.\n";
625 local $_;
626
627 while (<$fd>) {
628 s/^[ \t]*%.*//;
629 s/([^\\])%.*/$1/;
630 while (m/\\([a-z]+)/i) {
631 $_ = $';
632 my $keyword = $1;
633 if ( $keyword eq 'begin' ) {
634 die "$file:$.: missing {environment} instead of $_\n"
635 if not m/^[ \t]*\{([a-z]*)\}/i;
636 $keyword = $1;
637 my $val = '';
638 $_ = $';
639 while ( not m/\\end\{$keyword\}/ ) {
640 $val .= $_;
641 $_ = <$fd>;
642 die "$file:$.: "
643 . "unexpected end of file while searching end of $keyword\n"
644 if not defined $_;
645 }
646 m/\\end\{$keyword\}/;
647 $_ = $';
648 $val .= $`;
649 $val =~ s/^[ \t\n\r]*//m;
650 $val =~ s/[ \t\n\r]*$//m;
651 $this->add( $keyword => $val );
652 }
653 elsif ( $keyword eq 'endinput' ) {
654 last;
655 }
656 elsif ( defined $fields->{$keyword} ) {
657 die "$file:$.: missing {environment} instead of $_\n"
658 if not m/^[ \t]*\{([^{}]*)\}/i;
659 $_ = $';
660 $this->add( $keyword => $1 );
661 }
662 else {
663 die "$file:$.: undefined keyword $keyword\n";
664 }
665 s/^[ \t]*%.*//;
666 }
667 }
668 $fd->close();
669 return $this;
670 }
671
672 #------------------------------------------------------------------------------
673 # Method: upload
674 # Arguments: Upload a file and the parameters
675 # Description: Connect to the CTAN server to upload or validate the package.
676 #
677 sub upload {
678 my $this = shift;
679 my $submit = shift;
680
681 my $service_url;
682 if ($submit) {
683 print STDERR "--- Sending to CTAN for submission..." if $verbose;
684 $service_url = $CTAN_URL . 'submit/upload';
685 }
686 else {
687 print STDERR "--- Uploading to CTAN for validation..." if $verbose;
688 $service_url = $CTAN_URL . 'submit/validate';
689 }
690 my $ua = LWP::UserAgent->new();
691 my $request = POST(
692 $service_url,
693 'Content_Type' => 'multipart/form-data',
694 'Content' => $this
695 );
696 my $response = $ua->request($request);
697 print STDERR "done\n" if $verbose;
698
699 die CTAN::ErrorHandler::format( $response->decoded_content,
700 $response->status_line )
701 . "\n"
702 if not $response->is_success;
703
704 if ( not $submit and $response->decoded_content eq '[]' ) {
705 print "ok\n";
706 print STDERR "--- The validation has succeeded.\n",
707 "--- You can now submit your package to CTAN for publication.\n"
708 if $verbose;
709 }
710 else {
711 print CTAN::ErrorHandler::format( $response->decoded_content, 'ok' ),
712 "\n";
713 }
714 return $this;
715 }
716
717 #------------------------------------------------------------------------------
718 # Method: write
719 # Arguments: none
720 # Description: Write a new configuration to stdout.
721 #
722 sub write {
723 my $this = shift;
724 my %this = @$this;
725 my $fields = shift;
726
727 print <<__EOF__;
728 % This is a description file for ctan-o-mat.
729 % It manages uploads of a package to
730 % CTAN -- the Comprehensive TeX Archive Network.
731 %
732 % The syntax is roughly oriented towards (La)TeX.
733 % Two form of the macros are used. The simple macros take one argument
734 % in braces. Here the argument may not contain embedded macros.
735 %
736 % The second form uses an environment enclosed in \\begin{}/\\end{}.
737 % In the long text fields logo macros can be used.
738 %
739 % You should enter your values between the begin and the end of the
740 % named type.
741 __EOF__
742 local $_;
743 foreach (@CTAN::Upload::Fields::parameter) {
744 print <<__EOF__;
745 % -------------------------------------------------------------------------
746 % This field contains the $fields->{$_}->{'text'}.
747 __EOF__
748 if ( defined $fields->{$_}->{'nullable'} ) {
749 print "% The value is optional.\n";
750 }
751 if ( defined $fields->{$_}->{'url'} ) {
752 print "% The value is a URL.\n";
753 }
754 if ( defined $fields->{$_}->{'email'} ) {
755 print "% The value is an email address.\n";
756 }
757 if ( defined $fields->{$_}->{'file'} ) {
758 print
759 "% The value is the file name of the archive to be uploaded.\n";
760 print "% It may have a relative or absolute directory.\n";
761 }
762 if ( defined $fields->{$_}->{'maxsize'} ) {
763 print "% The value is restricted to ", $fields->{$_}->{'maxsize'},
764 " characters.\n";
765 }
766 if ( defined $fields->{$_}->{'list'} ) {
767 print "% Multiple values are allowed.\n\\$_\{}\n";
768 }
769 elsif ( defined $fields->{$_}->{'maxsize'}
770 and $fields->{$_}->{'maxsize'} ne 'null'
771 and $fields->{$_}->{'maxsize'} < 256 )
772 {
773 my $v = $this{$_};
774 $v = '' if not defined $v;
775 print "\\$_\{$v\}\n";
776 }
777 elsif ( defined $fields->{$_}->{'file'}
778 and $fields->{$_}->{'file'} eq 'true' )
779 {
780 my $v = $this{$_};
781 $v = '' if not defined $v;
782 print "\\$_\{$v\}\n";
783 }
784 else {
785 my $v = $this{$_};
786 if ( defined $v ) {
787 $v = "\n " + $v + "\n";
788 }
789 else {
790 $v = '';
791 }
792
793 print "\\begin{$_}$v\\end{$_}\n";
794 }
795 }
796 }
797
798 #------------------------------------------------------------------------------
799 # Local Variables:
800 # mode: perl
801 # End: