Debian Codegraph @

texk/texlive/linked_scripts/ctanupload/ctanupload.pl

raw
1 #!/usr/bin/perl
2 ################################################################################
3 # Copyright (c) 2011-2012 Martin Scharrer <[email protected]>
4 # This is open source software under the GPL v3 or later.
5 ################################################################################
6 use strict;
7 use warnings;
8
9 sub fromenv;
10 sub load_data;
11 sub save_data;
12
13 my $VERSION = 'v1.2c from 2012/05/08';
14 my %CTAN_SERVERURLS = (
15 dante => 'http://dante.ctan.org/upload.html',
16 uktug => 'http://www.tex.ac.uk/upload/',
17 );
18 my %CTAN_SERVERALIASES = (
19 de => 'dante',
20 'uk-tug' => 'uktug',
21 uk => 'uktug',
22 cambridge => 'uktug',
23 );
24
25 my $CTAN_URL;
26
27 my @FIELDS = qw(contribution version name email summary directory DoNotAnnounce announce notes license freeversion file);
28 my @REQUIRED = qw(contribution version name email summary license file);
29 my %FIELDS = map { $_ => fromenv($_) } @FIELDS;
30
31
32 my %FIELDS_DESC = (
33 contribution => 'Name of your contribution',
34 version => 'Version number',
35 name => 'Your name',
36 email => 'Your email',
37 summary => 'Summary description',
38 directory => 'Suggested CTAN directory',
39 DoNotAnnounce => 'No need to announce this update',
40 announce => 'Short description for announcement',
41 notes => 'Optional notes to the CTAN maintainers (DANTE only)',
42 license => 'License type',
43 freeversion => 'Which free license',
44 file => 'File to upload',
45 );
46
47 my @LICENSES = qw( free nocommercial nosell nosource shareware artistic other-nonfree);
48
49 my %LICENSES = (
50 free => 'Free',
51 nocommercial => 'Nocommercial',
52 nosell => 'Nosell',
53 nosource => 'Nosource',
54 shareware => 'Shareware',
55 artistic => 'Perl Artistic (Clarified or later)',
56 'other-nonfree' => 'Other nonfree License',
57 );
58
59 my @FREEVERSIONS = qw( apache2 artistic fdl gfl gpl lgpl lppl ofl pd other-free);
60
61 my %FREEVERSIONS = (
62 apache2 => 'Apache License v2.0',
63 artistic => 'Perl Artistic License (version 2)',
64 fdl => 'GNU Free Documentation License',
65 gfl => 'GUST Font License',
66 gpl => 'GNU General Public License',
67 lgpl => 'Lesser GPL',
68 lppl => 'LaTeX Project',
69 ofl => 'Open Font License',
70 pd => 'Public Domain',
71 'other-free' => 'Other Free',
72 );
73
74 my $QUIET = 0;
75 my $ASK = 1;
76 my $FORCE = 0;
77 my $PROMPT = -t STDIN ? 1 : 0;
78 my $LOG = 0;
79
80 my $LOGFILE = $ENV{CTANUPLOAD_LOGFILE};
81
82 sub usage {
83 my $warn = shift;
84 if ($warn) {
85 print STDERR "Unknown option '$warn'\n";
86 }
87 print STDERR <<"EOT";
88 ctanupload script $VERSION
89 Uploads a contribution to CTAN.
90
91 Usage:
92 ctanupload <options> --<name>=<value>
93
94 Arguments can also be given using the CTANUPLOAD environment variable which are processed first, so that
95 explicit arguments will overwrite them.
96
97 Options:
98 -p Prompt for missing values. (default if run in terminal)
99 -P Do not prompt for missing values. (default if not run in terminal)
100 -y Do not ask for confirmation, but submit immediately.
101 -u <URL> CTAN URL to upload to (defaults to CTAN_URL environment variable or random if not set)
102 -U <name> CTAN server to upload to, either 'dante' or 'uktug'.
103 -F <file> Read data from file
104 -f Force upload even if input seems incorrect (unknown license etc.).
105 -v Verbose mode.
106 -q Quiet mode.
107 -l Write log file.
108 -L Do not write log file. (default)
109 -o <file> Define log file. (still requires -l)
110 -h Print this help and exit.
111 -V Print version number.
112
113 The following fields exists and can be given as
114 --<name>=<value>, --<name> <value>, --<name>-file=<file>, --<name>-file <file>
115 or as environment variables: CTAN_<NAME>, CTAN_<name>, <NAME> or <name>.
116
117 EOT
118
119 foreach my $field (@FIELDS) {
120 printf STDERR " %15s %s\n", $field, $FIELDS_DESC{$field};
121 }
122
123 printf STDERR "\nValid license types:\n";
124 for my $license (@LICENSES) {
125 printf STDERR " %15s %s\n", $license, $LICENSES{$license};
126 }
127
128 printf STDERR "\nValid free license:\n";
129 for my $license (@FREEVERSIONS) {
130 printf STDERR " %15s %s\n", $license, $FREEVERSIONS{$license};
131 }
132
133 exit ($warn ? 1 : 0);
134 }
135
136 if (exists $ENV{CTAN_URL}) {
137 unshift @ARGV, ('-u', $ENV{'CTAN_URL'});
138 }
139 elsif (exists $ENV{CTAN_SERVER}) {
140 unshift @ARGV, ('-U', $ENV{'CTAN_SERVER'});
141 }
142
143 if (exists $ENV{'CTANUPLOAD'}) {
144 unshift @ARGV, (split /\s+/, $ENV{'CTANUPLOAD'});
145 }
146
147
148 while (my $arg = shift @ARGV) {
149 if ($arg =~ /^--(.*?)(?:=(.*))?$/) {
150 my $name = $1;
151 my $value = defined $2 ? $2 : shift @ARGV;
152 my $file;
153 if ($name =~ /(.*)-file$/) {
154 $name = $1;
155 $file = $value;
156 }
157 if (exists $FIELDS{$name}) {
158 if ($file) {
159 if (open (my $valfile, '<', $file)) {
160 $value = join '', <$valfile>;
161 close ($valfile);
162 }
163 else {
164 warn "Couldn't read file '$file'!\n";
165 $value = '';
166 }
167 }
168 $FIELDS{$name} = $value;
169 }
170 else {
171 usage('--' . $name . ($file ? '-file' : ''));
172 }
173 }
174 elsif ($arg =~ /^-([a-z]+)$/i) {
175 for my $char (split //, $1) {
176 if ($char eq 'h') {
177 usage(0);
178 }
179 elsif ($char eq 'q') {
180 $QUIET = 1;
181 }
182 elsif ($char eq 'v') {
183 $QUIET = 0;
184 }
185 elsif ($char eq 'y') {
186 $ASK = 0;
187 }
188 elsif ($char eq 'F') {
189 my $file = shift @ARGV;
190 load_data ($file);
191 }
192 elsif ($char eq 'f') {
193 $FORCE = 1;
194 }
195 elsif ($char eq 'p') {
196 $PROMPT = 1;
197 }
198 elsif ($char eq 'P') {
199 $PROMPT = 0;
200 }
201 elsif ($char eq 'u') {
202 $CTAN_URL = shift @ARGV;
203 }
204 elsif ($char eq 'U') {
205 my $server = lc shift @ARGV;
206 $server = $CTAN_SERVERALIASES{$server} || $server;
207 if (not exists $CTAN_SERVERURLS{ $server }) {
208 die "Error: Unknown CTAN server '$server'.\n";
209 }
210 $CTAN_URL = $CTAN_SERVERURLS{ $server };
211 }
212 elsif ($char eq 'l') {
213 $LOG = 1;
214 }
215 elsif ($char eq 'L') {
216 $LOG = 0;
217 }
218 elsif ($char eq 'o') {
219 $LOGFILE = shift @ARGV;
220 }
221 elsif ($char eq 'V') {
222 print STDERR "ctanupload script $VERSION.\n";
223 exit (0);
224 }
225 else {
226 usage('-' . $char);
227 }
228 }
229 }
230 else {
231 usage($arg);
232 }
233 }
234
235 $CTAN_URL = (values %CTAN_SERVERURLS)[int rand scalar values %CTAN_SERVERURLS]
236 if not defined $CTAN_URL;
237
238 my $DANTE = $CTAN_URL =~ /dante/;
239
240 PROMPT:
241
242 if ($PROMPT) {
243 foreach my $field (@FIELDS) {
244 if (!defined $FIELDS{$field} || !length $FIELDS{$field}) {
245 my $input = '';
246 if ($field eq 'notes' || $field eq 'announce') {
247 next if $field eq 'announce' and $FIELDS{DoNotAnnounce};
248 next if $field eq 'notes' and not $DANTE;
249 print "\u$FIELDS_DESC{$field}: (press CTRL-Z (Windows) or CTRL-D (Linux) to stop)\n";
250 while (<STDIN>) {
251 $input .= $_;
252 }
253 }
254 elsif ($field eq 'DoNotAnnounce') {
255 print "\uNo need to announce upload? [y/N]: ";
256 $input = <STDIN>;
257 }
258 elsif ($field eq 'license') {
259 print "\u$FIELDS_DESC{$field}:\n";
260 my $num = 1;
261 foreach my $license (@LICENSES) {
262 printf " [%d] %s\n", $num++, $LICENSES{$license};
263 }
264 print "Selection: ";
265 $input = lc (<STDIN> || '');
266 if ($input =~ /^\s*(\d+)\s*$/) {
267 $input = $LICENSES[$1-1];
268 }
269 }
270 elsif ($field eq 'freeversion') {
271 next if $FIELDS{license} ne 'free';
272 print "\u$FIELDS_DESC{$field}:\n";
273 my $num = 1;
274 foreach my $license (@FREEVERSIONS) {
275 printf " [%d] %s\n", $num++, $FREEVERSIONS{$license};
276 }
277 print "Selection: ";
278 $input = lc (<STDIN> || '');
279 if ($input =~ /^\s*(\d+)\s*$/) {
280 $input = $FREEVERSIONS[$1-1];
281 }
282 }
283 else {
284 print "\u$FIELDS_DESC{$field}: ";
285 $input = <STDIN>;
286 }
287 $input = '' if not defined $input;
288 chomp $input;
289 $input =~ s/^\s+//;
290 $input =~ s/\s+$//;
291 $FIELDS{$field} = $input;
292 }
293 }
294 }
295
296 my $abort = '';
297 foreach my $required (@REQUIRED) {
298 if (!length $FIELDS{$required}) {
299 $abort .= "Required field '$required' is missing!\n";
300 }
301 }
302 if (not exists $LICENSES{ $FIELDS{license} }) {
303 $abort .= "Unknown license type '$FIELDS{license}'!\n"
304 if not $FORCE;
305 }
306 if ($FIELDS{license} ne 'free') {
307 $FIELDS{freeversion} = '-not-selected-';
308 } elsif (!$FIELDS{freeversion}) {
309 $abort .= "Required field 'freeversion' is missing!\n";
310 } elsif (not exists $FREEVERSIONS{ $FIELDS{freeversion} }) {
311 $abort .= "Unknown free license '$FIELDS{freeversion}'!\n"
312 if not $FORCE;
313 }
314 if ($FIELDS{file} && ! -f $FIELDS{file}) {
315 $abort .= "File '$FIELDS{file}' not found!\n";
316 }
317
318 if ($LOG) {
319 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
320 my $DATE = sprintf ('%04d-%02d-%02d %02d:%02d:%02d', $year+1900, $mon+1, $mday, $hour, $min, $sec);
321 if (!$LOGFILE) {
322 $LOGFILE = 'ctanupload-' . $FIELDS{contribution} . " $DATE.log";
323 $LOGFILE =~ tr/: /-_/;
324 }
325 save_data($LOGFILE, 1, "# $DATE");
326 if (!open (LOG, '>>', $LOGFILE)) {
327 warn "Couldn't open logfile '$LOGFILE'.\n";
328 $LOG = 0;
329 }
330 }
331
332 if ($abort) {
333 print $abort, "Aborting!\n";
334 if ($LOG) {
335 $abort =~ s/\n/\n# /gms;
336 print LOG "# $abort\n# Aborting!\n";
337 }
338 exit (2);
339 }
340
341 my $DoNotAnnounce = ($FIELDS{DoNotAnnounce}) ? '[x]' : '[ ]';
342
343 if (!$FIELDS{directory}) {
344 $FIELDS{directory} = '/macros/latex/contrib/' . $FIELDS{contribution};
345 }
346
347
348 print "\nThe following data will be submitted to CTAN ($CTAN_URL):\n";
349
350 foreach my $field (@FIELDS) {
351 if ($field eq 'DoNotAnnounce') {
352 print "$FIELDS_DESC{$field}: $DoNotAnnounce\n";
353 }
354 elsif ($field eq 'license') {
355 print "$FIELDS_DESC{$field}: $LICENSES{$FIELDS{$field}}\n";
356 }
357 elsif ($field eq 'freeversion') {
358 print "$FIELDS_DESC{$field}: $FREEVERSIONS{$FIELDS{$field}}\n";
359 }
360 else {
361 print "$FIELDS_DESC{$field}: ", $FIELDS{$field} || '', "\n";
362 }
363 }
364
365 eval {
366 my $file = $FIELDS{file};
367 die if not -f $file;
368 print "\n$file (", (-s $file), "):\n";
369 if ($file =~ /\.tar\.gz$/) {
370 system ('tar', 'tzvf', $file);
371 }
372 };
373
374 if ($ASK) {
375 print "\nUpload? [(y/N/(e)dit/(s)ave] ";
376 my $response = lc <STDIN>;
377 chomp ($response);
378
379 unless ($response eq 'y' or $response eq 'yes') {
380 if ($response eq 'e' or $response eq 'edit') {
381 my $file = 'ctanupload.dat';
382 save_data($file);
383 my $editor = $ENV{'CTANUPLOAD_EDITOR'} || $ENV{'EDITOR'};
384 if (not defined $editor) {
385 print "No EDITOR environment variable defined.\n";
386 print "Data was stored in file '$file'. Please edit manually and load with the -F option.\n";
387 exit (1);
388 }
389 else {
390 system($editor, $file);
391 load_data($file);
392 goto PROMPT;
393 }
394 }
395 elsif ($response eq 's' or $response eq 'save') {
396 my $file = 'ctanupload.dat';
397 save_data($file);
398 print "Data was stored in file '$file'.\n";
399 print LOG "\n# Data was stored in file '$file'.\n" if $LOG;
400 exit (2);
401 }
402 else {
403 print "\nUpload aborted!\n";
404 print LOG "\n# Upload aborted!\n" if $LOG;
405 exit (1);
406 }
407 }
408 }
409
410 if ($FIELDS{DoNotAnnounce}) {
411 if ($DANTE) {
412 $FIELDS{DoNotAnnounce} = 'No';
413 delete $FIELDS{'to_announce'};
414 $FIELDS{notes} = '';
415 } else {
416 $FIELDS{'to_announce'} = 'Yes';
417 delete $FIELDS{DoNotAnnounce};
418 delete $FIELDS{notes};
419 }
420 }
421 else {
422 if ($DANTE) {
423 $FIELDS{DoNotAnnounce} = 'Yes';
424 delete $FIELDS{to_announce};
425 } else {
426 delete $FIELDS{'to_announce'};
427 delete $FIELDS{DoNotAnnounce};
428 delete $FIELDS{notes};
429 }
430 }
431 # Add Submit button for the sake of completeness:
432 $FIELDS{'SUBMIT'} = ($DANTE) ? 'Submit' : 'Submit contribution';
433
434 use WWW::Mechanize;
435 my $mech = WWW::Mechanize->new(quiet => $QUIET, autocheck => 1);
436
437 print "Accessing CTAN upload page ...\n";
438 $mech->get( $CTAN_URL );
439 print "Uploading ...\n";
440 $mech->submit_form(
441 form_number => 1,
442 fields => \%FIELDS,
443 );
444
445 if ($mech->success()) {
446 print "HTTP response code: ", $mech->response()->message(), "\n";
447 }
448
449 print "\nResponse:\n";
450 print LOG "\n# Response:\n" if $LOG;
451 eval {
452 if ($mech->response()->header('Content-Type') eq 'text/plain') {
453 #open (my $DEBUGFILE, '>', 'ctanupload.dbg');
454 #print {$DEBUGFILE} $mech->content();
455 #close ($DEBUGFILE);
456 print $mech->content();
457 print LOG $mech->content() if $LOG;
458 }
459 else {
460 use HTML::TreeBuilder;
461 use HTML::FormatText;
462 my $tree = HTML::TreeBuilder->new_from_content( $mech->content() );
463 my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 80);
464 my @response = split /\n/, $formatter->format($tree);
465 local $, = "\n";
466 print @response[3..$#response-3];
467 local $, = "\n# ";
468 print LOG '', @response[3..$#response-3] if $LOG;
469 }
470 1;
471 } or do {
472 print "Can't display HTML response, storing reponse if log file 'ctanupload_response.html'\n";
473 open (HLOG, '>', 'ctanupload_response.html');
474 print HLOG $mech->content();
475 close (HLOG);
476 };
477
478 print "\n";
479 close LOG if $LOG;
480 exit (0);
481
482
483 sub fromenv {
484 my $name = shift;
485 foreach my $var ('CTAN_' . uc $name, uc $name, 'CTAN_' . $name, $name) {
486 return $ENV{$var} if exists $ENV{$var};
487 }
488 return undef;
489 }
490
491 sub load_data {
492 my $file = shift;
493 open (my $load, '<', $file) or die "Couldn't open data file '$file'!\n";
494 LOAD_LOOP:
495 while (my $line = <$load>) {
496 next if ($line =~ /^\s*(#|$)/);
497 if ($line =~ /^([a-z]+)\s*=\s*(.*)$/i) {
498 my ($name,$value) = ($1,$2);
499 if (!exists $FIELDS{$name}) {
500 print STDERR "Ignoring unknown variable '$name' from file '$file' (line $.)!\n";
501 next;
502 }
503 $FIELDS{$name} = $value;
504 if ($name eq 'announce' || $name eq 'notes') {
505 $FIELDS{$name} .= "\n";
506 while ($line = <$load>) {
507 next if ($line =~ /^\s*#/);
508 last if ($line =~ /^[a-z]+\s*=/i);
509 $FIELDS{$name} .= $line;
510 }
511 $FIELDS{$name} =~ s/^[\s\n]+//;
512 $FIELDS{$name} =~ s/[\s\n]+$//;
513 redo LOAD_LOOP if not eof $load;
514 }
515 }
516 else {
517 print STDERR "Ignoring incorrect line '$line' from file '$file' (line $.)!\n";
518 }
519 }
520 close ($load);
521 }
522
523 sub save_data {
524 my $file = shift;
525 my $storefileinfo = shift;
526 my $header = shift;
527 $file = 'ctanupload.dat' if not defined $file;
528 open (my $save, '>', $file) or do { warn "Couldn't save data to file '$file'!\n"; return; };
529 print {$save} "$header\n" if $header;
530 foreach my $field (@FIELDS) {
531 if ($field eq 'DoNotAnnounce') {
532 print {$save} "$field = ", $FIELDS{DoNotAnnounce} ? '1' : '0', "\n";
533 }
534 else {
535 print {$save} "$field = ", $FIELDS{$field} || '', "\n";
536 }
537 }
538 if ($storefileinfo && -f $FIELDS{file}) {
539 my $file = $FIELDS{file};
540 print {$save} "\n# $file (", (-s $file), "):\n";
541 if ($file =~ /\.tar\.gz$/) {
542 open (my $TAR, '-|', "tar tzvf $file");
543 print {$save} map { "# $_" } <$TAR>;
544 print {$save} "\n";
545 close ($TAR);
546 }
547 }
548 close ($save);
549 return;
550 }
551
552
553
554 __END__