-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnomdep
More file actions
executable file
·273 lines (221 loc) · 5.38 KB
/
Copy pathnomdep
File metadata and controls
executable file
·273 lines (221 loc) · 5.38 KB
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
#!/usr/bin/env perl
#
# Copyright (C) 2017 Christian Garbs <mitch@cgarbs.de>
# Licensed under GNU GPL v3 or later.
#
# This file is part of nomd.
#
# nomd 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.
#
# nomd 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 nomd. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use warnings;
use v5.10;
use File::Basename;
use Getopt::Long;
#################################
sub find_perl_files()
{
return glob("nomdoc nomdep check/wrapper/*.pl");
}
sub find_shell_files()
{
return glob("nomd check/*.check notify/*.notify tool/*.tool");
}
sub extract_perl_dependencies($)
{
my ($file) = @_;
my @deps = ();
open PERL, '<', $file or die "can't open $file: $!";
while (my $line = <PERL>) {
if ($line =~ /^use ([a-zA-z0-9.:]+)(?:\s+.*)?;/) {
chomp $line;
push @deps, {
FILE => $file,
DEP => $1,
LINE => $line,
TYPE => 'perl',
};
}
}
close PERL or die "can't close $file: $!";
return @deps;
}
sub extract_shell_dependencies($)
{
my ($file) = @_;
my @deps = ();
open SHELL, '<', $file or die "can't open $file: $!";
while (my $line = <SHELL>) {
if ($line =~ /^##SHELLDEP\s+(.+?)\s*$/) {
chomp $line;
push @deps, {
FILE => $file,
DEP => $1,
LINE => $line,
TYPE => 'shell',
};
}
}
close SHELL or die "can't close $file: $!";
return @deps;
}
sub group_by_line(@)
{
my (@deps) = @_;
my %uses = ();
foreach my $dep (@deps) {
$uses{$dep->{LINE}}->{DEP} = $dep->{DEP};
$uses{$dep->{LINE}}->{LINE} = $dep->{LINE};
$uses{$dep->{LINE}}->{TYPE} = $dep->{TYPE};
push @{$uses{$dep->{LINE}}->{FILES}}, $dep->{FILE};
}
return values %uses;
}
sub sort_by_type_then_name()
{
return $a->{TYPE} cmp $b->{TYPE} || $a->{DEP} cmp $b->{DEP};
}
sub is_binary_in_path($)
{
my ($cmd) = @_;
`which $cmd`;
return ! $?;
}
sub keep_missing($)
{
my ($dep) = @_;
if ($dep->{TYPE} eq 'perl') {
eval($dep->{LINE});
my $module_load_ok = $@ eq '';
return ! $module_load_ok;
}
elsif ($dep->{TYPE} eq 'shell') {
# support OR conditions with multiple binaries in one line
my $binary_in_path = grep { is_binary_in_path $_ } split /\s+/, $dep->{DEP};
return ! $binary_in_path;
}
return 1;
}
sub print_missing($)
{
my (@deps) = @_;
foreach my $dep (@deps) {
my $name = $dep->{DEP};
if ($dep->{TYPE} eq 'shell') {
# expand OR conditions
$name =~ s/\s+/ or /g;
}
printf
"missing %s dependency: %s (used in %s)\n",
$dep->{TYPE},
$name,
join(', ', map { basename $_} @{$dep->{FILES}});
}
}
sub print_cpanm($)
{
my (@deps) = @_;
my @modules = map { $_->{DEP} } @deps;
# TODO: remove --skip-satisfied? will break with versioned dependencies
printf "cpanm --skip-satisfied %s\n", join(' ', @modules);
}
sub print_apt($)
{
my (@deps) = @_;
my %APT_MAP = ('notify-send' => 'libnotify-bin');
my @packages = map {
if (exists $APT_MAP{$_}) {
$APT_MAP{$_}
}
else {
$_
}
} map {
if ($_->{TYPE} eq 'perl') {
sprintf "lib%s-perl", join('-', split(/::/, lc($_->{DEP})));
}
elsif ($_->{TYPE} eq 'shell') {
# only take first option if there are more
(split /\s/, $_->{DEP}, 2)[0];
}
else {
$_->{DEP}
}
} @deps;
printf "apt install %s\n", join(' ', @packages);
}
sub print_pkg($)
{
my (@deps) = @_;
my @packages = map {
if ($_->{TYPE} eq 'perl') {
sprintf "p5-%s", join('-', split(/::/, $_->{DEP}));
}
elsif ($_->{TYPE} eq 'shell') {
# only take first option if there are more
(split /\s/, $_->{DEP}, 2)[0];
}
else {
$_->{DEP}
}
} @deps;
printf "pkg install %s\n", join(' ', @packages);
}
sub get_perl_deps()
{
return map { extract_perl_dependencies($_) } find_perl_files();
}
sub get_shell_deps()
{
return map { extract_shell_dependencies($_) } find_shell_files();
}
#################################
my $help;
my $apt;
my $cpanm;
my $pkg;
GetOptions('help' => \$help, 'apt' => \$apt, 'cpanm' => \$cpanm, 'pkg' => \$pkg);
if ($help) {
print <<'EOF'
usage: nomdep [--help | --apt | --cpanm | --pkg]
Call without parameters to list all missing dependencies.
Use --cpanm to generate a cpanm command to install missing Perl libraries.
Use --apt to try to guess package names to install missing dependencies via apt.
Use --pkg to try to guess package names to install missing dependencies via pkg.
EOF
;
exit 0;
}
my @deps;
my $printer;
if ($apt) {
@deps = (get_perl_deps(), get_shell_deps());
$printer = \&print_apt;
}
elsif ($cpanm)
{
@deps = get_perl_deps();
$printer = \&print_cpanm;
}
elsif ($pkg)
{
@deps = (get_perl_deps(), get_shell_deps());
$printer = \&print_pkg;
}
else {
@deps = (get_perl_deps(), get_shell_deps());
$printer = \&print_missing;
}
my @missing = sort { sort_by_type_then_name() } grep { keep_missing($_) } group_by_line (@deps);
$printer->(@missing) if @missing;