summaryrefslogtreecommitdiff
path: root/scripts/osgideps.pl
blob: 428c9080c1600d91f16e7a5b844b38cead54404a (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
#!/usr/bin/perl


use Cwd;
use Getopt::Long;


$cdir = getcwd();
$TEMPDIR="/tmp";
$MANIFEST_NAME="META-INF/MANIFEST.MF";


# prepare temporary directory
if (! (-d $TEMPDIR)) {
        if (($_ = `mkdir $TEMPDIR`) != 0) {exit 1;}
        elsif (! (-w $TEMPDIR) && (-x $TEMPDIR)) {exit 1;}
}

# parse options
my ($show_provides, $show_requires);

my $result = GetOptions("provides" => \$show_provides,
			"requires" => \$show_requires);

exit(1) if (not $result);



@allfiles = <STDIN>;

if ($show_provides) {
	do_provides(@allfiles);
}

if ($show_requires) {
	do_requires(@allfiles);
}


exit(0);



sub do_provides {


foreach $jar (@_) {

next if -f $jar && -r $jar;
 $jar =~ s/[^[:print:]]//g;
        # if this jar contains MANIFEST.MF file
        if (`jar tf $jar | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n") {
                # extract MANIFEST.MF file from jar to temporary directory
                chdir $TEMPDIR;
                `jar xf $cdir/$jar $MANIFEST_NAME`;
                open(MANIFEST, "$MANIFEST_NAME");
                my $bundleName = "";
                my $bundleVersion = "";
                # parse bundle name and version
                while(<MANIFEST>) {
                        # get rid of non-print chars (some manifest files contain weird chars
                        s/[^[:print]]//g;
                        if (m/(^Bundle-SymbolicName: )((\w|\.)+)(\;*)(.*\n)/) {
                                $bundleName = $2;
                        }
                        if (m/(^Bundle-Version: )(.*)/) {
                                $bundleVersion = $2;
                        }
                }
                # skip this jar if no bundle name exists
                if (! $bundleName eq "") {
                        if (! $bundleVersion eq "") {
                                print "osgi(".$bundleName.") = ".$bundleVersion."\n";
                        } else {
                                print "osgi(".$bundleName.")\n";
                        }
                }
                chdir $cdir;
        }
	
}

}


sub do_requires {

foreach $jar (@_) {
next if -f $jar && -r $jar;
$jar =~ s/[^[:print:]]//g;
        if (`jar tf $jar | grep -e \^$MANIFEST_NAME` eq "$MANIFEST_NAME\n") {
                chdir $TEMPDIR;
                `jar xf $cdir/$jar $MANIFEST_NAME`;
                open(MANIFEST, "$MANIFEST_NAME") or die;
                my %reqcomp = ();
                while(<MANIFEST>) {
                        if (m/(^(Require-Bundle|Import-Package): )(.*)$/) {
                                my $reqlist = "$3"."\n";
                                while(<MANIFEST>) {
                                        if (m/^[[:upper:]][[:alpha:]]+-[[:upper:]][[:alpha:]]+: .*/) {
                                                $len = length $_;
                                                seek MANIFEST, $len*-1 , 1;
                                                last;
                                        }
                                        $reqlist.="$_";
                                }
                                push @requirelist,  parseReqString($reqlist);
                        }

                }
                chdir $cdir;
	}

}

$list = "";
for $require (@requirelist) {
        $list .= "osgi(".$require->{NAME}.")".$require->{VERSION}."\n";
}
#$abc = `echo \"$list\"|grep -e \^osgi\\(.*\\)| sort|uniq`;
print $list;

}

sub parseReqString {
        my $reqstr = $_[0];
        my @return;
        $reqstr =~ s/ //g;
        $reqstr =~ s/\n//g;
        $reqstr =~ s/[^[:print:]]//g;
        $reqstr =~ s/("[[:alnum:]|\-|\_|\.|\(|\)|\[|\]]+)(,)([[:alnum:]|\-|\_|\.|\(|\)|\[|\]]+")/$1 $3/g;
        @reqcomp = split /,/g, $reqstr;
        foreach $reqelement (@reqcomp) {
                @reqelementfrmnt = split /;/g, $reqelement;
                $name="";
                $version="";
                $name = $reqelementfrmnt[0];
                for $i (1 .. $#reqelementfrmnt) {
                        if ($reqelementfrmnt[$i] =~ m/(^(bundle-|)version=")(.*)(")/){
                                $version = $3;
                                last;
                        }
                }
                $version = parseVersion($version);
                push @return, { NAME=>"$name", VERSION=>"$version"};
        }

        return @return;
}

sub parseVersion {
        my $ver = $_[0];
        if ($ver eq "") { return "";}
        if ($ver =~ m/(^[\[|\(])(.+)\ (.+)([\]|\)]$)/) {
                ($1 eq "\[") ? return " <= $2" : return " < $2";
        } else {
                return " = $ver";
        }
        return $ver;
}