-
Notifications
You must be signed in to change notification settings - Fork 15
/
sbofind
executable file
·184 lines (159 loc) · 5.2 KB
/
sbofind
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
#!/usr/bin/perl
#
# vim: ts=4:noet
#
# sbofind
# script to locate something in a local SlackBuilds tree.
#
# authors: Jacob Pipkin <[email protected]>
# Luke Williams <[email protected]>
# Andreas Guldstrand <[email protected]>
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>
use 5.16.0;
use strict;
use warnings FATAL => 'all';
use SBO::Lib qw/ slackbuilds_or_fetch slurp script_error open_read get_build_queue %config $slackbuilds_txt $repo_path show_version in indent get_from_info /;
use File::Basename;
use Getopt::Long qw(:config bundling);
my $self = basename($0);
sub show_usage {
print <<"EOF";
Usage: $self (search_term)
Options:
-h|--help:
this screen.
-v|--verison:
version information.
-e|--exact:
only exact matching.
-t|--no-tags:
exclude tags from search.
-i|--info:
show the .info for each found item.
-r|--readme:
show the README for each found item.
-q|--queue:
show the build queue for each found item.
Example:
$self libsexy
EOF
return 1;
}
my ($help, $vers, $search_exact, $exclude_tags, $show_info, $show_readme, $show_queue);
GetOptions(
'help|h' => \$help,
'version|v' => \$vers,
'exact|e' => \$search_exact,
'no-tags|t' => \$exclude_tags,
'info|i' => \$show_info,
'readme|r' => \$show_readme,
'queue|q' => \$show_queue,
);
if ($help) { show_usage(); exit 0 }
if ($vers) { show_version(); exit 0 }
if (!@ARGV) { show_usage(); exit 1 }
my $search = $ARGV[0];
# if we can't find SLACKBUILDS.TXT in $config{HOME}, prompt to fetch the tree
slackbuilds_or_fetch();
# find anything with $search in its name
sub perform_search {
script_error 'perform_search requires an argument.' unless @_ == 1;
my $search_arg = shift;
my $search_tag_re = $search_exact ? qr/^(\S+).*(:\s|,)\b\Q$search_arg\E\b(,|$)/i : qr/^(\S+):\s.*\Q$search_arg\E/i;
my $search_name_re = $search_exact ? qr/^\Q$search_arg\E$/i : qr/.*\Q$search_arg\E.*/i;
# first get a bunch of names from the TAGS.txt if it's available
my $tags_file = "$config{SBO_HOME}/repo/TAGS.txt";
my @names;
if (!$exclude_tags && -f $tags_file) {
_race::cond('$tags_file may be deleted after -f check');
my ($t_fh, $t_exit) = open_read "$config{SBO_HOME}/repo/TAGS.txt";
unless ($t_exit) {
while (my $line = <$t_fh>) {
if ($line =~ $search_tag_re) {
push @names, $1;
}
}
}
}
my $loc_regex = qr/LOCATION:\s+\.?(.*)$/;
my ($fh, $exit) = open_read $slackbuilds_txt;
if ($exit) {
warn $fh;
exit $exit;
}
my (%local, @findings);
FIRST: while (my $line = <$fh>) {
if ($line =~ /NAME:\s+(.*)$/) {
my $name = $1;
# Try to match either one of the names from TAGS.txt or the search string
my $names = @names;
# Whenever we find an element equal to $name, throw it away (and
# replace with last element rather than shifting stuff around)
for (reverse @names) { $_ = pop @names if $_ eq $name; }
# next if $name didn't match either one of @names or $search_name_re
if ($names == @names and $name !~ $search_name_re) { next FIRST; }
# We only reach this point if $name matched one of @names, or if
# $search_name_re matched
# If the name matches a local override, use its location
if ($config{LOCAL_OVERRIDES} ne 'FALSE' and -d "$config{LOCAL_OVERRIDES}/$name") {
push @findings, {name => $name, location => "$config{LOCAL_OVERRIDES}/$name", local => 1 };
$local{$name} = 1;
next FIRST;
}
# Otherwise the location should be in the next line
LOCATION: {
my $loc_line = <$fh>;
if (my ($location) = $loc_line =~ $loc_regex) {
push @findings, {name => $name, location => $repo_path . $location};
next FIRST;
} else {
redo LOCATION; # But if it isn't, we try again...
}
}
}
}
if ($config{LOCAL_OVERRIDES} ne 'FALSE') {
opendir(my $dh, $config{LOCAL_OVERRIDES});
while (my $dir = readdir($dh)) {
next if $local{$dir};
if ($dir =~ $search_name_re or in($dir, @names)) {
push @findings, {name => $dir, location => "$config{LOCAL_OVERRIDES}/$dir", local => 1 };
}
}
closedir $dh;
}
return \@findings;
}
# pull the contents of a file into a variable and format it for output
sub get_file_contents {
script_error 'get_file_contents requires an argument.' unless @_ == 1;
my $file = shift;
my $contents = slurp($file);
return "Unable to open $file.\n" unless defined $contents;
return "\n" . indent 6, $contents;
}
# get build queue and return it as a single line.
sub show_build_queue {
script_error('show_build_queue requires an argument.') unless @_ == 1;
my $queue = get_build_queue([shift], {});
return join(" ", @$queue);
}
my $findings = perform_search($search);
# pretty formatting
if (exists $$findings[0]) {
for my $hash (@$findings) {
my $name = $hash->{name};
my $location = $hash->{location};
my $version = get_from_info(LOCATION => $location, GET => 'VERSION')->[0];
my $sbo = "SBo: "; $sbo = "Local: " if $hash->{local};
say "$sbo $name $version";
say "Path: $location";
say "info: ". get_file_contents("$location/$name.info") if $show_info;
say "README: ". get_file_contents("$location/README") if $show_readme;
say "Queue: ". show_build_queue($name) if $show_queue;
say '';
}
} else {
say "Nothing found for search term: $search";
}
exit 0;