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
|
#!/usr/bin/env perl
#
# vim: set ts=4:noet
#
# Lib.pm
# shared functions for the sbo_ scripts.
#
# authors: Jacob Pipkin <j@dawnrazor.net>
# Luke Williams <xocel@iquidus.org>
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>
use 5.16.0;
use strict;
use warnings FATAL => 'all';
package SBO::Lib;
our $VERSION = '1.3';
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
script_error
open_fh
open_read
show_version
slackbuilds_or_fetch
fetch_tree
update_tree
get_installed_packages
get_inst_names
get_available_updates
get_requires
get_readme_contents
do_slackbuild
make_clean
make_distclean
do_upgradepkg
get_sbo_location
get_from_info
get_tmp_extfn
get_arch
get_build_queue
merge_queues
get_installed_cpans
$tempdir
$conf_dir
$conf_file
%config
);
$< == 0 or die "This script requires root privileges.\n";
use Tie::File;
use Sort::Versions;
use Digest::MD5;
use File::Copy;
use File::Path qw(make_path remove_tree);
use File::Temp qw(tempdir tempfile);
use File::Find;
use File::Basename;
use Fcntl qw(F_SETFD F_GETFD);
our $tempdir = tempdir(CLEANUP => 1);
# define this to facilitate unit testing - should only ever be modified from
# t/test.t
our $pkg_db = '/var/log/packages';
# subroutine for throwing internal script errors
sub script_error(;$) {
exists $_[0] ? die "A fatal script error has occurred:\n$_[0]\nExiting.\n"
: die "A fatal script error has occurred. Exiting.\n";
}
# sub for opening files, second arg is like '<','>', etc
sub open_fh {
exists $_[1] or script_error 'open_fh requires two arguments';
unless ($_[1] eq '>') {
-f $_[0] or script_error "open_fh, $_[0] is not a file";
}
my ($file, $op) = @_;
open my $fh, $op, $file or die "Unable to open $file.\n";
return $fh;
}
sub open_read($) {
return open_fh(shift, '<');
}
# global config variables
our $conf_dir = '/etc/sbotools';
our $conf_file = "$conf_dir/sbotools.conf";
our %config = (
NOCLEAN => 'FALSE',
DISTCLEAN => 'FALSE',
JOBS => 'FALSE',
PKG_DIR => 'FALSE',
SBO_HOME => 'FALSE',
);
# subroutine to suck in config in order to facilitate unit testing
sub read_config() {
my %conf_values;
if (-f $conf_file) {
my $fh = open_read $conf_file;
my $text = do {local $/; <$fh>};
%conf_values = $text =~ /^(\w+)=(.*)$/mg;
close $fh;
}
for my $key (keys %config) {
$config{$key} = $conf_values{$key} if exists $conf_values{$key};
}
$config{JOBS} = 'FALSE' unless $config{JOBS} =~ /^\d+$/;
$config{SBO_HOME} = '/usr/sbo' if $config{SBO_HOME} eq 'FALSE';
}
read_config;
# some stuff we'll need later - define first two as our for unit testing
our $distfiles = "$config{SBO_HOME}/distfiles";
our $slackbuilds_txt = "$config{SBO_HOME}/SLACKBUILDS.TXT";
my $name_regex = '\ASLACKBUILD\s+NAME:\s+';
sub show_version() {
say "sbotools version $VERSION";
say 'licensed under the WTFPL';
say '<http://sam.zoy.org/wtfpl/COPYING>';
}
# %supported maps what's in /etc/slackware-version to what's at SBo
# which is now not needed since this version drops support < 14.0
# but it's already future-proofed, so leave it.
sub get_slack_version() {
my %supported = ('14.0' => '14.0');
my $fh = open_read '/etc/slackware-version';
chomp(my $line = <$fh>);
close $fh;
my $version = ($line =~ /\s+(\d+[^\s]+)$/)[0];
die "Unsupported Slackware version: $version\n"
unless $version ~~ %supported;
return $supported{$version};
}
# does the SLACKBUILDS.TXT file exist in the sbo tree?
sub chk_slackbuilds_txt() {
return -f $slackbuilds_txt ? 1 : undef;
}
# check for the validity of new $config{SBO_HOME}
sub check_home() {
my $sbo_home = $config{SBO_HOME};
if (-d $sbo_home) {
opendir(my $home_handle, $sbo_home);
FIRST: while (readdir $home_handle) {
next FIRST if /^\.[\.]{0,1}$/;
die "$sbo_home exists and is not empty. Exiting.\n";
}
} else {
make_path($sbo_home) or die "Unable to create $sbo_home.\n";
}
return 1;
}
# rsync the sbo tree from slackbuilds.org to $config{SBO_HOME}
sub rsync_sbo_tree() {
my $slk_version = get_slack_version;
my @arg = ('rsync', '-a', '--exclude=*.tar.gz', '--exclude=*.tar.gz.asc');
push @arg, '--delete', "rsync://slackbuilds.org/slackbuilds/$slk_version/*";
my $out = system @arg, $config{SBO_HOME};
my $wanted = sub {
$File::Find::name ? chown 0, 0, $File::Find::name
: chown 0, 0, $File::Find::dir;
};
find($wanted, $config{SBO_HOME});
say 'Finished.' and return $out;
}
# wrappers for differing checks and output
sub fetch_tree() {
check_home;
say 'Pulling SlackBuilds tree...';
rsync_sbo_tree, return 1;
}
sub update_tree() {
fetch_tree, return unless chk_slackbuilds_txt;
say 'Updating SlackBuilds tree...';
rsync_sbo_tree, return 1;
}
# if the SLACKBUILDS.TXT is not in $config{SBO_HOME}, we assume the tree has
# not been populated there; prompt the user to automagickally pull the tree.
sub slackbuilds_or_fetch() {
unless (chk_slackbuilds_txt) {
say 'It looks like you haven\'t run "sbosnap fetch" yet.';
print 'Would you like me to do this now? [y] ';
<STDIN> =~ /^[Yy\n]/ ? fetch_tree :
die "Please run \"sbosnap fetch\"\n";
}
return 1;
}
# pull an array of hashes, each hash containing the name and version of a
# package currently installed. Gets filtered using STD, SBO or ALL.
sub get_installed_packages($) {
exists $_[0] or script_error 'get_installed_packages requires an argument.';
my $filter = shift;
my @installed;
my $regex = qr#/([^/]+)-([^-]+)-[^-]+-([^-]+)$#;
for my $path (<$pkg_db/*>) {
my ($name, $version, $build) = ($path =~ $regex)[0,1,2];
# valid types: STD, SBO
my $type = 'STD';
if ($build =~ m/_SBo*/) {
my $sbo = $name;
$sbo =~ s/-compat32//g if $name =~ /-compat32$/;
$type = 'SBO' if get_sbo_location($sbo);
}
if ($filter eq $type or $filter eq 'ALL') {
push @installed, {name => $name, version => $version};
}
}
return \@installed;
}
# pull an array of hashes, each hash containing the name and version of an sbo
# currently installed.
# sub get_installed_sbos() {
# my @installed;
# # $1 == name, $2 == version
# my $regex = qr#/([^/]+)-([^-]+)-[^-]+-[^-]+$#;
# for my $path (<$pkg_db/*_SBo>) {
# my ($name, $version) = ($path =~ $regex)[0,1];
# push @installed, {name => $name, version => $version};
# }
# return \@installed;
# }
# for a ref to an array of hashes of installed packages, return an array ref
# consisting of just their names
sub get_inst_names($) {
exists $_[0] or script_error 'get_inst_names requires an argument.';
my $inst = shift;
my @installed;
push @installed, $$_{name} for @$inst;
return \@installed;
}
# search the SLACKBUILDS.TXT for a given sbo's directory
sub get_sbo_location {
exists $_[0] or script_error 'get_sbo_location requires an argument.';
my @sbos = @_;
if (ref $sbos[0] eq 'ARRAY') {
my $tmp = $sbos[0];
@sbos = @$tmp;
}
state $store = {};
# if scalar context and we already have the location, return it now.
unless (wantarray) {
return $$store{$sbos[0]} if exists $$store{$sbos[0]};
}
my %locations;
my $fh = open_read $slackbuilds_txt;
FIRST: for my $sbo (@sbos) {
$locations{$sbo} = $$store{$sbo}, next FIRST if exists $$store{$sbo};
my $regex = qr#LOCATION:\s+\.(/[^/]+/\Q$sbo\E)$#;
while (my $line = <$fh>) {
if (my $loc = ($line =~ $regex)[0]) {
# save what we found for later requests
$$store{$sbo} = "$config{SBO_HOME}$loc";
return $$store{$sbo} unless wantarray;
$locations{$sbo} = $$store{$sbo};
}
}
seek $fh, 0, 0;
}
close $fh;
return keys %locations > 0 ? %locations : undef;
}
# pull the sbo name from a $location: $config{SBO_HOME}/system/wine, etc.
sub get_sbo_from_loc($) {
exists $_[0] or script_error 'get_sbo_from_loc requires an argument.';
return (shift =~ qr#/([^/]+)$#)[0];
}
# pull piece(s) of data, GET, from the $sbo.info file under LOCATION.
sub get_from_info {
my %args = (
LOCATION => '',
GET => '',
@_
);
unless ($args{LOCATION} && $args{GET}) {
script_error 'get_from_info requires LOCATION and GET.';
}
state $store = {PRGNAM => ['']};
my $sbo = get_sbo_from_loc $args{LOCATION};
return $$store{$args{GET}} if $$store{PRGNAM}[0] eq $sbo;
# if we're here, we haven't read in the .info file yet.
my $fh = open_read "$args{LOCATION}/$sbo.info";
# suck it all in, clean it all up, stuff it all in $store.
my $contents = do {local $/; <$fh>};
$contents =~ s/("|\\\n)//g;
$store = {$contents =~ /^(\w+)=(.*)$/mg};
# fill the hash with array refs - even for single values,
# since consistency here is a lot easier than sorting it out later
for my $key (keys %$store) {
if ($$store{$key} =~ /\s/) {
my @array = split ' ', $$store{$key};
$$store{$key} = \@array;
} else {
$$store{$key} = [$$store{$key}];
}
}
return exists $$store{$args{GET}} ? $$store{$args{GET}} : undef;
}
# find the version in the tree for a given sbo (provided a location)
sub get_sbo_version($) {
exists $_[0] or script_error 'get_sbo_version requires an argument.';
my $version = get_from_info(LOCATION => shift, GET => 'VERSION');
return $$version[0] ? $$version[0] : undef;
}
# for each installed sbo, find out whether or not the version in the tree is
# newer, and compile an array of hashes containing those which are
sub get_available_updates() {
my @updates;
my $pkg_list = get_installed_packages 'SBO';
FIRST: for my $key (keys @$pkg_list) {
my $location = get_sbo_location($$pkg_list[$key]{name});
# if we can't find a location, assume invalid and skip
next FIRST unless defined $location;
my $version = get_sbo_version $location;
if (versioncmp($version, $$pkg_list[$key]{version}) == 1) {
push @updates, {
name => $$pkg_list[$key]{name},
installed => $$pkg_list[$key]{version},
update => $version
};
}
}
return \@updates;
}
# get downloads and md5sums from an sbo's .info file, first
# checking for x86_64-specific info if we are told to
sub get_download_info {
my %args = (
LOCATION => 0,
X64 => 1,
@_
);
$args{LOCATION} or script_error 'get_download_info requires LOCATION.';
my ($get, $downs, $md5s, %return);
$get = ($args{X64} ? 'DOWNLOAD_x86_64' : 'DOWNLOAD');
$downs = get_from_info(LOCATION => $args{LOCATION}, GET => $get);
# did we get nothing back, or UNSUPPORTED/UNTESTED?
if ($args{X64}) {
if (! $$downs[0] || $$downs[0] =~ qr/^UN(SUPPOR|TES)TED$/) {
$args{X64} = 0;
$downs = get_from_info(LOCATION => $args{LOCATION},
GET => 'DOWNLOAD');
}
}
# if we still don't have any links, something is really wrong.
return unless $$downs[0];
# grab the md5s and build a hash
$get = $args{X64} ? 'MD5SUM_x86_64' : 'MD5SUM';
$md5s = get_from_info(LOCATION => $args{LOCATION}, GET => $get);
return unless $$md5s[0];
$return{$$downs[$_]} = $$md5s[$_] for (keys @$downs);
return %return;
}
sub get_arch() {
chomp(my $arch = `uname -m`);
return $arch;
}
# TODO: should probably combine this with get_download_info
sub get_sbo_downloads {
my %args = (
LOCATION => '',
32 => 0,
@_
);
$args{LOCATION} or script_error 'get_sbo_downloads requires LOCATION.';
my $location = $args{LOCATION};
-d $location or script_error 'get_sbo_downloads given a non-directory.';
my $arch = get_arch;
my %dl_info;
if ($arch eq 'x86_64') {
%dl_info = get_download_info(LOCATION => $location) unless $args{32};
}
unless (keys %dl_info > 0) {
%dl_info = get_download_info(LOCATION => $location, X64 => 0);
}
return %dl_info;
}
# given a link, grab the filename from it and prepend $distfiles
sub get_filename_from_link($) {
exists $_[0] or script_error 'get_filename_from_link requires an argument';
my $fn = shift;
my $regex = qr#/([^/]+)$#;
my $filename = $fn =~ $regex ? $distfiles .'/'. ($fn =~ $regex)[0] : undef;
$filename =~ s/%2B/+/g if $filename;
return $filename;
}
# for a given file, compute its md5sum
sub compute_md5sum($) {
-f $_[0] or script_error 'compute_md5sum requires a file argument.';
my $fh = open_read shift;
my $md5 = Digest::MD5->new;
$md5->addfile($fh);
my $md5sum = $md5->hexdigest;
close $fh;
return $md5sum;
}
# for a given distfile, see whether or not it exists, and if so, if its md5sum
# matches the sbo's .info file
sub verify_distfile {
exists $_[1] or script_error 'verify_distfile requires two arguments.';
my ($link, $info_md5) = @_;
my $filename = get_filename_from_link $link;
return unless -f $filename;
my $md5sum = compute_md5sum $filename;
return $info_md5 eq $md5sum ? 1 : 0;
}
# for a given distfile, attempt to retrieve it and, if successful, check its
# md5sum against that in the sbo's .info file
sub get_distfile {
exists $_[1] or script_error 'get_distfile requires an argument';
my ($link, $info_md5) = @_;
my $filename = get_filename_from_link $link;
mkdir $distfiles unless -d $distfiles;
chdir $distfiles;
system("wget --no-check-certificate $link") == 0 or
die "Unable to wget $link\n";
# can't do anything if the link in the .info doesn't lead to a good d/l
verify_distfile(@_) ? return 1 : die "md5sum failure for $filename.\n";
return 1;
}
# for a given distfile, figure out what the full path to its symlink will be
sub get_symlink_from_filename {
exists $_[1] or script_error
'get_symlink_from_filename requires two arguments';
-f $_[0] or script_error
'get_symlink_from_filename first argument is not a file';
my ($filename, $location) = @_;
return "$location/". ($filename =~ qr#/([^/]+)$#)[0];
}
# determine whether or not a given sbo is 32-bit only
sub check_x32($) {
exists $_[0] or script_error 'check_x32 requires an argument.';
my $dl = get_from_info(LOCATION => shift, GET => 'DOWNLOAD_x86_64');
return $$dl[0] =~ /UN(SUPPOR|TES)TED/ ? 1 : undef;
}
# can't do 32-bit on x86_64 without this file, so we'll use it as the test to
# to determine whether or not an x86_64 system is setup for multilib
sub check_multilib() {
return 1 if -f '/etc/profile.d/32dev.sh';
return;
}
# given a list of downloads, return just the filenames
sub get_dl_fns($) {
my $fns = shift;
my $return;
push @$return, ($_ =~ qr|/([^/]+)$|)[0] for @$fns;
return $return;
}
# given a line that looks like it's decompressing something, try to return a
# valid filename regex
sub get_dc_regex {
my $line = shift;
# get rid of initial 'tar x'whatever stuff
$line =~ s/^.*(?<![a-z])(tar|p7zip|unzip|ar|rpm2cpio|sh)\s+[^\s]+\s+//;
# need to know preceeding character - should be safe to assume it's either
# a slash or a space
my $initial = $line =~ qr|/| ? '/' : ' ';
# get rid of initial path info
$line =~ s|^\$[^/]+/||;
# convert any instances of command substitution to [^-]+
$line =~ s/\$\([^)]+\)/[^-]+/g;
# convert any bash variables to [^-]+
$line =~ s/\$({|)[A-Za-z0-9_]+(}|)/[^-]+/g;
# get rid of anything excess at the end
$line =~ s/\s+.*$//;
# fix .?z* at the end
$line =~ s/\.\?z\*/\.[a-z]z.*/;
# return what's left as a regex
my $regex = qr/$initial$line/;
return $regex, $initial;
}
# make a backup of the existent SlackBuild, and rewrite the original as needed
sub rewrite_slackbuild {
my %args = (
SBO => '',
SLACKBUILD => '',
CHANGES => {},
C32 => 0,
@_
);
$args{SLACKBUILD} or script_error 'rewrite_slackbuild requires SLACKBUILD.';
my $slackbuild = $args{SLACKBUILD};
my $changes = $args{CHANGES};
copy($slackbuild, "$slackbuild.orig") or
die "Unable to backup $slackbuild to $slackbuild.orig\n";
my $libdir_regex = qr/^\s*LIBDIRSUFFIX="64"\s*$/;
my $arch_regex = qr/\$VERSION-\$ARCH-\$BUILD/;
my $dc_regex = qr/(?<![a-z])(tar|p7zip|unzip|ar|rpm2cpio|sh)\s+/;
# tie the slackbuild, because this is the easiest way to handle this.
tie my @sb_file, 'Tie::File', $slackbuild;
# if we're dealing with a compat32, we need to change the tar line(s) so
# that the 32-bit source is untarred
if ($args{C32}) {
my $location = get_sbo_location($args{SBO});
my %downloads = get_sbo_downloads(
LOCATION => $location,
32 => 1,
);
my $fns = get_dl_fns [keys %downloads];
for my $line (@sb_file) {
if ($line =~ $dc_regex) {
my ($regex, $initial) = get_dc_regex $line;
for my $fn (@$fns) {
$fn = "$initial$fn";
$line =~ s/$regex/$fn/ if $fn =~ $regex;
}
}
}
}
for my $line (@sb_file) {
# then check for and apply any other %$changes
if (exists $$changes{libdirsuffix}) {
$line =~ s/64/$$changes{libdirsuffix}/ if $line =~ $libdir_regex;
}
if (exists $$changes{arch_out}) {
$line =~ s/\$ARCH/$$changes{arch_out}/ if $line =~ $arch_regex;
}
}
untie @sb_file;
return 1;
}
# move a backed-up .SlackBuild file back into place
sub revert_slackbuild($) {
exists $_[0] or script_error 'revert_slackbuild requires an argument';
my $slackbuild = shift;
if (-f "$slackbuild.orig") {
unlink $slackbuild if -f $slackbuild;
rename "$slackbuild.orig", $slackbuild;
}
return 1;
}
# for each $download, see if we have it, and if the copy we have is good,
# otherwise download a new copy
sub check_distfiles {
exists $_[0] or script_error 'check_distfiles requires an argument.';
my %dists = @_;
while (my ($link, $md5) = each %dists) {
get_distfile($link, $md5) unless verify_distfile($link, $md5);
}
return 1;
}
# given a location and a list of download links, assemble a list of symlinks,
# and create them.
sub create_symlinks {
exists $_[1] or script_error 'create_symlinks requires two arguments.';
my ($location, %downloads) = @_;
my @symlinks;
for my $link (keys %downloads) {
my $filename = get_filename_from_link $link;
my $symlink = get_symlink_from_filename($filename, $location);
push @symlinks, $symlink;
symlink $filename, $symlink;
}
return @symlinks;
}
# pull the created package name from the temp file we tee'd to
sub get_pkg_name($) {
my $fh = shift;
seek $fh, 0, 0;
my $regex = qr/^Slackware\s+package\s+([^\s]+)\s+created\.$/;
my $out;
FIRST: while (my $line = <$fh>) {
last FIRST if $out = ($line =~ $regex)[0];
}
return $out;
}
sub get_src_dir($) {
exists $_[0] or script_error 'get_src_dir requires an argument';
my $fh = shift;
seek $fh, 0, 0;
my @src_dirs;
opendir(my $tsbo_dh, '/tmp/SBo');
FIRST: while (my $ls = readdir $tsbo_dh) {
next FIRST if $ls =~ /^\.[\.]{0,1}$/;
next FIRST if $ls =~ /^package-/;
my $found = 0;
SECOND: while (my $line = <$fh>) {
if ($line =~ /$ls/) {
$found++;
last SECOND;
}
}
push @src_dirs, $ls unless $found;
}
close $tsbo_dh;
close $fh;
return \@src_dirs;
}
# return a filename from a temp fh for use externally
sub get_tmp_extfn($) {
exists $_[0] or script_error 'get_tmp_extfn requires an argument.';
my $fh = shift;
fcntl($fh, F_SETFD, 0) or die "Can't unset exec-on-close bit\n";
return '/dev/fd/'. fileno $fh;
}
# prep and run .SlackBuild
sub perform_sbo {
my %args = (
OPTS => 0,
JOBS => 0,
LOCATION => '',
ARCH => '',
C32 => 0,
X32 => 0,
@_
);
unless ($args{LOCATION} && $args{ARCH}) {
script_error 'perform_sbo requires LOCATION and ARCH.';
}
my $location = $args{LOCATION};
my $sbo = get_sbo_from_loc $location;
my ($cmd, %changes);
# set any changes we need to make to the .SlackBuild, setup the command
$cmd = '( ';
$args{JOBS} = 0 if $args{JOBS} eq 'FALSE';
if ($args{ARCH} eq 'x86_64' and ($args{C32} || $args{X32})) {
if ($args{C32}) {
$changes{libdirsuffix} = '';
} elsif ($args{X32}) {
$changes{arch_out} = 'i486';
}
$cmd .= '. /etc/profile.d/32dev.sh &&';
}
$cmd .= " $args{OPTS}" if $args{OPTS};
$cmd .= " MAKEOPTS=\"-j$args{JOBS}\"" if $args{JOBS};
# we need to get a listing of /tmp/SBo before we run the SlackBuild so that
# we can compare to a listing taken afterward.
my $src_ls_fh = tempfile(DIR => $tempdir);
opendir(my $tsbo_dh, '/tmp/SBo');
FIRST: while (readdir $tsbo_dh) {
next FIRST if /^\.[\.]{0,1}$/;
say {$src_ls_fh} $_;
}
# get a tempfile to store the exit status of the slackbuild
my $exit_temp = tempfile(DIR => $tempdir);
my $exit_fn = get_tmp_extfn $exit_temp;
$cmd .= " /bin/sh $location/$sbo.SlackBuild; echo \$? > $exit_fn )";
my $tempfh = tempfile(DIR => $tempdir);
my $fn = get_tmp_extfn $tempfh;
$cmd .= " | tee -a $fn";
rewrite_slackbuild(
SBO => $sbo,
SLACKBUILD => "$location/$sbo.SlackBuild",
CHANGES => \%changes,
C32 => $args{C32},
);
chdir $location, system $cmd;
seek $exit_temp, 0, 0;
my $out = do {local $/; <$exit_temp>};
revert_slackbuild "$location/$sbo.SlackBuild";
die "$sbo.SlackBuild returned non-zero exit status\n" unless $out == 0;
my $pkg = get_pkg_name $tempfh;
my $src = get_src_dir $src_ls_fh;
return $pkg, $src;
}
# run convertpkg on a package to turn it into a -compat32 thing
sub do_convertpkg($) {
exists $_[0] or script_error 'do_convertpkg requires an argument.';
my $pkg = shift;
my $tempfh = tempfile(DIR => $tempdir);
my $fn = get_tmp_extfn $tempfh;
my $cmd = "/usr/sbin/convertpkg-compat32 -i $pkg -d /tmp | tee $fn";
system($cmd) == 0 or
die "convertpkg-compt32 returned non-zero exit status\n";
unlink $pkg;
return get_pkg_name $tempfh;
}
# "public interface", sort of thing.
sub do_slackbuild {
my %args = (
OPTS => 0,
JOBS => 0,
LOCATION => '',
COMPAT32 => 0,
@_
);
$args{LOCATION} or script_error 'do_slackbuild requires LOCATION.';
my $location = $args{LOCATION};
my $sbo = get_sbo_from_loc $location;
my $arch = get_arch;
my $multilib = check_multilib;
my $version = get_sbo_version $location;
my $x32;
# ensure x32 stuff is set correctly, or that we're setup for it
if ($args{COMPAT32}) {
die "compat32 requires multilib.\n" unless $multilib;
die "compat32 requires /usr/sbin/convertpkg-compat32.\n"
unless -f '/usr/sbin/convertpkg-compat32';
} else {
if ($arch eq 'x86_64') {
$x32 = check_x32 $args{LOCATION};
if ($x32 && ! $multilib) {
die "$sbo is 32-bit which requires multilib on x86_64.\n";
}
}
}
# get a hash of downloads and md5sums, ensure we have 'em, symlink 'em
my %downloads = get_sbo_downloads(
LOCATION => $location,
32 => $args{COMPAT32}
);
check_distfiles %downloads;
my @symlinks = create_symlinks($args{LOCATION}, %downloads);
# setup and run the .SlackBuild itself
my ($pkg, $src) = perform_sbo(
OPTS => $args{OPTS},
JOBS => $args{JOBS},
LOCATION => $location,
ARCH => $arch,
C32 => $args{COMPAT32},
X32 => $x32,
);
$pkg = do_convertpkg $pkg if $args{COMPAT32};
unlink $_ for @symlinks;
return $version, $pkg, $src;
}
# remove work directories (source and packaging dirs under /tmp/SBo)
sub make_clean {
my %args = (
SBO => '',
SRC => '',
VERSION => '',
@_
);
unless ($args{SBO} && $args{SRC} && $args{VERSION}) {
script_error 'make_clean requires three arguments.';
}
my $src = $args{SRC};
say "Cleaning for $args{SBO}-$args{VERSION}...";
my $tmpsbo = '/tmp/SBo';
for my $dir (@$src) {
remove_tree("$tmpsbo/$dir") if -d "$tmpsbo/$dir";
}
remove_tree("$tmpsbo/package-$args{SBO}") if
-d "$tmpsbo/package-$args{SBO}";
return 1;
}
# remove distfiles
sub make_distclean {
my %args = (
SRC => '',
VERSION => '',
LOCATION => '',
@_
);
unless ($args{SRC} && $args{VERSION} && $args{LOCATION}) {
script_error 'make_distclean requires four arguments.';
}
my $sbo = get_sbo_from_loc $args{LOCATION};
make_clean(SBO => $sbo, SRC => $args{SRC}, VERSION => $args{VERSION});
say "Distcleaning for $sbo-$args{VERSION}...";
# remove any distfiles for this particular SBo.
my %downloads = get_sbo_downloads(LOCATION => $args{LOCATION});
for my $key (keys %downloads) {
my $filename = get_filename_from_link $key;
unlink $filename if -f $filename;
}
return 1;
}
# run upgradepkg for a created package
sub do_upgradepkg($) {
exists $_[0] or script_error 'do_upgradepkg requires an argument.';
system('/sbin/upgradepkg', '--reinstall', '--install-new', shift);
return 1;
}
# wrapper to pull the list of requirements for a given sbo
sub get_requires($) {
my $location = get_sbo_location(shift);
return unless $location;
my $info = get_from_info(LOCATION => $location, GET => 'REQUIRES');
return $$info[0] ne '' ? $info : undef;
}
# avoid being called to early to check prototype when add_to_queue calls itself
sub add_to_queue($);
# used by get_build_queue.
sub add_to_queue($) {
my $args = shift;
my $sbo = \${$args}{NAME};
return unless $$sbo;
push @$args{QUEUE}, $$sbo;
my $requires = get_requires $$sbo;
FIRST: for my $req (@$requires) {
next FIRST if $req eq $$sbo;
if ($req eq "%README%") {
${$args}{WARNINGS}{$$sbo}="%README%";
} else {
$$sbo = $req;
add_to_queue($args);
}
}
}
# recursively add a sbo's requirements to the build queue.
sub get_build_queue {
exists $_[1] or script_error 'get_build_queue requires two arguments.';
my ($sbos, $warnings) = @_;
my $temp_queue = [];
for my $sbo (@$sbos) {
my %args = (
QUEUE => $temp_queue,
NAME => $sbo,
WARNINGS => $warnings
);
add_to_queue(\%args);
}
# Remove duplicate entries (leaving first occurrence)
my (%seen, @build_queue);
FIRST: for my $sb (@$temp_queue) {
next FIRST if $seen{$sb}++;
push @build_queue, $sb;
}
return \@build_queue;
}
sub merge_queues {
# Usage: merge_queues(\@queue_a, \@queue_b);
# Results in queue_b being merged into queue_a (without duplicates)
exists $_[1] or script_error 'merge_queues requires two arguments.';
my $queue_a = $_[0];
my $queue_b = $_[1];
for my $item (reverse @$queue_b) {
push @$queue_a, $item unless $item ~~ @$queue_a;
}
return $queue_a;
}
sub get_readme_contents($) {
exists $_[0] or script_error 'get_readme_contents requires an argument.';
my $fh = open_read(shift .'/README');
my $readme = do {local $/; <$fh>};
close $fh;
return $readme;
}
# return a list of perl modules installed via the CPAN
sub get_installed_cpans() {
my @locals;
for my $dir (@INC) {
push @locals, "$dir/perllocal.pod" if -f "$dir/perllocal.pod";
}
my @contents;
for my $file (@locals) {
my $fh = open_read $file;
# push @contents, grep {/Module|VERSION/} <$fh>;
push @contents, grep {/Module/} <$fh>;
close $fh;
}
my $mod_regex = qr/C<Module>\s+L<([^\|]+)/;
# my $ver_regex = qr/C<VERSION:\s+([^>]+)>/;
my (@mods, @vers);
for my $line (@contents) {
push @mods, ($line =~ $mod_regex)[0];
# push @vers, ($line =~ $ver_regex)[0];
}
return \@mods;
# my %cpans;
# $cpans{$mods[$_]} = $vers[$_] for keys @mods;
# return \%cpans;
}
|