|
1 #!/usr/bin/perl -w |
|
2 # This Source Code Form is subject to the terms of the Mozilla Public |
|
3 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
5 |
|
6 |
|
7 # |
|
8 # Create a mapping from symbolic component name to directory name(s). |
|
9 # |
|
10 # Tue Oct 16 16:48:36 PDT 2001 |
|
11 # <mcafee@netscape.com> |
|
12 |
|
13 use strict; |
|
14 |
|
15 # For --option1, --option2, ... |
|
16 use Getopt::Long; |
|
17 Getopt::Long::Configure("bundling_override"); |
|
18 Getopt::Long::Configure("auto_abbrev"); |
|
19 |
|
20 # Globals |
|
21 my $list_only_mode = 0; |
|
22 my $opt_list_only; |
|
23 my $mapfile = ""; |
|
24 my %map; |
|
25 |
|
26 sub PrintUsage { |
|
27 die <<END_USAGE |
|
28 Prints out directories needed for a given list of components. |
|
29 usage: module2dir.pl [--list-only] [--mapfile mapfile] <component-name1> <component-name2> ... |
|
30 END_USAGE |
|
31 } |
|
32 |
|
33 sub parse_map_file($) { |
|
34 my ($mapfile) = @_; |
|
35 my (%mod_map, $tmp, $dir, $mod, @mod_list); |
|
36 |
|
37 undef %mod_map; |
|
38 open (MAPFILE, "$mapfile") || die ("$mapfile: $!\n"); |
|
39 while ($tmp=<MAPFILE>) { |
|
40 chomp ($tmp); |
|
41 ($dir, $mod, @mod_list) = split(/:/, $tmp, 3); |
|
42 $mod =~ s/[\s]*(\S+)[\s]*/$1/; |
|
43 $mod_map{$mod} .= "$dir "; |
|
44 } |
|
45 close(MAPFILE); |
|
46 foreach $mod (sort(keys %mod_map)) { |
|
47 my (@dirlist, @trimlist, $found, $tdir); |
|
48 @dirlist = split(/\s+/, $mod_map{$mod}); |
|
49 $mod_map{$mod} = ""; |
|
50 foreach $dir (@dirlist) { |
|
51 $found = 0; |
|
52 foreach $tdir (@trimlist) { |
|
53 $found++, last if ($dir =~ m/^$tdir\// || $dir eq $tdir); |
|
54 } |
|
55 push @trimlist, $dir if (!$found); |
|
56 } |
|
57 $map{$mod} = join(" ", @trimlist); |
|
58 #print "$mod: $map{$mod}\n"; |
|
59 } |
|
60 } |
|
61 |
|
62 sub dir_for_required_component { |
|
63 my ($component) = @_; |
|
64 my $rv; |
|
65 my $dir; |
|
66 |
|
67 $dir = $map{$component}; |
|
68 if($dir) { |
|
69 # prepend "mozilla/" in front of directory names. |
|
70 $rv = "mozilla/$dir"; |
|
71 $rv =~ s/\s+/ mozilla\//g; # Hack for 2 or more directories. |
|
72 } else { |
|
73 $rv = 0; |
|
74 } |
|
75 return $rv; |
|
76 } |
|
77 |
|
78 { |
|
79 |
|
80 # Add stdin to the commandline. This makes commandline-only mode hang, |
|
81 # call it a bug. Not sure how to get around this. |
|
82 push (@ARGV, split(' ',<STDIN>)); |
|
83 |
|
84 PrintUsage() if !GetOptions('list-only' => \$opt_list_only, |
|
85 'mapfile=s' => \$mapfile); |
|
86 |
|
87 # Pick up arguments, if any. |
|
88 if($opt_list_only) { |
|
89 $list_only_mode = 1; |
|
90 } |
|
91 |
|
92 &parse_map_file($mapfile); |
|
93 |
|
94 my $arg; |
|
95 my $dir; |
|
96 while ($arg = shift @ARGV) { |
|
97 $dir = dir_for_required_component($arg); |
|
98 if($dir) { |
|
99 if($list_only_mode) { |
|
100 print $dir, " "; |
|
101 } else { |
|
102 print "$arg: ", $dir, "\n"; |
|
103 } |
|
104 } else { |
|
105 # do nothing |
|
106 } |
|
107 } |
|
108 if($dir && $list_only_mode) { |
|
109 print "\n"; |
|
110 } |
|
111 } |