Ticket #101: osgideps.pl_2009-08-25.patch

File osgideps.pl_2009-08-25.patch, 11.9 kB (added by alcapcom, 3 years ago)
  • scripts/osgideps.pl

    old new  
    22# 
    33# osgideps.pl -- Analyze dependencies of OSGi bundles. 
    44# 
    5 # Kyu Lee 
    6 # Alphonse Van Assche <alcapcom@fedoraproject.org> 
     5# Kyu Lee (initial idea) 
     6# Alphonse Van Assche <alcapcom@fedoraproject.org> (current maintainer) 
    77# 
    88# $Id: osgideps.pl,v 1.0 2009/06/08 12:12:12 mej Exp $ 
    9 # 
    109 
    11 use Cwd; 
    1210use Getopt::Long; 
    1311use File::Temp qw/ tempdir /; 
     12use threads; 
     13use Thread::Queue; 
    1414 
    15 $cdir = getcwd(); 
    16 $TEMPDIR = tempdir( CLEANUP => 1 ); 
    1715$MANIFEST_NAME = "META-INF/MANIFEST.MF"; 
    1816 
    19 # prepare temporary directory 
    20 if ( !( -d $TEMPDIR ) ) { 
    21         if ( ( $_ = `mkdir $TEMPDIR` ) != 0 ) { exit 1; } 
    22         elsif ( !( -w $TEMPDIR ) && ( -x $TEMPDIR ) ) { exit 1; } 
    23 } 
    24  
    2517# parse options 
    2618my ( $show_provides, $show_requires, $show_system_bundles, $debug ); 
    2719my $result = GetOptions( 
     
    4739 
    4840# this function print provides of OSGi aware files 
    4941sub getProvides { 
     42         
     43        my $queue = Thread::Queue->new; 
    5044        foreach $file (@_) { 
    51                 chomp($file); 
    52                 # we don't follow symlinks for provides 
    53                 next if -f $file && -r $file && -l $file; 
    54                 $file =~ s/[^[:print:]]//g; 
    55                 if ( $file =~ m/$MANIFEST_NAME$/ || $file =~ m/\.jar$/ ) { 
    56                         if ( $file =~ m/\.jar$/ ) { 
    57                                 if ( `jar tf $file | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n" ) { 
    58                                         # extract MANIFEST.MF file from jar to temporary directory 
    59                                         chdir $TEMPDIR; 
    60                                         `jar xf $file $MANIFEST_NAME`; 
    61                                         open( MANIFEST, "$MANIFEST_NAME" ); 
    62                                         chdir $cdir; 
     45                $queue->enqueue($file); 
     46        } 
     47 
     48        my @workers; 
     49        push @workers, threads->create('getProvidesWorker'); 
     50        push @workers, threads->create('getProvidesWorker'); 
     51        push @workers, threads->create('getProvidesWorker'); 
     52        push @workers, threads->create('getProvidesWorker'); 
     53 
     54        map { $_->join } @workers; 
     55         
     56        sub getProvidesWorker { 
     57                while ( my $file = $queue->dequeue_nb ) { 
     58                        chomp($file); 
     59                        # we don't follow symlinks for provides 
     60                        next if ( -f $file && -r $file && -l $file ); 
     61                        $file =~ s/[^[:print:]]//g; 
     62                        if ( $file =~ m/$MANIFEST_NAME$/ || $file =~ m/\.jar$/ ) { 
     63                                if ( $file =~ m/\.jar$/ ) { 
     64                                        if ( `zipinfo -1 $file 2> /dev/null | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n" ) { 
     65                                                # extract MANIFEST.MF file from jar to temporary directory 
     66                                                $tmpdir = tempdir( CLEANUP => 1 );                                               
     67                                                `unzip -d $tmpdir -qqo $file $MANIFEST_NAME`; 
     68                                                open( MANIFEST, "$tmpdir/$MANIFEST_NAME" ); 
     69                                        } 
     70                                } else { 
     71                                        open( MANIFEST, "$file" ); 
    6372                                } 
    64                         } else { 
    65                                 open( MANIFEST, "$file" ); 
    66                         } 
    67                         my $bundleName = ""; 
    68                         my $version = ""; 
    69                         # parse Bundle-SymbolicName, Bundle-Version and Export-Package attributes 
    70                         while (<MANIFEST>) { 
    71                                 # get rid of non-print chars (some manifest files contain weird chars) 
    72                                 s/[^[:print]]//g; 
    73                                 if ( m/(^(Bundle-SymbolicName): )(.*)$/ ) { 
    74                                         $bundleName = "$3" . "\n"; 
    75                                         while (<MANIFEST>) { 
    76                                                 if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
    77                                                         $len = length $_; 
    78                                                         seek MANIFEST, $len * -1, 1; 
    79                                                         last; 
     73                                my $bundleName = ""; 
     74                                my $version = ""; 
     75                                # parse Bundle-SymbolicName, Bundle-Version and Export-Package attributes 
     76                                while (<MANIFEST>) { 
     77                                        # get rid of non-print chars (some manifest files contain weird chars) 
     78                                        s/[^[:print]]//g; 
     79                                        if ( m/(^(Bundle-SymbolicName): )(.*)$/ ) { 
     80                                                $bundleName = "$3" . "\n"; 
     81                                                while (<MANIFEST>) { 
     82                                                        if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
     83                                                                $len = length $_; 
     84                                                                seek MANIFEST, $len * -1, 1; 
     85                                                                last; 
     86                                                        } 
     87                                                        $bundleName .= "$_"; 
    8088                                                } 
    81                                                 $bundleName .= "$_"; 
     89                                                $bundleName =~ s/\s+//g; 
     90                                                $bundleName =~ s/;.*//g; 
    8291                                        } 
    83                                         $bundleName =~ s/\s+//g; 
    84                                         $bundleName =~ s/;.*//g; 
    85                                 } 
    86                                 if ( m/(^Bundle-Version: )(.*)/ ) { 
    87                                         $version = $2; 
    88                                 } 
    89                                 if ( m/(^(Export-Package): )(.*)$/ ) { 
    90                                         my $bunlist = "$3" . "\n"; 
    91                                         while (<MANIFEST>) { 
    92                                                 if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
    93                                                         $len = length $_; 
    94                                                         seek MANIFEST, $len * -1, 1; 
    95                                                         last; 
     92                                        if ( m/(^Bundle-Version: )(.*)/ ) { 
     93                                                $version = $2; 
     94                                        } 
     95                                        if ( m/(^(Export-Package): )(.*)$/ ) { 
     96                                                my $bunlist = "$3" . "\n"; 
     97                                                while (<MANIFEST>) { 
     98                                                        if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
     99                                                                $len = length $_; 
     100                                                                seek MANIFEST, $len * -1, 1; 
     101                                                                last; 
     102                                                        } 
     103                                                        $bunlist .= "$_"; 
    96104                                                } 
    97                                                 $bunlist .= "$_"; 
     105                                                push @bundlelist, parsePkgString($bunlist, $file); 
     106                                        } 
     107                                }        
     108         
     109                                # skip this jar if no bundle name exists 
     110                                if ( !$bundleName eq "" ) { 
     111                                        if ( !$version eq "" ) { 
     112                                                $version = parseVersion($version); 
     113                                                push @bundlelist, { FILE => "$file", NAME => "$bundleName", VERSION => "$version" }; 
     114                                        } else {         
     115                                                push @bundlelist, { FILE => "$file", NAME => "$bundleName", VERSION => "" }; 
    98116                                        } 
    99                                         push @bundlelist, parsePkgString($bunlist, $file); 
    100                                 } 
    101                         } 
    102  
    103                         # skip this jar if no bundle name exists 
    104                         if ( !$bundleName eq "" ) { 
    105                                 if ( !$version eq "" ) { 
    106                                         $version = parseVersion($version); 
    107                                         push @bundlelist, { FILE => "$file", NAME => "$bundleName", VERSION => "$version" }; 
    108                                 } else { 
    109                                         push @bundlelist, { FILE => "$file", NAME => "$bundleName", VERSION => "" }; 
    110117                                } 
     118                                `rm -rf $tmpdir`; 
    111119                        } 
    112120                } 
    113        
    114         if ( !$debug ) { @bundlelist = prepareOSGiBundlesList(@bundlelist); } 
    115         $list = ""; 
    116         for $bundle (@bundlelist) { 
    117                 if ( !$debug ) { 
    118                         $list .= "osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
    119                 } else { 
    120                         $list .= $bundle->{FILE} . " osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     121               if ( !$debug ) { @bundlelist = prepareOSGiBundlesList(@bundlelist);
     122               $list = ""; 
     123               for $bundle (@bundlelist) { 
     124                       if ( !$debug ) { 
     125                               $list .= "osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     126                        } else { 
     127                               $list .= $bundle->{FILE} . " osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     128                        } 
    121129                } 
     130                print $list; 
    122131        } 
    123         print $list; 
    124132} 
    125133 
    126134# this function print requires of OSGi aware files 
    127135sub getRequires { 
     136 
     137        my $queue = Thread::Queue->new; 
    128138        foreach $file (@_) { 
    129                 next if (-f $file && -r $file); 
    130                 # we explicitly requires symlinked jars 
    131                 if (-l $file) { 
    132                         $file = readlink $file; 
    133                         if ( !$file eq "" ) {                                                    
    134                                 print "$file" . "\n"; 
    135                         } 
    136                         next; 
    137                 }  
    138                 $file =~ s/[^[:print:]]//g; 
    139                 if ( $file =~ m/$MANIFEST_NAME$/ || $file =~ m/\.jar$/ ) { 
    140                         if ( $file =~ m/\.jar$/ ) { 
    141                                 if ( `jar tf $file | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n" ) { 
    142                                         # extract MANIFEST.MF file from jar to temporary directory 
    143                                         chdir $TEMPDIR; 
    144                                         `jar xf $file $MANIFEST_NAME`; 
    145                                         open( MANIFEST, "$MANIFEST_NAME" ); 
    146                                         chdir $cdir; 
    147                                 } 
    148                         } else { 
    149                                 open( MANIFEST, "$file" ); 
    150                         } 
    151                         while (<MANIFEST>) { 
    152                                 if ( m/(^(Require-Bundle|Import-Package): )(.*)$/ ) { 
    153                                         my $bunlist = "$3" . "\n"; 
    154                                         while (<MANIFEST>) { 
    155                                                 if (m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
    156                                                         $len = length $_; 
    157                                                         seek MANIFEST, $len * -1, 1; 
     139                $queue->enqueue($file); 
     140        } 
     141 
     142        my @workers; 
     143        push @workers, threads->create('getRequiresWorker'); 
     144        push @workers, threads->create('getRequiresWorker'); 
     145        push @workers, threads->create('getRequiresWorker'); 
     146        push @workers, threads->create('getRequiresWorker'); 
     147 
     148        map { $_->join } @workers; 
     149         
     150        sub getRequiresWorker { 
     151                while ( my $file = $queue->dequeue_nb ) { 
     152                        next if ( -f $file && -r $file ); 
     153                        $file =~ s/[^[:print:]]//g; 
     154                        if ( $file =~ m/$MANIFEST_NAME$/ || $file =~ m/\.jar$/ ) { 
     155                                # we explicitly requires symlinked jars 
     156                                # _that_reside_outside_the_package_ 
     157                                if (-l $file) { 
     158                                        $exist = 0; 
     159                                        $lnksrc = `readlink -qen $file`; 
     160                                        foreach $exfile ( @allfiles ) { 
     161                                                $exfile =~ s/[^[:print:]]//g; 
     162                                                if ( $lnksrc =~ m/$exfile$/ ) {                                                  
     163                                                        $exist = 1; 
    158164                                                        last; 
    159165                                                } 
    160                                                 $bunlist .= "$_"; 
    161166                                        } 
    162                                         push @bundlelist, parsePkgString($bunlist, $file); 
     167                                        print "$lnksrc\n" if (!$exist); 
     168                                        next; 
     169                                }         
     170                                 
     171                                if ( $file =~ m/\.jar$/ ) { 
     172                                        if ( `zipinfo -1 $file 2> /dev/null | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n" ) { 
     173                                                # extract MANIFEST.MF file from jar to temporary directory 
     174                                                $tmpdir = tempdir( CLEANUP => 1 ); 
     175                                                `unzip -d $tmpdir -qqo $file $MANIFEST_NAME`; 
     176                                                open( MANIFEST, "$tmpdir/$MANIFEST_NAME" ); 
     177                                        } 
     178                                } else { 
     179                                        open( MANIFEST, "$file" ); 
    163180                                } 
    164                                 # we also explicitly require symlinked jars define by  
    165                                 # Bundle-ClassPath attribut 
    166                                 if ( m/(^(Bundle-ClassPath): )(.*)$/ ) { 
    167                                         $bunclp = "$3" . "\n"; 
    168                                         while (<MANIFEST>) { 
    169                                                 if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
    170                                                         $len = length $_; 
    171                                                         seek MANIFEST, $len * -1, 1; 
    172                                                         last; 
     181                                while (<MANIFEST>) { 
     182                                        if ( m/(^(Require-Bundle|Import-Package): )(.*)$/ ) { 
     183                                                my $bunlist = "$3" . "\n"; 
     184                                                while (<MANIFEST>) { 
     185                                                        if (m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
     186                                                                $len = length $_; 
     187                                                                seek MANIFEST, $len * -1, 1; 
     188                                                                last; 
     189                                                        } 
     190                                                        $bunlist .= "$_"; 
    173191                                                } 
    174                                                 $bunclp .= "$_"
     192                                                push @bundlelist, parsePkgString($bunlist, $file)
    175193                                        } 
    176                                         $bunclp =~ s/\ //g; 
    177                                         $bunclp =~ s/\n//g; 
    178                                         $bunclp =~ s/[^[:print:]]//g; 
    179                                         $dir = `dirname $file`; 
    180                                         $dir =~ s/\n//g; 
    181                                         @jars = split /,/, $bunclp; 
    182                                         for $jarfile (@jars) { 
    183                                                 $jarfile = "$dir\/\.\.\/$jarfile"; 
    184                                                 $jarfile = readlink $jarfile; 
    185                                                 if ( !$jarfile eq "" ) {                                                         
    186                                                         print "$jarfile" . "\n"; 
     194                                        # we also explicitly require symlinked jars define by  
     195                                        # Bundle-ClassPath attribut 
     196                                        if ( m/(^(Bundle-ClassPath): )(.*)$/ ) { 
     197                                                $bunclp = "$3" . "\n"; 
     198                                                while (<MANIFEST>) { 
     199                                                        if ( m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/ ) { 
     200                                                                $len = length $_; 
     201                                                                seek MANIFEST, $len * -1, 1; 
     202                                                                last; 
     203                                                        } 
     204                                                        $bunclp .= "$_"; 
     205                                                } 
     206                                                $bunclp =~ s/\ //g; 
     207                                                $bunclp =~ s/\n//g; 
     208                                                $bunclp =~ s/[^[:print:]]//g; 
     209                                                $dir = `dirname $file`; 
     210                                                $dir =~ s/\n//g; 
     211                                                @jars = split /,/, $bunclp; 
     212                                                for $jarfile (@jars) { 
     213                                                        $jarfile = "$dir\/\.\.\/$jarfile"; 
     214                                                        $jarfile = readlink $jarfile; 
     215                                                        if ( !$jarfile eq "" ) {                                                         
     216                                                                print "$jarfile" . "\n"; 
     217                                                        } 
    187218                                                } 
    188219                                        } 
    189220                                } 
     221                                `rm -rf $tmpdir`; 
    190222                        } 
    191223                } 
    192        
    193         if ( !$debug ) { @bundlelist = prepareOSGiBundlesList(@bundlelist); } 
    194         $list = ""; 
    195         for $bundle (@bundlelist) { 
    196                 # replace '=' by '>=' because qualifiers are set on provides  
    197                 # but not on requires. 
    198                 $bundle->{VERSION} =~ s/\ =/\ >=/g; 
    199                 if ( !$debug ) { 
    200                         $list .= "osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
    201                 } else { 
    202                         $list .= $bundle->{FILE} . " osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     224               if ( !$debug ) { @bundlelist = prepareOSGiBundlesList(@bundlelist);
     225               $list = ""; 
     226               for $bundle (@bundlelist) { 
     227                       # replace '=' by '>=' because qualifiers are set on provides  
     228                       # but not on requires. 
     229                       $bundle->{VERSION} =~ s/\ =/\ >=/g; 
     230                       if ( !$debug ) { 
     231                               $list .= "osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     232                        } else { 
     233                               $list .= $bundle->{FILE} . " osgi(" . $bundle->{NAME} . ")" . $bundle->{VERSION} . "\n"; 
     234                        } 
    203235                } 
     236                print $list; 
    204237        } 
    205         print $list; 
    206238} 
    207239 
    208240# this function print system bundles of OSGi profile files. 
    209241sub getSystemBundles { 
    210242        foreach $file (@_) { 
    211                 if ( -r $file && -r $file ) { 
     243                if ( -f $file && -r $file ) { 
    212244                        print "'$file' file not found or cannot be read!"; 
    213245                        next; 
    214246                } else { 
     
    261293                $version = ""; 
    262294                $name = $reqelementfrmnt[0]; 
    263295                $name =~ s/\"//g; 
    264                 # ignore 'system.bundle'. 
    265                 # see http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.platform.doc.isv/porting/3.3/incompatibilities.html 
     296                # ignoring OSGi 'system.bundle'  
    266297                next if ( $name =~ m/^system\.bundle$/ ); 
    267298                for $i ( 1 .. $#reqelementfrmnt ) { 
    268299                        if ( $reqelementfrmnt[$i] =~ m/(^(bundle-|)version=")(.*)(")/ ) {