Debian Codegraph @

bin/install_deps.pl

raw
1 #!/usr/bin/perl
2
3 # v1.7 - 2013-04-20 - Matt
4 # - get list of modules from Makefile.PL or dist.ini
5 # - abstracted yum and apt into subs
6 #
7 # v1.6 - 2013-04-01 - Matt
8 # - improved error reporting for FreeBSD port installs
9 #
10 # v1.5 - 2013-03-27 - Matt
11 # - added option to specify port category
12 #
13 # v1.4 - 2012-10-23 - Matt
14 # - improved yum & apt-get module installer
15 #
16 # v1.3 - 2012-10-23 - Matt
17 # - added apt-get support
18 # - added app install support
19 #
20 # circa 2008, by Matt Simerson & Phil Nadeau
21 # - based on installer in Mail::Toaster dating back to the 20th century
22
23 use strict;
24 use warnings;
25
26 use CPAN;
27 use English qw( -no_match_vars );
28
29 my $apps = [
30 { app => 'daemontools', info => { } },
31 { app => 'ucspi-tcp', info => { } },
32 # { app => 'dspam', info => { } },
33 # { app => 'mysql-server-55', info => { port => 'mysql55-server', dport=>'mysql5', yum =>'mysql-server'} },
34 # { app => 'apache22' , info => { port => 'apache22', dport=>'', yum => 'httpd' } },
35 ];
36
37 $EUID == 0 or die "You will have better luck if you run me as root.\n";
38
39 my @failed;
40 foreach ( @$apps ) {
41 my $name = $_->{app} or die 'missing app name';
42 install_app( $name, $_->{info} );
43 };
44
45 foreach ( get_perl_modules() ) {
46 #print Dumper($_);
47 my $module = $_->{module} or die 'missing module name';
48 my $info = $_->{info};
49 my $version = $info->{version} || '';
50 print "checking for $module $version\n";
51
52 ## no critic
53 eval "use $module $version";
54 next if ! $EVAL_ERROR;
55 next if $info->{ships_with} && $info->{ships_with} eq 'perl';
56
57 install_module( $module, $info, $version );
58 eval "use $module $version";
59 ## use critic
60 if ($EVAL_ERROR) {
61 push @failed, $module;
62 }
63 }
64
65 if ( scalar @failed > 0 ) {
66 print "The following modules failed installation:\n";
67 print join( "\n", @failed );
68 print "\n";
69 }
70
71 exit;
72
73 sub get_perl_modules {
74 if ( -f 'dist.ini' ) {
75 return get_perl_modules_from_ini();
76 };
77 if ( -f 'Makefile.PL' ) {
78 return get_perl_modules_from_Makefile_PL();
79 };
80 die "unable to find module list. Run this script in the dist dir\n";
81 };
82
83 sub get_perl_modules_from_Makefile_PL {
84 my $fh = new IO::File 'Makefile.PL', 'r'
85 or die "unable to read Makefile.PL\n";
86
87 my $in = 0;
88 my @modules;
89 foreach my $line ( <$fh> ) {
90 if ( $line =~ /PREREQ_PM/ ) {
91 $in++;
92 next;
93 };
94 next if ! $in;
95 last if $line =~ /}/;
96 next if $line !~ /=/; # no = char means not a module
97 my ($mod,$ver) = split /\s*=\s*/, $line;
98 $mod =~ s/[\s'"\#]*//g; # remove whitespace and quotes
99 next if ! $mod;
100 push @modules, name_overrides($mod);
101 #print "module: .$mod.\n";
102 }
103 $fh->close;
104 return @modules;
105 };
106
107 sub get_perl_modules_from_ini {
108 my $fh = new IO::File 'dist.ini', 'r'
109 or die "unable to read dist.ini\n";
110
111 my $in = 0;
112 my @modules;
113 foreach my $line ( <$fh> ) {
114 if ( '[Prereqs]' eq substr($line,0,9) ) {
115 $in++;
116 next;
117 };
118 next if ! $in;
119 print "line: $line\n";
120 last if '[' eq substr($line,0,1); # [...] starts a new section
121 my ($mod,$ver) = split /\s*=\s*/, $line;
122 $mod =~ s/\s*//g; # remove whitespace
123 next if ! $mod;
124 push @modules, name_overrides($mod);
125 print "module: $mod\n";
126 }
127 $fh->close;
128 #print Dumper(\@modules);
129 return @modules;
130 };
131
132 sub install_app {
133 my ( $app, $info) = @_;
134
135 if ( lc($OSNAME) eq 'darwin' ) {
136 install_app_darwin($app, $info );
137 }
138 elsif ( lc($OSNAME) eq 'freebsd' ) {
139 install_app_freebsd($app, $info );
140 }
141 elsif ( lc($OSNAME) eq 'linux' ) {
142 install_app_linux( $app, $info );
143 };
144
145 };
146
147 sub install_app_darwin {
148 my ($app, $info ) = @_;
149
150 my $port = $info->{dport} || $info->{port} || $app;
151
152 if ( ! -x '/opt/local/bin/port' ) {
153 print "MacPorts is not installed! Consider installing it.\n";
154 return;
155 }
156
157 system "/opt/local/bin/port install $port"
158 and warn "install failed for Darwin port $port";
159 }
160
161 sub install_app_freebsd {
162 my ($app, $info ) = @_;
163
164 print " from ports...";
165 my $name = $info->{port} || $app;
166
167 if ( `/usr/sbin/pkg_info | /usr/bin/grep $name` ) {
168 return print "$app is installed.\n";
169 }
170 elsif( `/usr/sbin/pkg info | /usr/bin/grep $name` ) {
171 return print "$app is installed.\n";
172 }
173
174 print "installing $app";
175
176 my $category = $info->{category} || '*';
177 my ($portdir) = glob "/usr/ports/$category/$name";
178
179 if ( $portdir && -d $portdir && chdir $portdir ) {
180 print " from ports ($portdir)\n";
181 system "make install clean"
182 and warn "'make install clean' failed for port $app\n";
183 };
184 };
185
186 sub install_app_linux {
187 my ($app, $info ) = @_;
188
189 if ( -x '/usr/bin/yum' ) {
190 my $rpm = $info->{yum} || $app;
191 system "/usr/bin/yum -y install $rpm";
192 }
193 elsif ( -x '/usr/bin/apt-get' ) {
194 my $package = $info->{apt} || $app;
195 system "/usr/bin/apt-get -y install $package";
196 }
197 else {
198 warn "no Linux package manager detected\n";
199 };
200 };
201
202
203 sub install_module {
204
205 my ($module, $info, $version) = @_;
206
207 if ( lc($OSNAME) eq 'darwin' ) {
208 install_module_darwin($module, $info, $version);
209 }
210 elsif ( lc($OSNAME) eq 'freebsd' ) {
211 install_module_freebsd($module, $info, $version);
212 }
213 elsif ( lc($OSNAME) eq 'linux' ) {
214 install_module_linux( $module, $info, $version);
215 };
216
217 ## no critic
218 eval "require $module";
219 ## use critic
220 return 1 if ! $EVAL_ERROR;
221
222 install_module_cpan($module, $version);
223 };
224
225 sub install_module_cpan {
226
227 my ($module, $version) = @_;
228
229 print " from CPAN...";
230 sleep 1;
231
232 # this causes problems when CPAN is not configured.
233 #$ENV{PERL_MM_USE_DEFAULT} = 1; # supress CPAN prompts
234
235 $ENV{FTP_PASSIVE} = 1; # for FTP behind NAT/firewalls
236
237 # some Linux distros break CPAN by auto/preconfiguring it with no URL mirrors.
238 # this works around that annoying little habit
239 no warnings;
240 $CPAN::Config = get_cpan_config();
241 use warnings;
242
243 # a hack to grab the latest version on CPAN before its hits the mirrors
244 if ( $module eq 'Provision::Unix' && $version ) {
245 $module =~ s/\:\:/\-/g;
246 $module = "M/MS/MSIMERSON/$module-$version.tar.gz";
247 }
248 CPAN::Shell->install($module);
249 }
250
251 sub install_module_darwin {
252 my ($module, $info, $version) = @_;
253
254 my $dport = '/opt/local/bin/port';
255 if ( ! -x $dport ) {
256 print "MacPorts is not installed! Consider installing it.\n";
257 return;
258 }
259
260 my $port = "p5-$module";
261 $port =~ s/::/-/g;
262 system "$dport install $port"
263 and warn "install failed for Darwin port $module";
264 }
265
266 sub install_module_freebsd {
267 my ($module, $info, $version) = @_;
268
269 my $name = $info->{port} || $module;
270 my $portname = "p5-$name";
271 $portname =~ s/::/-/g;
272
273 print " from ports...$portname...";
274
275 if ( `/usr/sbin/pkg_info | /usr/bin/grep $portname` ) {
276 return print "$module is installed.\n";
277 }
278 elsif( `/usr/sbin/pkg info | /usr/bin/grep $portname` ) {
279 return print "$module is installed.\n";
280 }
281
282 print "installing $module ...";
283
284 my $category = $info->{category} || '*';
285 my ($portdir) = glob "/usr/ports/$category/$portname";
286
287 if ( ! $portdir || ! -d $portdir ) {
288 print "oops, no match at /usr/ports/$category/$portname\n";
289 return;
290 };
291
292 if ( ! chdir $portdir ) {
293 print "unable to cd to /usr/ports/$category/$portname\n";
294 };
295
296 print " from ports ($portdir)\n";
297 system "make install clean"
298 and warn "'make install clean' failed for port $module\n";
299 }
300
301 sub install_module_linux {
302 my ($module, $info, $version) = @_;
303
304 my $package;
305 if ( -x '/usr/bin/yum' ) {
306 return install_module_linux_yum($module, $info);
307 }
308 elsif ( -x '/usr/bin/apt-get' ) {
309 return install_module_linux_apt($module, $info);
310 }
311 warn "no Linux package manager detected\n";
312 };
313
314 sub install_module_linux_yum {
315 my ($module, $info) = @_;
316 my $package;
317 if ( $info->{yum} ) {
318 $package = $info->{yum};
319 }
320 else {
321 $package = "perl-$module";
322 $package =~ s/::/-/g;
323 };
324 system "/usr/bin/yum -y install $package";
325 };
326
327 sub install_module_linux_apt {
328 my ($module, $info) = @_;
329 my $package;
330 if ( $info->{apt} ) {
331 $package = $info->{apt};
332 }
333 else {
334 $package = 'lib' . $module . '-perl';
335 $package =~ s/::/-/g;
336 };
337 system "/usr/bin/apt-get -y install $package";
338 };
339
340 sub get_cpan_config {
341
342 my $ftp = `which ftp`; chomp $ftp;
343 my $gzip = `which gzip`; chomp $gzip;
344 my $unzip = `which unzip`; chomp $unzip;
345 my $tar = `which tar`; chomp $tar;
346 my $make = `which make`; chomp $make;
347 my $wget = `which wget`; chomp $wget;
348
349 return
350 {
351 'build_cache' => q[10],
352 'build_dir' => qq[$ENV{HOME}/.cpan/build],
353 'cache_metadata' => q[1],
354 'cpan_home' => qq[$ENV{HOME}/.cpan],
355 'ftp' => $ftp,
356 'ftp_proxy' => q[],
357 'getcwd' => q[cwd],
358 'gpg' => q[],
359 'gzip' => $gzip,
360 'histfile' => qq[$ENV{HOME}/.cpan/histfile],
361 'histsize' => q[100],
362 'http_proxy' => q[],
363 'inactivity_timeout' => q[5],
364 'index_expire' => q[1],
365 'inhibit_startup_message' => q[1],
366 'keep_source_where' => qq[$ENV{HOME}/.cpan/sources],
367 'lynx' => q[],
368 'make' => $make,
369 'make_arg' => q[],
370 'make_install_arg' => q[],
371 'makepl_arg' => q[],
372 'ncftp' => q[],
373 'ncftpget' => q[],
374 'no_proxy' => q[],
375 'pager' => q[less],
376 'prerequisites_policy' => q[follow],
377 'scan_cache' => q[atstart],
378 'shell' => q[/bin/csh],
379 'tar' => $tar,
380 'term_is_latin' => q[1],
381 'unzip' => $unzip,
382 'urllist' => [ 'http://www.perl.com/CPAN/', 'http://mirrors.kernel.org/pub/CPAN/', 'ftp://cpan.cs.utah.edu/pub/CPAN/', 'ftp://mirrors.kernel.org/pub/CPAN', 'ftp://osl.uoregon.edu/CPAN/', 'http://cpan.yahoo.com/', 'ftp://ftp.funet.fi/pub/languages/perl/CPAN/' ],
383 'wget' => $wget, };
384 }
385
386 sub name_overrides {
387 my $mod = shift;
388 # Package and port managers have naming conventions for perl modules. The
389 # methods will typically work out the name based on the module name and a
390 # couple rules. When that doesn't work, add entries here for FreeBSD (port),
391 # MacPorts ($dport), yum, and apt.
392 my @modules = (
393 { module=>'LWP::UserAgent', info => { cat=>'www', port=>'p5-libwww', dport=>'p5-libwww-perl' }, },
394 { module=>'Mail::Send' , info => { port => 'Mail::Tools', } },
395 { module=>'Mail::SpamAssassin' , info => { cat => 'mail', } },
396 );
397 my ($match) = grep { $_->{module} eq $mod } @modules;
398 return $match if $match;
399 return { module=>$mod, info => { } };
400 };