-
Notifications
You must be signed in to change notification settings - Fork 4
/
portroach.pl
1802 lines (1411 loc) · 44 KB
/
portroach.pl
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# Copyright (C) 2005-2011, Shaun Amott. All rights reserved.
# Copyright (C) 2014-2015, Jasper Lievisse Adriaanse. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#------------------------------------------------------------------------------
use IO::Handle;
use File::Basename;
use File::Copy;
use Socket;
use POSIX;
use Fcntl;
use Proc::Queue;
use Sys::Hostname;
use LWP::UserAgent;
use MIME::Lite;
use Net::FTP;
use URI;
use JSON qw(decode_json);
use DBI;
use Portroach;
use Portroach::Const;
use Portroach::Util;
use Portroach::Config;
use feature qw(switch);
no if $] >= 5.018, warnings => "experimental::smartmatch";
use strict;
#use warnings;
require v5.10.0;
#------------------------------------------------------------------------------
# Globals
#------------------------------------------------------------------------------
my @bad_versions;
my $datasrc;
@bad_versions =
qw(win32 cygwin linux osx hpux irix hp-ux hp_ux solaris
hp-ux irix mac-?os darwin aix macintosh manual docs?
examples sunos tru64 rh\d-rpm suse sun4[a-z]? cvs snap
win jdk i[3-6]86 theme nolib dyn lin(?:ux)?(?:\d\d)?
\.exe$ pkg elf debian html mingw);
#------------------------------------------------------------------------------
# Signal Handlers
#------------------------------------------------------------------------------
sub terminate
{
local $SIG{INT} = 'IGNORE';
kill('TERM', -$$);
print "PID #$$ Terminating...\n";
exit 1;
}
sub reaper
{
my $child;
(1) while (($child = waitpid(-1, WNOHANG)) > 0);
$SIG{CHLD} = \&reaper;
}
$SIG{INT} = \&terminate;
$SIG{TERM} = \&terminate;
#$SIG{CHLD} = \&reaper;
$SIG{PIPE} = 'IGNORE';
#------------------------------------------------------------------------------
# Begin Code
#------------------------------------------------------------------------------
main();
#------------------------------------------------------------------------------
# Func: main()
# Desc: Pseudo script entry-point.
#
# Args: n/a
#
# Retn: n/a
#------------------------------------------------------------------------------
sub main
{
my ($dbengine);
if ($settings{debug}) {
print STDERR '-' x 72 . "\n";
print STDERR "Using settings:\n";
print STDERR " Variable: $_ -> $settings{$_}\n"
foreach (keys %settings);
print STDERR '-' x 72 . "\n";
}
Usage() if (!defined $ARGV[0]);
if ($ARGV[0] eq 'debug')
{
if ($#ARGV == 3 and $ARGV[1] eq 'vercompare')
{
my $res;
print 'vercompare: ';
if ($ARGV[2] eq $ARGV[3]) {
$res = '=';
} elsif (vercompare($ARGV[2], $ARGV[3])) {
$res = '>';
} else {
$res = '<';
}
print "$ARGV[2] $res $ARGV[3]\n";
exit 0;
} else {
Usage();
}
}
print APPNAME.' v'.APPVER.', by '.AUTHOR."\n\n";
SwitchUser();
# Load stuff specific to the database engine we're using
$dbengine = $settings{db_connstr};
$dbengine =~ s/^\s*DBI:([A-Za-z0-9]+):?.*$/$1/;
Portroach::SQL->Load($dbengine)
or die 'Failed to load queries for DBI engine "' . $dbengine . '"';
# Check DB schema version
if (getdbver() != DB_VERSION) {
print STDERR "Database schema mismatch; did you forget to upgrade?\n";
exit 1;
}
if ($dbengine eq 'SQLite' && $settings{num_children} > 0) {
print STDERR "SQLite is currently only supported in non-forking mode!\n"
. "--> Forcing num_children => 0...\n\n";
$settings{num_children} = 0;
sleep 2;
}
$datasrc = Portroach::DataSrc->new(
$settings{datasrc},
$settings{datasrc_opts}
);
# Handle for the Sqlports database so we can close it at the right time.
my $sdbh = Portroach::SQL::connect_sqlports($settings{sqlports});
my $rc = (ExecArgs($ARGV[0], $sdbh) ? 0 : 1);
$sdbh->disconnect();
exit $rc;
}
#------------------------------------------------------------------------------
# Func: ExecArgs()
# Desc: Initiate primary operation requested by user.
#
# Args: $cmd - Command to execute
#
# Retn: $success - true/false
#------------------------------------------------------------------------------
sub ExecArgs
{
my ($cmd, $sdbh) = @_;
my $res;
if ($cmd eq 'build')
{
print "-- [ Building ports database ] -----------------------------------------\n\n";
$res = $datasrc->Build($sdbh);
}
elsif ($cmd eq 'check')
{
print "-- [ Checking ports distfiles ] ----------------------------------------\n\n";
Proc::Queue::size($settings{num_children})
unless($settings{num_children} == 0);
$res = Check($sdbh);
}
elsif ($cmd eq 'generate')
{
Portroach::Template->templatedir($settings{templates_dir} . '/' . $settings{output_type});
Portroach::Template->outputdir($settings{html_data_dir});
$res = GenerateHTML();
}
elsif ($cmd eq 'rebuild')
{
$res = $datasrc->Build($sdbh);
if ($res) {
$res = Prune($sdbh);
}
}
elsif ($cmd eq 'mail')
{
Portroach::Template->templatedir($settings{templates_dir});
if ($settings{mail_method} ne 'sendmail') {
MIME::Lite->send($settings{mail_method}, $settings{mail_host});
}
$res = MailMaintainers();
}
elsif ($cmd eq 'showupdates')
{
$res = ShowUpdates();
}
elsif ($cmd eq 'add-mail' or $cmd eq 'remove-mail')
{
my (@addrs) = @ARGV; # Should be a list of addrs
shift @addrs; # Remove $cmd
Usage() if (!@addrs);
$res = ($cmd eq 'add-mail')
? AddMailAddrs(@addrs)
: RemoveMailAddrs(@addrs);
}
elsif ($cmd eq 'show-mail')
{
$res = ShowMailAddrs();
}
elsif ($cmd eq 'uncheck')
{
$res = Uncheck();
}
elsif ($cmd eq 'prune')
{
$res = Prune($sdbh);
}
else
{
Usage();
}
return $res;
}
#------------------------------------------------------------------------------
# Func: Check()
# Desc: Using the information found from a run of Build(), attempt to
# identify ports with possible updated distfiles.
#
# Args: n/a
#
# Retn: $success - true/false
#------------------------------------------------------------------------------
sub Check
{
my $sdbh = shift;
my (%sths, @workblock, $dbh, $nofork, $num_rows, $i);
$nofork = ($settings{num_children} == 0);
$dbh = connect_db();
prepare_sql($dbh, \%sths, qw(portdata_count portdata_select));
STDOUT->autoflush(1);
$sths{portdata_count}->execute(lc hostname());
($num_rows) = $sths{portdata_count}->fetchrow_array;
$sths{portdata_select}->execute(lc hostname());
if ($nofork) {
prepare_sql($dbh, \%sths,
qw(portdata_setchecked portdata_setnewver
sitedata_select sitedata_failure sitedata_success
sitedata_initliecount sitedata_decliecount)
);
}
$i = 0;
while (my $port = $sths{portdata_select}->fetchrow_hashref)
{
my $want = 0;
$i++;
$want = wantport($port->{name}, $port->{cat}, $port->{maintainer});
if ($nofork) {
# This is all we need if we're not forking.
VersionCheck($dbh, \%sths, $port) if $want;
next;
}
push @workblock, $port if ($port and $want);
next if (!$want and $i < $num_rows);
# Got enough work?
if ($#workblock > $settings{workqueue_size} or $i == $num_rows)
{
my $pid = fork;
die "Cannot fork: $!" unless (defined $pid);
if ($pid) {
# Parent
my $progress = $num_rows - $i;
print "Spawned PID #$$ ($progress ports unallocated)\n";
undef @workblock;
} else {
# Child
my (%sths, $dbh, $time);
$time = time;
$dbh = connect_db(1);
prepare_sql($dbh, \%sths,
qw(portdata_setchecked portdata_setnewver
sitedata_select sitedata_failure sitedata_success
sitedata_initliecount sitedata_decliecount)
);
while (my $port = pop @workblock) {
VersionCheck($dbh, \%sths, $port);
}
finish_sql($dbh, \%sths);
$dbh->disconnect;
$time = (time - $time);
print "PID #$$ finished work block (took $time seconds)\n";
exit;
}
(1) while (waitpid(-1, WNOHANG) > 0);
}
}
(1) while (wait != -1);
if ($sths{portdata_select}->rows == 0) {
print "No ports found.\n";
} else {
print !$nofork
? "Master process finished. All work has been distributed.\n"
: "Finished.\n";
}
finish_sql($dbh, \%sths);
$dbh->disconnect;
return 1;
}
#------------------------------------------------------------------------------
# Func: Uncheck()
# Desc: Reset all newver, status, and checked fields in database - equivalent
# to doing a fresh build.
#
# Args: n/a
#
# Retn: n/a
#------------------------------------------------------------------------------
sub Uncheck
{
my ($dbh, $sth);
$dbh = connect_db();
$sth = $dbh->prepare($Portroach::SQL::sql{portdata_uncheck})
or die DBI->errstr;
print "Resetting 'check' data...\n";
$sth->execute;
$sth->finish;
$dbh->disconnect;
}
#------------------------------------------------------------------------------
# Func: VersionCheck()
# Desc: Check for an updated version of one particular port.
#
# Args: $dbh - Database handle
# \%sths - Prepared database statements
# \$port - Port data extracted from database
#
# Retn: n/a
#------------------------------------------------------------------------------
sub VersionCheck
{
my ($dbh, $sths, $port) = @_;
my ($k, $i);
$k = $port->{name};
$i = 0;
# Override MASTER_SITES if requested
$port->{mastersites} = $port->{indexsite} if ($port->{indexsite});
return if (!$port->{distfiles} || !$port->{mastersites});
info(0, $k, 'VersionCheck()');
# Loop through master sites
$sths->{sitedata_select}->execute($port->{mastersites});
while (my $sitedata = $sths->{sitedata_select}->fetchrow_hashref)
{
my (@files, @dates, $site, $path_ver, $new_found, $old_found);
my $method = METHOD_LIST;
$old_found = 0;
$new_found = 0;
$site = (grep /:\/\/\Q$sitedata->{host}\E\//, (split ' ', $port->{mastersites}))[0]
or next;
$site = URI->new($site)->canonical;
last if ($i >= $settings{mastersite_limit});
$i++;
info(0, $k, 'Checking site: ' . strchop($site, 60));
# Look to see if the URL contains the distfile version.
# This will affect our checks and guesses later on.
if ($port->{ver} =~ /^(?:\d+\.)+\d+$/
or $port->{ver} =~ /$date_regex/i) {
my ($lastdir, $majver);
$lastdir = uri_lastdir($site);
# Also check version sans last number if >= 3 numbers
# In other words, the "major" version.
# This could be emulated for date strings, but it
# gets a bit messy deciphering that format.
if ($port->{ver} =~ /^(?:\d+\.){2,}\d+$/) {
$majver = $port->{ver};
$majver =~ s/\.\d+$//;
}
# Look for a match
if ($lastdir eq $port->{ver}) {
# Last directory = current version
$path_ver = $lastdir;
} elsif ($majver && $lastdir eq $majver) {
# Last directory = current major version
$path_ver = $lastdir;
}
}
# Check for special handler for this site first
if (my $sh = Portroach::SiteHandler->FindHandler($site))
{
info(0, $k, $site, 'Using dedicated site handler for site.');
if (!$sh->GetFiles($site, $port, \@files)) {
info(0, $k, $site, 'SiteHandler::GetFiles() failed for ' . $site);
next;
} else {
$method = METHOD_HANDLER;
}
}
elsif ($site->scheme eq 'ftp')
{
my $ftp;
$ftp = Net::FTP->new(
$site->host,
Port => $site->port,
Timeout => $settings{ftp_timeout},
Debug => $settings{debug},
Passive => $settings{ftp_passive}
);
if (!$ftp) {
info(0, $k, $site, 'FTP connect problem: ' . $@);
$sths->{sitedata_failure}->execute($site->host)
unless ($settings{precious_data});
next;
}
my $ftp_failures = 0;
while ($ftp_failures <= $settings{ftp_retries}) {
if (!$ftp->login('anonymous')) {
info(0, $k, $site, 'FTP login error: ' . $ftp->message);
if ($ftp_failures == 0) {
$sths->{sitedata_failure}->execute($site->host)
unless ($settings{precious_data});
}
$ftp_failures++;
if ($ftp->message =~ /\b(?:IP|connections|too many|connected)\b/i) {
my $rest = 2+(int rand 15);
info(0, $k, $site,
"Retrying FTP site in $rest seconds "
. "(attempt $ftp_failures of "
. "$settings{ftp_retries})"
);
sleep $rest;
next;
} else {
last;
}
}
$ftp_failures = 0;
last;
}
next if ($ftp_failures);
# This acts as an error check, so we'll cwd to our
# original directory even if we're not going to look
# there.
if (!$ftp->cwd($site->path || '/')) {
$ftp->quit;
info(0, $k, $site, 'FTP cwd error: ' . $ftp->message);
$sths->{sitedata_failure}->execute($site->host)
unless ($settings{precious_data});
next;
}
@files = $ftp->ls;
if (!@files) {
info(0, $k, $site, 'FTP ls error (or no files found): ' . $ftp->message);
$ftp->quit;
next;
}
# Did we find a version in site path earlier? If so,
# we'll check the parent directory for other version
# directories.
if ($path_ver) {
my ($path);
my $site = $site->clone;
uri_lastdir($site, undef);
$path = $site->path;
# Parent directory
if ($ftp->cwd($site->path)) {
foreach my $dir ($ftp->ls) {
# Potential sibling version dirs
if ($dir =~ /^(?:\d+\.)+\d+$/
or $dir =~ /$date_regex/i) {
$site->path("$path$dir");
if ($ftp->cwd($site->path)) {
# Potential version files
push @files, "$path$dir/$_"
foreach ($ftp->ls);
}
}
}
}
}
$ftp->quit;
if (!@files) {
info(0, $k, $site, 'No files found.');
next;
}
}
else
{
my ($ua, $response);
unless (robotsallowed($dbh, $site, $sitedata)) {
info(0, $k, $site, 'Ignoring site as per rules in robots.txt.');
# Don't count 'robots' bans as a failure.
# (We fetch them from the database so that
# they can be re-checked every so often.)
$i--;
next;
}
$ua = LWP::UserAgent->new;
$ua->agent(USER_AGENT);
$ua->timeout($settings{http_timeout});
$response = $ua->get($site);
# A 404 here ought to imply that the distfile
# is unavailable, since we expect it to be
# inside this directory. However, some sites
# use scripts or rewrite rules disguised as
# directories.
if ($response->is_success) {
extractfilenames($response->content, $port->{sufx},
\@files, \@dates);
if (@files && $path_ver) {
# Directory listing a success: we can
# investigate $path_ver variations...
my $site = $site->clone;
my (@dirs, $path);
# Visit parent directory
uri_lastdir($site, undef);
$path = $site->path;
$response = $ua->get($site);
extractdirectories($response->content, \@dirs)
if ($response->is_success);
# Investigate sibling version dirs
foreach my $dir (@dirs) {
if ($dir =~ /^(?:\d+\.)+\d+$/
or $dir =~ /$date_regex/i) {
my @files_tmp;
$site->path("$path$dir");
$response = $ua->get($site);
extractfilenames(
$response->content,
$port->{sufx},
\@files_tmp,
\@dates
) if ($response->is_success);
push @files, "$path$dir/$_"
foreach (@files_tmp);
}
}
}
}
if ($settings{debug}) {
print STDERR "Files for $port->{cat}/$port->{name} from $site:\n";
print STDERR " --> $_\n"
foreach @files;
}
# No files found - try some guesses
if (!@files && !$port->{indexsite})
{
my (%headers, $ua, $response, $url);
my $bad_mimetypes = 'html|text|css|pdf|jpeg|gif|png|image|mpeg|bitmap';
$ua = LWP::UserAgent->new;
$ua->agent(USER_AGENT);
$ua->timeout($settings{http_timeout});
$url = $site;
$url .= '/' unless $url =~ /\/$/;
# We keep a counter of "lies" from each site, and only
# re-check every so often.
if ($sitedata->{liecount} > 0) {
info(0, $k, $site, 'Not doing any guessing; site has previously lied.');
$sths->{sitedata_decliecount}->execute($sitedata->{host})
unless($settings{precious_data});
next;
}
# Verify site gives an error for bad filenames
$response = $ua->head($url.randstr(8).'_shouldntexist.tar.gz');
%headers = %{$response->headers};
# Got a response which wasn't HTTP 4xx -> bail out
if ($response->is_success && $response->status_line !~ /^4/) {
info(0, $k, $site, 'Not doing any guessing; site is lieing to us.');
$sths->{sitedata_initliecount}->execute($sitedata->{host})
unless($settings{precious_data});
next;
}
foreach (
verguess(
$port->{newver} ? $port->{newver} : $port->{ver},
$port->{limitwhich}
)
) {
my $guess_v = $_;
my $old_v = quotemeta $port->{ver};
my $s = quotemeta $port->{sufx};
# Only change major version if port isn't
# version-specific
if ($port->{limitver}) {
next unless ($guess_v =~ /$port->{limitver}/);
} elsif ($port->{name} =~ /^(.*\D)(\d{1,3})(?:[-_]\D+)?$/) {
my $nm_nums = $2;
my $vr_nums = $guess_v;
my $vo_nums = $old_v;
unless (($1.$2) =~ /(?:md5|bz2|bzip2|rc4|rc5|ipv6|mp3|utf8)$/i) {
my $fullver = "";
while ($vo_nums =~ s/^(\d+?)[-_\.]?//) {
$fullver .= $1;
last if ($fullver eq $nm_nums);
}
if ($fullver eq $nm_nums) {
$vr_nums =~ s/[-_\.]//g;
next unless ($vr_nums =~ /^$nm_nums/);
}
}
}
if ($port->{skipversions}) {
my @skipvers = split /\s+/, $port->{skipversions};
arrexists(\@skipvers, $guess_v)
and next;
}
info(0, $k, $site, "Guessing version $port->{ver} -> $guess_v");
foreach my $distfile (split ' ', $port->{distfiles})
{
my $site = $site->clone;
next unless ($distfile =~ s/$old_v/$guess_v/gi);
if ($path_ver) {
my ($path);
uri_lastdir($site, undef);
$path = $site->path;
if ($path_ver ne $port->{ver}) {
# Major ver in site path
my $guess_maj = $guess_v;
$guess_maj =~ s/\.\d+$//;
$site->path("$path$guess_maj/");
} else {
# Full ver in site path
$site->path("$path$guess_v/");
}
}
my $response = $ua->head($url.$distfile);
my %headers = %{$response->headers};
if ($response->is_success && $response->status_line =~ /^2/ &&
$headers{'content-type'} !~ /($bad_mimetypes)/i) {
info(0, $k, $site, "UPDATE $port->{ver} -> $guess_v");
$sths->{portdata_setnewver}->execute(
$guess_v, METHOD_GUESS, $url.$distfile,
$port->{id}
) unless ($settings{precious_data});
$new_found = 1;
last;
} else {
info(0, $k, $site, "Guess failed $port->{ver} -> $guess_v");
}
last if ($new_found);
}
last if ($new_found);
}
}
last if ($new_found);
}
# Make note of working site
$sths->{sitedata_success}->execute($site->host);
next if (!@files);
my $file = FindNewestFile($port, $site, \@files);
$old_found = 1 if $file->{oldfound};
if ($file && $file->{newfound}) {
info(0, $k, $site, "UPDATE $port->{ver} -> $file->{version}");
$sths->{portdata_setnewver}->execute(
$file->{version},
$method,
$file->{url},
$port->{id}
) unless ($settings{precious_data});
last;
}
last if ($old_found && $settings{oldfound_enable});
}
# Update checked timestamp
$sths->{portdata_setchecked}->execute($port->{id})
unless ($settings{precious_data});
info(0, $k, 'Done');
}
#------------------------------------------------------------------------------
# Func: FindNewestFile()
# Desc: Given an array of files, try to determine if any are newer than our
# current version, and return the newest, if any.
#
# Args: \%port - Port hash from database.
# $site - Site URL.
# \@files - Files returned from spidering (+ absolute path or no path).
#
# Retn: \%res - Hash containing file info:
# newfound - True if we found a suitable file.
# oldfound - True if we found the "current" file.
# version - Version of file found.
# url - URL of file.
#------------------------------------------------------------------------------
sub FindNewestFile
{
my ($port, $site, $files) = @_;
my ($poss_match, $poss_url, $old_found, $new_found, $golang);
foreach my $file (@$files)
{
my ($poss_path, $github);
if ($file =~ /^(.*)\/(.*?)$/) {
# Files from SiteHandlers can come with paths
# attached; we're only handling absolute paths
# here though (XXX: future handlers?)
$poss_path = $1;
$file = $2;
} else {
$poss_path = '';
}
$golang = 1 if ($port->{mastersites} =~ /proxy\.golang\.org/);
foreach my $distfile (split ' ', $port->{distfiles})
{
# in Go we explicitly know what the next version is if we have it.
next if ($golang);
my $v = $port->{ver};
my $s = $port->{sufx};
my $old_v;
$github = 1 if ($site->clone =~ /https:\/\/github.com\//);
if ($github) {
$old_v = $distfile;
} else {
$old_v = $v;
}
my $skip = 0;
if ($poss_path) {
# Do a full-URL comparison for $old_found
# if we're dealing with paths too.
my ($new_url, $old_url);
# $site + abs. path
$new_url = $site->clone;
$new_url->path($poss_path.'/'.$file);
# $site + filename
$old_url = $site->clone;
uri_filename($old_url, $distfile);
if (URI::eq($old_url, $new_url)) {
$old_found = 1;
next;
}
} else {
if ($file eq $distfile) {
$old_found = 1;
next;
}
}
# Skip beta versions if requested
if ($port->{skipbeta}) {
if (isbeta($file) && !isbeta($distfile)) {
next;
}
}
# Weed out some bad matches
if ($settings{freebsdhacks_enable}) {
foreach (@bad_versions) {
if ($file =~ /$_/i && $distfile !~ /$_/i) {
$skip = 1;
last;
}
}
}
next if ($skip);
# XXX Force number at start - is this reasonable?
# XXX: multiple occurences of $v in distfile?
next unless ($distfile =~ s/^(.*?)\Q$v\E(.*)$/\Q$1\E(\\d.*?)\Q$2\E/);
# Possible candidate - extract version
if (($file =~ /^($distfile)$/ && $2) or $github)
{
my ($version, $new_v);
unless ($github) {
$version = $2;
$new_v = lc $version;
# Catch a few missed cases
$new_v =~ s/(?:$ext_regex)$//;
# Version is much longer than original - skip it
next if (length $new_v > (12 + length $old_v));
# New version is in date format (or contains a date-like
# string) - old one is not. Probably best to ignore.
next if (
$new_v =~ /$date_regex/i &&
$old_v !~ /$date_regex/i
);
# Skip a few strange version format change cases
# (formatted -> "just a number")
next if ($new_v !~ /\./ && $old_v =~ /\./);
} else {
# GitHub is "special" as the API gives us a non-${EXTRACT_SUFX}
# as filename (e.g. tarball/v0.25.1). The sitehandler uses the
# last part to stub a distname thru 'project%%$V.tar.gz'.
# The '%%' placeholder is used to make it easier here to extract
# the actual version.
# NB: The link in the webinterface is will still point to the
# previous tag as the recorded master site contains a
# hardcoded version. Little we can do at this point.
$version = $1 if $file =~ m/%%(.*)\.tar.gz/;
# Turn this into the real filename and set $new_v to the filename.
$file =~ s/%%/-/;
$new_v = $file;
}