root/floppyfw-3.0/perl/modpick.pl

Revision 181, 6.9 KB (checked in by root, 5 years ago)

More than one module didn't work, fixed.

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2#
3# $Id:$
4
5=head1 AUTHOR
6
7Thomas Lundquist <thomasez@zelow.no>
8
9=head1 NAME
10
11modpick.pl
12
13=head1 SYNOPSIS
14
15modpick.pl [options] -m <Module(s)>
16
17=head1 DESCRIPTION
18
19Gives the right option it will return a list with the required modules.
20
21And no, this script ain't made for efficiency and I've used a script
22I made a few years ago as the base, it may not be perfect..
23
24=head1 OPTIONS
25
26=over
27
28=item -m, --module <STRING> REQUIRED
29
30A module name or list of modules.
31
32with options in () after the module.
33
34=item -k, --kernelversion <STRING>
35
36The kernel version.
37
38(This can also be set in modpick.conf)
39
40=item -r, --moduleroot <STRING>
41
42The root of the module tree.
43
44(This can also be set in modpick.conf)
45
46=item -c, --copyto <STRING>
47
48The base directory for copying the modules to.
49
50(This can also be set in modpick.conf)
51
52=item -d --deplist
53
54Prints a list of modules, with path relative to the moduleroot, needed
55for the specified module.
56
57=item -h --help
58
59Prints short help message about usage.
60
61=back
62
63=cut
64
65
66use strict;
67use Getopt::Long qw(:config gnu_getopt); #);
68use Pod::Usage;
69use Data::Dumper;
70use File::Basename;
71use Text::ParseWords;
72
73
74use vars qw($help $moduleroot $kernelversion $modules $copyto $deplist $help);
75
76if ( -f "./modpick.conf" ) { require ("./modpick.conf"); }
77
78my %opts = ("help|h"            => \$help,
79            "kernelversion|k=s" => \$kernelversion,
80            "moduleroot|r=s"    => \$moduleroot,
81            "module|m=s"        => \$modules,
82            "copyto|c=s"        => \$copyto,
83            "deplist|d"         => \$deplist,
84            );
85
86GetOptions(%opts);
87
88if ($help) {
89    pod2usage(1);
90    exit(0);
91}
92
93unless ($kernelversion && $moduleroot && $modules) {
94    pod2usage(1);
95    exit(1);
96}
97
98my ($allmodules, $p) = &readModulesDep();
99
100# print STDERR Dumper($allmodules);
101
102my (@deplist);
103my (%optlist);
104my ($last_module);
105
106foreach my $module ( quotewords('\s+', 0, $modules) ) {
107
108# print STDERR "this: ($module)\n";
109
110        # Ok, first we have to check for options.
111        # If there are only one option inside we'll both start and end
112        # here.
113        if (  $module =~ /(.*)\)/ &&  $module !~ /\(/ ) {
114
115                $optlist{$last_module} .= ($1 . " ");
116                # So, get rid of the (first) eventual (
117                $optlist{$last_module} =~ s/\(//;
118
119                $last_module = undef;
120                next;
121               
122        }
123
124        if ($last_module && exists($optlist{$last_module})) {
125                $optlist{$last_module} .= ($module . " ");
126                next;
127        }
128       
129        if ($module =~ /(.*)\((.*)/) {
130
131                $module = $last_module = ($1 || $last_module);
132                # Too much of .o and not .o for my liking.
133                $last_module =~ s/\.o$//;
134                $optlist{$last_module} .= ($2 . " ");
135
136                # $last_module = undef if ($optlist{$last_module} =~ s/\)//);
137                if ($optlist{$last_module} =~ /\)/) {
138                        $optlist{$last_module} =~ s/\)//;
139                        $last_module = undef;
140                }
141
142        } else {
143                $last_module = $module;
144        }
145               
146
147        next unless ($module);
148
149        $module .= ".o" unless ($module =~ /\.o$/);
150
151        unless (defined($allmodules->{$module})) { 
152                die "Module $module not found.\n";
153        }
154
155# print STDERR "getDependFiles($module)\n";
156
157        push @deplist,  getDependFiles($module);
158
159}
160
161if ($copyto) {
162        die "No directory" unless ( -d $copyto );
163
164
165        foreach my $module (@deplist) {
166# print STDERR "copyto($module)\n";
167                # Quick and dirty:
168                my ($dn) = dirname($module);
169                my ($bn) = basename($module);
170                my ($nd) = "$copyto/lib/modules/$kernelversion/kernel/$dn";
171
172# print STDERR "$nd\n";
173
174                system('mkdir', '-p', $nd);
175                system('cp', "$moduleroot/lib/modules/$kernelversion/kernel/$module", $nd . "/.");
176
177        }
178
179}
180
181# This is not really the deplist but the list you can use
182# for modules.lst
183if ($deplist) {
184
185        map { s/.*\/// } @deplist;
186        map { s/\.o// } @deplist;
187
188        # I shall say this only once.
189        # And never read it again, thanks Peder.
190        # printf "%s\n", join "\n", do { my %f; grep { ! $f{$_}++ } @deplist };
191        # But, this oneliner was neat altho I need to add options.
192        my %f;
193        foreach my $d (@deplist) {
194                next if ($f{$d});
195                $f{$d}++;
196                print $d;
197                if (exists($optlist{$d})) { print " " . $optlist{$d}; };
198                print "\n";
199        }       
200
201} else {
202        # Not sure I need this.
203        print join(" ", @deplist) . "\n";
204
205}
206
207# print STDERR "PATHS:\n\n";
208# &printPaths($m, $p);
209
210sub getDependFiles {
211        my ($mod) = @_;
212        my (@retarr);
213
214# print STDERR "Testing $mod\n";
215
216        # First, check if there are (more) dependencies:
217        # If so, recurseively, ask for more files:
218        if (exists($allmodules->{$mod}->{depends})) {
219                foreach my $mo (@{$allmodules->{$mod}->{depends}}) {
220# print STDERR "Checking $mo\n";
221                        push @retarr, getDependFiles($mo);
222                }
223        }
224
225        push @retarr, $allmodules->{$mod}->{file};
226
227        return @retarr;
228
229
230}
231
232sub readModulesDep {
233
234        my ($modules, $paths);
235        my ($depfile) = $moduleroot . "/lib/modules/" . $kernelversion . "/modules.dep";
236        open (DEP, "$depfile" )
237                 or die "No modules.dep.\n";
238
239        my ($last_module);
240        while (<DEP>) {
241
242                chomp;
243                next unless (/kernel/);
244
245                my ($line) = /\w+\/kernel\/(.*)/;
246
247# print STDERR "\n\n" . $line . "\n";
248
249                if ($line =~ /\:/) {
250                        $last_module = undef;
251
252                        # To fix busybox depmod.pl:
253                        $line =~ s/\s+$//;
254
255                        my ($modline, $dep) = split (/\:/, $line);
256                        my (@path) = split (/\//, $modline);
257
258                        my ($module, $lastp, $lastp1, $lastp2, $lastp3);
259
260                        foreach my $p (@path) {
261                                #
262                                # We have a module;
263                                #
264                                ($module) = $1 if ( $p =~ /(.*\.o)/ );
265
266                                if ($module) {
267                                        # Gawd this is ugly.
268
269                                        if ($lastp1) { push @{$paths->{$lastp}->{'modules'}}, $module; }
270
271                                        if ($lastp2) { push @{$paths->{$lastp}->{$lastp1}->{'modules'}}, $module; }
272
273                                        if ($lastp3) { 
274                                                push @{$paths->{$lastp}->{$lastp1}->{$lastp2}->{'modules'}}, $module;
275                                                push @{$paths->{$lastp}->{$lastp1}->{$lastp2}->{$lastp3}->{'modules'}}, $module;
276                                        }
277
278
279                                        $modules->{$module}->{'file'} = $modline;
280
281                                        next;
282                                }
283
284
285                                # Gawd this is ugly. Part II.
286                                $lastp3 = $p if ($lastp && $lastp1 && $lastp2 && !$lastp3);
287                                $lastp2 = $p if ($lastp && $lastp1 && !$lastp2);
288                                $lastp1 = $p if ($lastp && !$lastp1);
289                                $lastp = $p if (!$lastp);
290
291
292                        }
293# print STDERR $module . ";" . $lastp . ";" . $lastp1 . ";" . $lastp2 . ";" . "$lastp3;\n";
294
295                        if ($dep) {
296# print STDERR "DEP: $dep\n";
297                                foreach my $single (split(/\s+/, $dep)) {
298                                        my ($dmodule) = $1 if ($single =~ /\/(\w+-?\w+\.o)/);
299                                        next unless $dmodule;
300# print STDERR "DEPM: $module-$dmodule-$single\n";
301                                        push @{$modules->{$module}->{'depends'}}, $dmodule;
302                                        $last_module = $module;
303                                }
304
305                        }
306
307
308
309                }
310
311                if ($last_module && $line =~ /^\t/) {
312                        my ($dmodule) = $1 if ( $_ =~ /(.*\.o)/ );
313# print STDERR "DEPM2: $dmodule\n";
314
315                        push @{$modules->{$last_module}->{'depends'}}, $dmodule;
316                }
317               
318
319        }
320       
321        close (DEP);
322        return ($modules, $paths);
323
324}
325
326sub printPaths {
327        my ($modules, $path, $level) = @_;
328
329        $level++;
330
331        foreach my $pat (sort(keys (%$path))) {
332                print "Path:$pat\n" if ($pat && $pat ne "modules");
333                if ($pat eq "modules") {
334                        foreach my $mod (@{$path->{'modules'}}) {
335                                print "\t$mod - ";
336                                print join (";", @{$modules->{$mod}->{'depends'}}) if (exists($modules->{$mod}->{'depends'}));
337                                print "\n";
338
339
340                        }
341                        next;
342                }
343                &printPaths($modules, $path->{$pat}, $level) if (ref($path->{$pat}) eq "HASH");
344        }
345
346}
Note: See TracBrowser for help on using the browser.