forked from trizen/trizen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trizen
executable file
·2603 lines (2054 loc) · 80.3 KB
/
trizen
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) 2010-2018 Trizen <echo dHJpemVuQHByb3Rvbm1haWwuY29tCg== | base64 -d>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Created: 07 July 2010
# First rewrite: 16 February 2011
# Second rewrite: 24 March 2012
# Latest edit: 04 May 2018
# https://github.com/trizen/trizen
# Contributors:
# notramo - https://github.com/notramo
# ccl2of4 - https://github.com/ccl2of4
# nl6720 - https://github.com/nl6720
# JJK96 - https://github.com/JJK96
# nicman23 - https://github.com/nicman23
# DragoonAethis - https://github.com/DragoonAethis
# Full list of contributors:
# https://github.com/trizen/trizen/graphs/contributors
# Sincere thanks to everyone who helped and contributed in improving
# this program through pull-requests, feature-requests and bug-reports.
eval 'exec perl -S $0 ${1+"$@"}'
if 0; # not running under some shell
use 5.020;
use strict;
use warnings;
use experimental qw(signatures);
use List::Util qw(first any max);
use Encode qw(decode_utf8);
use Getopt::Std qw(getopts);
use URI::Escape qw(uri_escape_utf8);
use Term::ANSIColor qw(:constants);
use File::Path qw(make_path rmtree);
use File::Spec::Functions qw(catdir catfile tmpdir curdir rel2abs);
my $version = '1.49';
my $execname = 'trizen';
my $AUR_V = '5'; # current version of AurJson
my $aur_base_url = 'https://aur.archlinux.org';
my $aur_rpc_base_url = "$aur_base_url/rpc.php";
my $aur_package_id_url = "$aur_base_url/packages.php?ID=%s";
# Home directory
my $home_dir = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid $<)[7] || `echo -n ~`;
# Configuration directory
my $config_dir = catdir($ENV{XDG_CONFIG_HOME} || catdir($home_dir, '.config'), $execname);
# Configuration file
my $config_file = catfile($config_dir, "$execname.conf");
my $user = (getpwuid $<)[0] || substr(`/usr/bin/whoami`, 0, -1);
my $arch = substr(`/usr/bin/uname -m`, 0, -1);
my %ignored_pkgs;
if (-x '/usr/bin/pacconf') {
%ignored_pkgs = map { ($_ => 1) } split(' ', `/usr/bin/pacconf IgnorePkg 2> /dev/null`);
}
# UTF-8 output
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
# Prefer HTTPS v3
local $ENV{HTTPS_VERSION} = 3;
#----------------------- COLORS -----------------------#
my %c = (
byellow => (BOLD YELLOW),
bpurple => (BOLD MAGENTA),
bblue => (BOLD BLUE),
bgreen => (BOLD GREEN),
bold => (BOLD),
bred => (BOLD RED),
bcyan => (BOLD CYAN),
reset => (RESET),
);
my $stdout_on_tty = -t STDOUT;
my $stdin_on_tty = -t STDIN;
if (not $stdin_on_tty and any { $_ eq '-' } @ARGV) {
push @ARGV, split(' ', join(' ', <STDIN>));
close STDIN;
}
@ARGV = map { decode_utf8($_) } @ARGV;
# Disable colors
if (not $stdout_on_tty or (any { $_ eq '--nocolors' } @ARGV)) {
%c = map { ($_ => "") } keys %c;
}
if (not -d $config_dir) {
make_path($config_dir)
or note(":: Unable to create directory <<$config_dir>>: {!$!!}");
}
#----------------------- GLOBAL VARIABLES -----------------------#
my %CONFIG = (
show_comments => 0,
quiet => 0,
debug => 0,
nocolors => 0,
movepkg => 0,
noedit => 0,
noinstall => 0,
nopull => 0,
noinfo => 0,
skipinteg => 0,
force => 0,
ask_for_retry => 1,
lwp_show_progress => 0,
lwp_env_proxy => 1,
lwp_timeout => 60,
ssl_verify_hostname => 1,
packages_in_stats => 5,
git_clone_depth => 0,
split_packages => 1,
recompute_deps => 1,
pager_mode => 0,
use_sudo => ((-x '/usr/bin/sudo') ? 1 : 0),
su_command => '/usr/bin/su -c',
sudo_command => '/usr/bin/sudo',
sudo_autorepeat => 0,
sudo_autorepeat_at_runtime => 0,
sudo_autorepeat_interval => 180,
makepkg_command => '/usr/bin/makepkg -scf',
movepkg_dir => '/var/cache/pacman/pkg',
pacman_local_dir => '/var/lib/pacman/local',
pacman_command => '/usr/bin/pacman',
show_diff_only => 0,
show_build_files_content => 1,
aur_results_sort_by => 'name',
aur_results_sort_order => 'ascending',
aur_results_votes => 1,
aur_results_popularity => 1,
aur_results_last_modified => 1,
aur_results_show_installed => 1,
flip_results => 0,
flip_indices => 0,
show_ood => 0,
show_inexistent => 1,
show_unmaintained => 1,
install_built_with_noconfirm => 0,
);
$CONFIG{clone_dir} = catdir(tmpdir(), "$execname-$user");
my %lconfig = (
%CONFIG,
devel => 0,
needed => 0,
noconfirm => 0,
aur => 0,
search => 0, # -s
info => 0, # -i
clean => 0, # -c
local => 0, # -l
pkgbuild => 0,
sysupgrade => 0, # -u
comments => 0,
database => 0, # -D
files => 0, # -F
query => 0, # -Q
remove => 0, # -R
sync => 0, # -S
deptest => 0, # -T
upgrade => 0, # -U
get => 0,
regex => 0, # -x
refresh => 0, # -y
upgrades => 0, # -u
check => 0, # -k
with_deps => 0, # -d
maintainer => 0, # -m
nobuild => 0,
regular => 0, # use only the regular repositories
asdep => 0, # alias for `--asdeps`
asdeps => 0,
asexplicit => 0,
help => 0,
update_config => 0,
ignore => '', # packages to ignore during -Su
as_root => ($user eq 'root' ? 1 : 0),
stats => \&show_stats,
version => \&version,
);
$lconfig{editor} = $ENV{VISUAL} || $ENV{EDITOR} || 'nano';
$lconfig{pager_command} = $ENV{PAGER} || 'less';
our $CONFIG;
sub dump_configuration ($config, $configuration_file) {
my $config_header = <<"EOH";
#!/usr/bin/perl
# $execname configuration file
# Updated on ${\(scalar localtime)}
EOH
my $config_options_help = <<"EOT";
aur_results_last_modified => bool Show the date when the packages were last updated in AUR results.
aur_results_popularity => bool Show the popularity score in AUR results.
aur_results_show_installed => bool Show when a package is installed in AUR results.
aur_results_sort_by => str Sort the AUR results by "name", "votes", "popularity" or "date".
aur_results_sort_order => str Sort the AUR results in "ascending" or "descending" order.
aur_results_votes => bool Show the number of votes in AUR results.
ask_for_retry => bool When `makepkg` fails to build a package, offer the option for trying again.
clone_dir => str Absolute path to the directory where to clone and build packages.
debug => bool Verbose mode.
flip_indices => bool In search+install mode, show the indices of packages in reverse order.
flip_results => bool Show the search results in reverse order.
force => bool Pass the `--force` flag to `pacman`.
git_clone_depth => int Pass the `--depth int` flag to `git clone`. (0 means no limit)
install_built_with_noconfirm => bool Install built packages with `--noconfirm`.
lwp_env_proxy => bool Use proxy settings defined in `env` (if any).
lwp_show_progress => bool Show the HTTPS requests made by LWP::UserAgent to the AUR servers.
lwp_timeout => int Seconds after which an HTTPS connection is aborted.
makepkg_command => str The `makepkg` command that is used internally in building a package.
movepkg => bool Move built packages in the directory `movepkg_dir`.
movepkg_dir => str Absolute path to the directory where to move built packages (with `movepkg`).
nocolors => bool Disable output colors for `$execname`.
noedit => bool Do not prompt to edit files when installing an AUR package.
noinfo => bool Do not display package information when installing an AUR package.
noinstall => bool Do not install built packages -- builds only.
nopull => bool Do not `git pull` new changes from the AUR git server.
packages_in_stats => int The number of packages to display in `--stats`
pacman_command => str The `pacman` command that is used internally for pacman operations.
pacman_local_dir => str Absolute path to the pacman's local directory.
pager_mode => bool Show the build files in pager mode using pager.
quiet => bool Do not display any warnings.
recompute_deps => bool Recompute the dependencies of a package after its build files are inspected.
show_build_files_content => bool Show the content of the build files of a package before building it.
show_diff_only => bool When the build files of a package already exist locally, show the diff only.
show_comments => bool Show AUR comments for a package before building it.
show_inexistent => bool Warn about packages that do not exist in AUR, during -Su.
show_ood => bool Warn about out-of-date marked packages, during -Su.
show_unmaintained => bool Warn about unmaintained packages, during -Su.
skipinteg => bool Pass the `--skipinteg` argument to `makepkg`.
split_packages => bool Ask about installing the other parts of a split package.
ssl_verify_hostname => bool Ensure LWP::UserAgent connects to servers that have a valid certificate.
su_command => str Command used when special permissions are required and `use_sudo` is set to 0.
sudo_autorepeat => bool Automatically repeat `sudo -v` in the background after a `sudo` command was first executed.
sudo_autorepeat_at_runtime => bool Execute `sudo -v` when `$execname` is first executed and apply the behavior of `sudo_autorepeat`.
sudo_autorepeat_interval => int Interval, in seconds, after which `sudo -v` is executed in background (with `sudo_autorepeat`).
sudo_command => str Command used when special permissions are required and `use_sudo` is set to 1.
use_sudo => bool Use the `sudo` command when special permissions are required.
EOT
my %table;
foreach my $line (split(/\n/, $config_options_help)) {
if ($line =~ /^(\w+)\s*=>\s*(\w+)\s+(.+)/) {
my ($name, $type, $desc) = ($1, $2, $3);
$table{$name} = {
type => $type,
desc => $desc,
};
}
}
open my $config_fh, '>:utf8', $configuration_file or return;
require Data::Dump;
my $dumped_config = q{our $CONFIG = } . Data::Dump::dump($config) . "\n";
if ($home_dir eq $ENV{HOME}) {
$dumped_config =~ s/\Q$home_dir\E/\$ENV{HOME}/g;
}
my $names_re = join('|', keys %table);
my $max_width = max(map { length($_) } split(/\n/, $dumped_config));
# Add description after each configuration option
$dumped_config =~ s{
^\s*($names_re)(\s*=>\s*.*)
}{
sprintf(" %s %*s # %s", $1.$2, $max_width - length($1.$2), ' ',
sprintf('%4s -- %s', $table{$1}{type}, $table{$1}{desc}))
}gxme;
print $config_fh $config_header, $dumped_config;
close $config_fh;
}
if (-e $config_file and (-s _) > 1) {
require $config_file; # Load the configuration file
if (ref($CONFIG) eq 'HASH') {
my @valid_keys = grep { exists $CONFIG{$_} } keys(%$CONFIG);
$CONFIG->{clone_dir} //= $CONFIG{clone_dir};
if (join(' ', sort keys %CONFIG) ne join(' ', sort keys %$CONFIG)) {
$lconfig{update_config} = 1;
}
@CONFIG{@valid_keys} = @{$CONFIG}{@valid_keys};
@lconfig{@valid_keys} = @{$CONFIG}{@valid_keys};
}
else {
note(":: Invalid config file: {!" . ($! || '$CONFIG must be a HASH ref!') . '!}');
$lconfig{update_config} = 1;
}
}
else {
msg(":: Saving configuration file...") if $lconfig{debug};
dump_configuration(\%CONFIG, $config_file)
or note(":: Cannot open file <<$config_file>> for write: {!$!!}");
}
if (not -d $lconfig{clone_dir}) {
make_path($lconfig{clone_dir})
or error(":: Unable to create clone directory <<$lconfig{clone_dir}>>: {!$!!}");
}
my $short_arguments = 'TDFCUNQRSGabcdefghiklmnopqrstuvwxy';
my @package_suffices = qw(
.tar.7z
.tar.Z
.tar.bz2
.tar.gz
.tar.lrz
.tar.lz
.tar.lzma
.tar.lzo
.tar.xz
.tgz
.xz
.7z
.bz2
.gz
.lzma
.lzo
.tar
.zip
);
my %already_built; # will keep track of built packages
my %already_installed; # will keep track of installed packages
my %seen_package; # will keep track of seen packages
my $pkg_suffices = join('|', map { quotemeta } @package_suffices);
my $pkg_suffix_re = qr/-[^-]+-\d+-\w+\.pkg(?:$pkg_suffices)\z/o;
my $sudo_autorepeat_pid;
sub exit_code ($code = $?) {
if ($code >= 256) {
$code >>= 8;
}
return $code;
}
# Main quit
sub main_quit ($code = undef) {
if ($lconfig{update_config}) {
dump_configuration(\%CONFIG, $config_file);
}
kill('TERM', $sudo_autorepeat_pid) if $sudo_autorepeat_pid;
exit($code // exit_code());
}
#----------------------- USAGE -----------------------#
sub help {
print <<"HELP";
========================== $c{bgreen}Trizen AUR Package Manager$c{reset} ==========================
$c{bold}usage:$c{reset} $execname [options] [pkgname] [pkgname] [...]
$c{bgreen}Main options:$c{reset}
$c{bold}-S$c{reset}, $c{bold}--sync$c{reset} : install packages (see: $execname -Sh)
$c{bold}-C$c{reset}, $c{bold}--comments$c{reset} : display AUR comments for a package
$c{bold}-G$c{reset}, $c{bold}--get$c{reset} : clones a package in the current directory
$c{bold}-R$c{reset}, $c{bold}--remove$c{reset} : remove packages from the system (see: pacman -Rh)
$c{bold}-Q$c{reset}, $c{bold}--query$c{reset} : query the package database (see: pacman -Qh)
$c{bold}-F$c{reset}, $c{bold}--files$c{reset} : query the files database (see: pacman -Fh)
$c{bold}-D$c{reset}, $c{bold}--database$c{reset} : operate on the package database (see: pacman -Dh)
$c{bold}-T$c{reset}, $c{bold}--deptest$c{reset} : check dependencies (see: pacman -Th)
$c{bold}-U$c{reset}, $c{bold}--upgrade$c{reset} : install built packages from $lconfig{clone_dir} or `pwd`
$c{bgreen}Other options:$c{reset}
$c{bold}-q$c{reset}, $c{bold}--quiet$c{reset} : do not display any warnings
$c{bold}-r$c{reset}, $c{bold}--regular$c{reset} : use only the regular repositories
$c{bold}--stats$c{reset} : show stats about the installed packages
$c{bold}--nocolors$c{reset} : disable text colors
$c{bold}--debug$c{reset} : activate the debug/verbose mode
$c{bold}--help$c{reset} : print this message and exit
$c{bold}--version$c{reset} : print version and exit
$c{bgreen}See also:$c{reset}
$c{bold}$execname -Sh$c{reset}
$c{bold}$execname -Gh$c{reset}
$c{bred}::$c{reset} Each configuration key is a valid option when preceded with '--'
$c{bred}::$c{reset} Configuration file: $c{bold}$config_file$c{reset}
HELP
main_quit();
}
sub sync_help {
print <<"SYNC_HELP";
$c{bold}usage:$c{reset} $execname {-S --sync} [options] [package(s)]
$c{bgreen}Main options:$c{reset}
$c{bold}-s$c{reset}, $c{bold}--search$c{reset} : search for packages
$c{bold}-i$c{reset}, $c{bold}--info$c{reset} : show info for packages
$c{bold}-m$c{reset}, $c{bold}--maintainer$c{reset} : show packages maintained by <username>
$c{bold}-p$c{reset}, $c{bold}--pkgbuild$c{reset} : show PKGBUILD only
$c{bold}-l$c{reset}, $c{bold}--local$c{reset} : build and install packages from `pwd`
$c{bold}-u$c{reset}, $c{bold}--sysupgrade$c{reset} : upgrade installed packages
$c{bold}-y$c{reset}, $c{bold}--refresh$c{reset} : refresh package databases (with: -u)
$c{bold}-c$c{reset}, $c{bold}--clean$c{reset} : clean the cache directory of `$execname` and `pacman`
$c{bold}-a$c{reset}, $c{bold}--aur$c{reset} : only AUR operations (with: -c, -u, -s, -i)
$c{bgreen}Other options:$c{reset}
$c{bold}--devel$c{reset} : update VCS packages during -Su
$c{bold}--show-ood$c{reset} : show out-of-date flagged packages during -Su
$c{bold}--noinfo$c{reset} : do not display package info after cloning
$c{bold}--nopull$c{reset} : do not `git pull` new changes
$c{bold}--noedit$c{reset} : do not prompt to edit files
$c{bold}--nobuild$c{reset} : do not build packages (implies --noedit)
$c{bold}--noinstall$c{reset} : do not install packages after building
$c{bold}--needed$c{reset} : do not reinstall up-to-date packages
$c{bold}--asdeps$c{reset} : install packages as non-explicitly installed
$c{bold}--asexplicit$c{reset} : install packages as explicitly installed
$c{bold}--force$c{reset} : pass the `--force` argument to `pacman`
$c{bold}--skipinteg$c{reset} : pass the `--skipinteg` argument to `makepkg`
$c{bold}--noconfirm$c{reset} : do not ask for any confirmation
$c{bold}--movepkg$c{reset} : move built packages into pacman's cache directory
$c{bold}--movepkg-dir=s$c{reset} : move built packages in this directory (implies --movepkg)
$c{bold}--clone-dir=s$c{reset} : directory where to clone and build packages
$c{bold}--editor=s$c{reset} : editor command used to edit build files
$c{bold}--ignore=s$c{reset} : space-separated list of packages to ignore during -Su
$c{bgreen}Examples:$c{reset}
$c{bold}$execname -S <package>$c{reset} # install <package>
$c{bold}$execname -Ss <keyword>$c{reset} # search for <keyword>
$c{bold}$execname -Si <package>$c{reset} # show info about <package>
SYNC_HELP
main_quit();
}
sub get_help {
print <<"GET_HELP";
$c{bold}usage:$c{reset} $execname {-G --get} [options] [package(s)]
$c{bgreen}Main options:$c{reset}
$c{bold}-d$c{reset}, $c{bold}--with-deps$c{reset} : clones a package with all needed AUR dependencies
$c{bgreen}Examples:$c{reset}
$c{bold}$execname -G <package>$c{reset} # clones <package>
$c{bold}$execname -Gd <package>$c{reset} # clones <package> along with its AUR dependencies
GET_HELP
main_quit();
}
@ARGV or help();
sub version {
say "$execname $version";
main_quit();
}
#----------------------- PARSING ARGUMENTS -----------------------#
sub parse_long_arguments (@args) {
Getopt::Long::GetOptionsFromArray(
\@args,
\%lconfig,
map {
(defined($lconfig{$_}) and $lconfig{$_} =~ /^[01]\z/) ? $_
: ref($lconfig{$_}) ? $_
: "$_=s"
} keys %lconfig,
);
}
sub parse_short_arguments () {
getopts($short_arguments, \%lconfig);
}
my @keyword_arguments;
my @leftover_short_arguments;
my @leftover_long_arguments;
sub parse_arguments (@arguments) {
my @long_arguments;
foreach my $arg (@arguments) {
if ($arg =~ /^--(\w.*?)=(.+)/) {
my ($option, $value) = ($1, $2);
$option =~ tr/-/_/;
if (exists $lconfig{$option}) {
$lconfig{$option} = $value;
# --movepkg_dir implies --movepkg
if ($option eq 'movepkg_dir') {
$lconfig{movepkg} = 1;
}
next;
}
}
if ($arg =~ /^--(\w.*)/) {
my $option = ($1 =~ tr/-/_/r);
if (exists $lconfig{$option}) {
push @long_arguments, "--$option";
next;
}
}
next if $arg =~ /^--?\z/; # ignore '--' and '-'
if ($arg =~ /^--/) {
push @leftover_long_arguments, $arg;
}
elsif ($arg =~ /^-/) {
$arg =~ tr/r//d;
next if $arg eq '-';
push @leftover_short_arguments, $arg;
}
else {
push @keyword_arguments, $arg;
}
}
if (@long_arguments) {
require Getopt::Long;
Getopt::Long::Configure('no_ignore_case');
parse_long_arguments(@long_arguments);
}
my @short_arguments = grep { /^-[$short_arguments]/ } @ARGV;
if (@short_arguments) {
local @ARGV = @short_arguments;
parse_short_arguments();
}
}
parse_arguments(@ARGV);
if ($lconfig{as_root}) {
warn "\n\t\t$c{bred}!!! You are running $c{bgreen}${execname}$c{reset}$c{bred} as root !!!$c{reset}\n\n" if $lconfig{S};
}
if ($lconfig{ignore}) {
@ignored_pkgs{split(/[,\s]+/, $lconfig{ignore})} = ();
}
if ($lconfig{skipinteg}) {
$lconfig{makepkg_command} .= ' --skipinteg';
}
if ($lconfig{noconfirm}) {
$Term::UI::AUTOREPLY = 1;
$lconfig{makepkg_command} .= ' --noconfirm';
}
else {
$Term::UI::AUTOREPLY = 0;
}
# `--asdep` as alias for `--asdeps`
if ($lconfig{asdep}) {
$lconfig{asdeps} = delete $lconfig{asdep};
}
# `-q` as alias for `--quiet`
if ($lconfig{q}) {
$lconfig{quiet} = delete $lconfig{q};
}
# `-a` as alias for `--aur`
if ($lconfig{a}) {
$lconfig{aur} = delete $lconfig{a};
}
if ($lconfig{debug}) {
$lconfig{lwp_show_progress} = 1;
}
if ($lconfig{quiet}) {
$SIG{__WARN__} = sub { };
}
if ($lconfig{nocolors}) {
%c = map { $_ => q{} } keys %c;
}
if ($lconfig{movepkg}) {
$lconfig{movepkg_dir} = rel2abs($lconfig{movepkg_dir});
}
if ($lconfig{nobuild}) {
$lconfig{noedit} = 1;
$lconfig{recompute_deps} = 0;
}
$lconfig{clone_dir} = rel2abs($lconfig{clone_dir});
if (not -d $lconfig{clone_dir}) {
make_path($lconfig{clone_dir})
or error(":: Unable to create directory <<$lconfig{clone_dir}>>: {!$!!}");
}
if ( $lconfig{sudo_autorepeat_at_runtime}
and $lconfig{use_sudo}
and !$lconfig{as_root}) {
start_sudo_autorepeat();
}
#----------------------- WORK AREA -----------------------#
# Run-time loaded modules
require Term::UI;
require Term::ReadLine;
require LWP::UserAgent;
require HTTP::Message;
# Initializing module objects
my $term = Term::ReadLine->new("$execname $version");
my $lwp = LWP::UserAgent->new(
env_proxy => $lconfig{lwp_env_proxy},
show_progress => $lconfig{lwp_show_progress},
timeout => $lconfig{lwp_timeout},
ssl_opts => {verify_hostname => $lconfig{ssl_verify_hostname}, SSL_version => 'TLSv1_2'},
agent => "Mozilla/5.0 (CLI; U; gzip; en-US) $execname/$version",
);
sub format_line ($string, $color) {
if ($string =~ /^:: (.*)/) {
my $substr = $1;
my $new_str = $color . '::' . $c{reset} . ' ';
$substr =~ s/<<(.*?)>>/$c{bgreen}$c{bold}$1$c{reset}/gs;
$substr =~ s/\{!(.*?)!\}/$c{bred}$c{bold}$1$c{reset}/gs;
if ($substr =~ /^(.+?: )(.+)/) {
$substr = $1 . $c{bgreen} . $2;
}
$new_str .= $substr;
$new_str .= $c{reset};
return $new_str;
}
return $string;
}
sub msg ($line) {
say format_line($line, $c{bblue});
}
sub note ($line) {
warn(format_line($line, $c{bred}) . "\n");
}
sub error ($line) {
die format_line($line, $c{bred});
}
sub json2perl ($json) {
require JSON;
JSON::from_json($json);
}
sub start_sudo_autorepeat () {
if (!$sudo_autorepeat_pid) {
system($lconfig{sudo_command}, '-v');
my $parent_pid = $$;
$sudo_autorepeat_pid = fork;
# Child process
if ($sudo_autorepeat_pid == 0) {
while ($parent_pid == getppid) {
system($lconfig{sudo_command}, '-v');
sleep $lconfig{sudo_autorepeat_interval};
}
exit;
}
}
}
sub get ($url) {
# Don't connect to the AUR servers in `--regular` mode
if ($lconfig{regular} or $lconfig{r}) {
return;
}
state $accepted_encodings = HTTP::Message::decodable();
my $response = $lwp->get($url, 'Accept-Encoding' => $accepted_encodings);
if ($response->is_success) {
my $content = $response->decoded_content;
if (not defined $content) {
note(":: Unable to decode HTML content: $@");
return;
}
return $content;
}
return;
}
sub get_comments ($id) {
$id // return;
$id =~ /^\d+\z/ or return;
my $url = sprintf($aur_package_id_url, $id);
my $content = get($url) // return;
require HTML::Entities;
my @comments;
while (
$content =~ m{
<h4\b.*?>\s*
(\S(?-s:.*?))\ commented\ on\ (\d\d\d\d-\d\d-\d\d\ \d\d:\d\d)
\s*</h4>\s*
<div\b.*?class="article-content">\s*
<div> \s* (.*?) </div>\s*
</div>
}gsix
) {
my $author = $1;
my $date = $2;
my $comment = $3;
$comment =~ s{<.*?>}{}gs;
$author =~ s{<.*?>}{}gs;
$comment = HTML::Entities::decode_entities($comment);
#$comment =~ s/\h{2,}/ /g;
$comment = strip_space($comment);
unshift @comments, <<"EOC";
$c{bold}Comment by $c{bgreen}$author$c{reset}$c{bold} on $date$c{reset}
$comment
EOC
}
return @comments;
}
sub get_pacman_arguments ($type, @options) {
my @args;
if ($type eq 'short' or $type eq 'all') {
push @args, @leftover_short_arguments;
}
if ($type eq 'long' or $type eq 'all') {
push @args, @leftover_long_arguments;
}
foreach my $option (@options) {
push(@args, '--' . $option) if $lconfig{$option};
}
return @args;
}
sub execute_pacman_command ($needs_root, @cmd) {
my $pacman_command = join(
q{ },
do {
my %seen;
($lconfig{pacman_command}, grep { !$seen{$_}++ } map { quotemeta($_) } @cmd);
}
);
if ( $needs_root
and $lconfig{sudo_autorepeat}
and $lconfig{use_sudo}
and !$lconfig{as_root}) {
start_sudo_autorepeat();
}
my $user_pacman_command = (
$needs_root
? $lconfig{use_sudo}
? "$lconfig{sudo_command} $pacman_command"
: qq{$lconfig{su_command} "$pacman_command"}
: $pacman_command
);
msg(":: Pacman command: " . ($user_pacman_command =~ s/\\(.)/$1/gr)) if $lconfig{debug};
{
system($lconfig{as_root} ? $pacman_command : $user_pacman_command);
if ($? and $needs_root) {
if (not $lconfig{ask_for_retry}) {
main_quit();
}
note(":: Exit code: " . exit_code()) if $lconfig{debug};
$term->ask_yn(prompt => "$c{bold}=>> Try again?$c{reset}", default => 'n') and redo;
}
}
return $? ? 0 : 1;
}
sub is_available_in_pacman_repo ($pkgname) {
my $output = `$lconfig{pacman_command} -Sp \Q$pkgname\E > /dev/stdout 2>&1`;
# In repo if the output has at least two lines
if ($output =~ /.\R:: /) {
return 1;
}
my $exit_code = $?;
$? = 0;
return ($exit_code ? 0 : 1);
}
sub package_is_installed ($pkgname, $strict = 1) {
my $installed_packages = {};
if (!$strict and ref($lconfig{_installed_packages_cache}) eq 'HASH') {
$installed_packages = $lconfig{_installed_packages_cache};
}
else {
if (opendir(my $dir_h, $lconfig{pacman_local_dir})) {
# Performance optimization when $strict is true
if ($strict) {
my $dirs_string = '/' . join('/', readdir($dir_h)) . '/';
closedir($dir_h);
if ($dirs_string =~ m{/\Q$pkgname\E-([^-]+-\d+)/}) {
return $1;
}
return;
}
foreach my $dir (readdir($dir_h)) {
if (scalar(reverse($dir)) =~ /^(.+?-.+?)-(.+)/) {
my $name = reverse($2);
my $version = reverse($1);
$installed_packages->{$name} = $version;
}
}
closedir($dir_h);
}
if (not keys(%$installed_packages)) {
%$installed_packages = map { split(' ', $_) } `$lconfig{pacman_command} -Q`;
}
# Cache the installed packages
$lconfig{_installed_packages_cache} = $installed_packages;
}
if (exists $installed_packages->{$pkgname}) {
return $installed_packages->{$pkgname};
}
return;
}
sub versioncmp ($v1, $v2) {
`/usr/bin/vercmp \Q$v1\E \Q$v2\E` + 0;
}
sub array_ref_or_string ($data) {
ref($data) eq 'ARRAY'
? (grep { defined($_) and $_ ne '' } @$data)
: ($data // 'None');
}
sub strip_space ($string) {
$string // return '';
$string =~ s/^\s+//;
return unpack 'A*', $string;
}
sub strip_version ($version) {
$version =~ s/\s*[<=>]=?.+//sg;
return strip_space($version);
}
sub parse_package_name_and_version ($pkgname) {
if ($pkgname =~ /^(.*?)([<=>]=?)(.*)/) {
return ($1, $2, $3);
}
return ($pkgname, undef, undef);
}
sub indent_array (@elems) {
my $first = strip_space(shift(@elems));
@elems or return $first;
my $rest = join("\n", @elems);
$rest =~ s/^\s+//gm;
$rest =~ s/^/' ' x 18/gem;
return "$first\n$rest";
}
sub info_for_package ($pkgname) {
my $info = get_rpc_info($pkgname) // return;
if (ref($info->{results}) ne 'ARRAY') {
note(":: Unable to get info for package: $pkgname") if $lconfig{debug};
return;
}
$info->{resultcount} > 0 or return;
if ($info->{resultcount} > 1) {
msg(":: Found <<$info->{resultcount}>> packages.");
print "\n";
my @packages = map { $_->{Name} } @{$info->{results}};
my %table = (map { $packages[$_] => $_ } 0 .. $#packages);
my $reply = $term->get_reply(
print_me => "\n$c{bold}=>> Select a package$c{reset}",
prompt => "$c{bold}Select$c{reset}",
choices => \@packages,
default => $packages[0],
);