summaryrefslogtreecommitdiff
path: root/scripts/perldeps.pl
blob: 76c1070e6d84c9f9aea60581dfdfdfb4327efad1 (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl -w
use strict;
use 5.006001;

use Getopt::Long;
my ($show_provides, $show_requires, $verbose, @ignores);

my $result = GetOptions("provides" => \$show_provides,
			"requires" => \$show_requires,
			"verbose"  => \$verbose,
			"ignore=s" => \@ignores);
my %ignores = map { $_ => 1 } @ignores;

exit(1) if (not $result);

my $deps = new DependencyParser;
for my $file (grep /^[^-]/, @ARGV) {
  $deps->process_file($file);
}

if ($show_requires) {
  for my $req ($deps->requires) {
    my $verbage = "";
    next if (exists $ignores{$req->to_string});
    printf "%s%s\n", $req->to_string, $verbage;
  }
}

if ($show_provides) {
  for my $prov ($deps->provides) {
    my $verbage = "";
    next if (exists $ignores{$prov->to_string});
    printf "%s%s\n", $prov->to_string, $verbage;
  }
}

exit(0);

####################
# Dependency Class #
####################
package Dependency;
sub new {
  my $class = shift;
  my $type  = shift;
  my $value = shift;

  return bless { type => $type, value => $value }, $class;
}

sub value {
  my $self = shift;

  if (@_) {
    $self->{value} = shift;
  }

  return $self->{value};
}

sub filename {
  my $self = shift;

  if (@_) {
    $self->{filename} = shift;
  }

  return $self->{filename};
}

sub type {
  my $self = shift;

  if (@_) {
    $self->{type} = shift;
  }

  return $self->{type};
}

sub line_number {
  my $self = shift;

  if (@_) {
    $self->{line_number} = shift;
  }

  return $self->{line_number};
}

sub to_string {
  my $self = shift;
  my $type = $self->type;

  if ($type eq 'perl version') {
    # we need to convert a perl release version to an rpm package
    # version

    my $epoch = 0;
    my $version = $self->value;
    $version =~ s/_/./g;
    $version =~ s/0+$//;

    if ($version =~ /^5.00[1-5]/) {
      $epoch = 0;
    }
    elsif ($version =~ /^5.006/ or $version =~ /^5.6/) {
      $version =~ s/00//g;
      $epoch = 1;
    }
    elsif ($version =~ /^5.00[7-9]/ or $version =~ /^5.[7-9]/) {
      $version =~ s/00//g;
      $epoch = 2;
    }
    $version =~ s/\.$//;

    return sprintf "perl >= %d:%s", $epoch, $version;
  }
  elsif ($type eq 'virtual') { 
   	return $self->value; 
  }
  else {
    return sprintf "perl(%s)", $self->value;
  }
}

package DependencyParser;
sub new {
  my $class = shift;
  return bless {}, $class;
}

sub requires {
  return @{shift->{requires} || []};
}

sub provides {
  return @{shift->{provides} || []};
}

sub add_provide {
  my $self = shift;
  my %params = @_;
  die "DependencyParser->add_provide requires -filename, -provide, and -type"
    if not exists $params{-filename} or not exists $params{-provide} or not exists $params{-type};

  #
  # Make sure this one has not been added already
  $self->{'provides_check'} ||= { };
  return if(exists($self->{'provides_check'}->{$params{'-provide'}}));

  #
  # Skip some common false positives
  return if $params{'-provide'} eq 'main';

  #
  # Created dependency object
  my $dep = new Dependency "provide", $params{-provide};
  $dep->filename($params{-filename});
  $dep->type($params{-type});
  $dep->line_number($params{-line}) if $params{-line};

  #
  # Add to requires check list
  $self->{'provides_check'}->{$params{'-provide'}} = 1; 

  #
  # Add to list
  push @{$self->{provides}}, $dep;
}

sub add_require {
  my $self = shift;
  my %params = @_;
  die "DependencyParser->add_require requires -filename, -require, and -type"
    if not exists $params{-filename} or not exists $params{-require} or not exists $params{-type};

  #
  # Make sure this one has not been added already
  $self->{'requires_check'} ||= { };
  return if(exists($self->{'requires_check'}->{$params{'-require'}}));

  #
  # Create dependency object.
  my $dep = new Dependency "require", $params{-require};
  $dep->filename($params{-filename});
  $dep->type($params{-type});
  $dep->line_number($params{-line}) if $params{-line};

  #
  # Add to requires check list
  $self->{'requires_check'}->{$params{'-require'}} = 1; 

  #
  # Add to list
  push @{$self->{requires}}, $dep;
}

sub process_file {
  my $self     = shift;
  my $filename = shift;

  if (not open FH, "<$filename") {
    # XXX: Should be die IMHO...JOO
    warn "Can't open $filename: $!";
    return;
  }

  while (<FH>) {
    next if m(^=(head[1-4]|pod|item)) .. m(^=cut);
    next if m(^=over) .. m(^=back);
    last if m/^__(DATA|END)__$/;

    if (m/^\s*package\s+([\w\:]+)\s*;/) {
      $self->add_provide(-filename => $filename, -provide => $1, -type => "package", -line => $.);
    }
    if (m/^\s*use\s+base\s+(.*)/) {
      # recognize the three main forms: literal string, qw//, and
      # qw().  this is incomplete but largely sufficient.

      my @module_list;
      my $base_params = $1;

      if ($base_params =~ m[qw\((.*)\)]) {
	@module_list = split /\s+/, $1;
      }
      elsif ($base_params =~ m[qw/(.*)/]) {
	@module_list = split /\s+/, $1;
      }
      elsif ($base_params =~ m/(['"])(.*)\1/) { # close '] to unconfuse emacs cperl-mode
	@module_list = ($2);
      }

      $self->add_require(-filename => $filename, -require => $_, -type => "base", -line => $.)
	     for @module_list;
    }
    elsif (m/^\s*(use|require)\s+(v?[0-9\._]+)/) {
      $self->add_require(-filename => $filename, -require => $2, -type => "perl version", -line => $.);
    }
    elsif (m/^\s*use\s+([\w\:]+)/) {
      $self->add_require(-filename => $filename, -require => $1, -type => "use", -line => $.);
    }
    elsif (m/^require\s+([\w\:]+).*;/) {
      $self->add_require(-filename => $filename, -require => $1, -type => "require", -line => $.);
    } 
    #
    # Allow for old perl.req Requires.  Support:
    #
    #    $RPM_Requires = "x y z";
    #    our $RPM_Requires = "x y z";
    # 
    # where the rvalue is a space delimited list of provides.
    elsif (m/^\s*(our\s+)?\$RPM_Requires\s*=\s*["'](.*)['"]/) {
      foreach my $require (split(/\s+/, $2)) {
      	$self->add_require(
           -filename => $filename, 
           -require  => $require, 
           -type     => "virtual", 
           -line     => $.
        );
      }
    }
    #
    # Allow for old perl.req Provides.  Support:
    #
    #    $RPM_Provides = "x y z";
    #    our $RPM_Provides = "x y z";
    # 
    # where the rvalue is a space delimited list of provides.
    elsif ( m/^\s*(our\s+)?\$RPM_Provides\s*=\s*["'](.*)['"]/) {
      foreach my $provide (split(/\s+/, $2)) {
        $self->add_provide(
           -filename => $filename, 
           -provide  => $provide, 
           -type     => "virtual", 
           -line     => $.
        );
      }
    }
  }

  close(FH);
}

#######
# POD #
#######
__END__

=head1 NAME

perldeps.pl - Generate Dependency Sets For a Perl Script

=head1 SYNOPSIS

	perldeps.pl --provides [--verbose] 
		[--ignore=(dep) ... --ignore=(depN)]
	perldeps.pl --requires [--verbose] 
		[--ignore=(dep) ... --ignore=(depN)]

=head1 DESCRIPTION

This script examines a perl script or library and determines what the
set of provides and requires for that file.  Depending on whether you
use the C<--provides> or C<--requires> switch it will print either
the provides or requires it finds.  It will print each dependency 
on a seperate line simular to:

	perl(strict)
	perl(warnings)
	perl(Cmd)
	perl(Dbug)
	perl(Fdisk::Cmd)

This is the standard output that rpm expects from all of its autodependency
scripts.

Provides are determined by C<package> lines such as:

	package Great::Perl::Lib;

Additionally, a script can infrom C<perldeps.pl> that it has additional
provides by creating the variable C<$RPM_Provides>, and setting it to 
a space delimited list of provides.  For instance:

	$RPM_Provides = "great stuff";

Would tell C<perldeps.pl> that this script provides C<great> and C<stuff>.

Requires are picked up from several sources:

=over 4

=item *

C<use> lines.   These can define either libraries to use or the version
of perl required (see C<use> under C<perlfunc(1)).

=item *

C<require> lines.  Defines libraries to be sourced and evaled.

=item *

C<use base> lines.   These define base classes of the libraries and are 
thus dependencies.  It can parse the following forms:

	use base "somelib";
	use base qw(somelib otherlib);
	use base qw/somelib otherlib);

=back

Aditionally, you can define the variable C<$RPM_Requires> to define
additonal non-perl requirments.  For instance your script may require
sendmail, in which case might do:

	$RPM_Requires = "sendmail";

=head1 OPTIONS

=over 4

=item B<--provides>

Print all provides.

=item B<--requires>

Print all requires.

=item B<--ignore=(dep)>

Ignore this dependency if found.

=back

=head1 EXIT STATUS

0 success, 1 failure

=head1 SEE ALSO

/usr/lib/rpm/macros

=head1 BUGS

Does not generate version information on dependencies.  

=head1 AUTHOR

Chip Turner <cturner@redhat.com>