|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 package JarSegregator; |
|
18 |
|
19 use File::Path; |
|
20 use File::Copy; |
|
21 use Cwd; |
|
22 use XML::DOM; |
|
23 |
|
24 #Create new object |
|
25 sub new |
|
26 { |
|
27 my $class=shift @_; |
|
28 my $self={}; # New blank object |
|
29 bless ($self, $class); |
|
30 return $self; |
|
31 } |
|
32 |
|
33 #internal function |
|
34 # ======================================================================= |
|
35 # |
|
36 # Recursive copy |
|
37 # |
|
38 # Copies a folder, its sub folders & files therein. |
|
39 # Paramter 1: Source folder path. |
|
40 # Paramter 2: Destination folder path. |
|
41 # return 0 for success |
|
42 # ======================================================================= |
|
43 sub CopyTree |
|
44 # ======================================================================= |
|
45 { |
|
46 use File::Path; |
|
47 use File::Find; |
|
48 use Cwd 'abs_path'; |
|
49 |
|
50 my $CopyFrom = shift @_; |
|
51 my $CopyTo = shift @_; |
|
52 if(-e $CopyFrom) |
|
53 { |
|
54 $CopyFrom = abs_path($CopyFrom); |
|
55 $CopyTo = abs_path($CopyTo); |
|
56 } |
|
57 my ($src,$RelativePath,$dest); |
|
58 |
|
59 # Make sure that the source directory exists, create the top level |
|
60 # destination directory |
|
61 if (-d $CopyFrom) |
|
62 { |
|
63 eval { mkpath($CopyTo) }; |
|
64 if ($@) |
|
65 { |
|
66 print("WARN: Couldn't create $_[0]: $@"); |
|
67 #return -1; |
|
68 } |
|
69 } else { |
|
70 print ("WARN: Directory $CopyFrom does not exist"); |
|
71 #return -2; |
|
72 } |
|
73 |
|
74 print("\n CopyTree: Copying $CopyFrom -> $CopyTo"); |
|
75 |
|
76 # Traverse file tree (highest level directory first) |
|
77 find(sub{ |
|
78 $src=$File::Find::name; |
|
79 $RelativePath=$src; |
|
80 $RelativePath=~s/^\Q$CopyFrom\E//; |
|
81 $dest=$CopyTo.$RelativePath; |
|
82 if (-f $src) |
|
83 { # Copy file |
|
84 if (!copy($src,$dest)) |
|
85 { |
|
86 print ("ERROR: Couldn't copy $src: $@"); |
|
87 } |
|
88 } else { # Make a duplicate directory if necessary |
|
89 unless($dest eq '..'||$dest eq '.') { |
|
90 unless(-e $dest) { |
|
91 # mkdir($dest,0775); |
|
92 mkpath($dest); |
|
93 } |
|
94 } |
|
95 } |
|
96 },$CopyFrom); |
|
97 return 0; |
|
98 } |
|
99 |
|
100 #This subroutine prepares the environment |
|
101 #First parameter is the log file handle (optional) |
|
102 #Second parameter is the j9 file name taken from sdk.ini file |
|
103 #Third parameter is the j9 path name taken from sdk.ini file |
|
104 #Fourth parameter is the temporary work area for this jars |
|
105 sub Prepare_env |
|
106 { |
|
107 my $self=shift @_; # class name passed by default |
|
108 my $j9filename = shift @_; |
|
109 my $j9path = shift @_; |
|
110 my $jsr_wa = shift @_; |
|
111 my $cmd; |
|
112 my @out; |
|
113 my $filename=""; |
|
114 print "\n\n--------------Preparing J9 Extraction Env--------------\n\n"; |
|
115 $filename = "$j9filename"; |
|
116 # if directory exists it deletes it |
|
117 if(-d $filename) |
|
118 { |
|
119 rmtree($filename); |
|
120 } |
|
121 #copies the j9 file from the platform to the temp work area |
|
122 if(-f "$j9path\\$filename.zip") |
|
123 { |
|
124 copy ("$j9path\\$filename.zip", "$jsr_wa\\$filename.zip"); |
|
125 } |
|
126 eval{ |
|
127 $cmd = "unzip -o $jsr_wa\\$filename.zip -d $jsr_wa\\$filename/"; |
|
128 @out = `$cmd`; |
|
129 }; |
|
130 if ($@) |
|
131 { |
|
132 print "\nERROR: $cmd is failed: $@"; |
|
133 return 1; |
|
134 } |
|
135 else |
|
136 { |
|
137 print @out; |
|
138 } |
|
139 } |
|
140 |
|
141 #This subroutine segregates the jars using the jsr_specification.xml |
|
142 #First parameter is the log file handle (optional) |
|
143 #Second parameter is the temporary work area for this jars |
|
144 #Third parameter is the hash reference to the information retrieved from jsr_spec.xml |
|
145 #Fourth parameter is the j9 file name taken from sdk.ini file |
|
146 sub segregation |
|
147 { |
|
148 my $self=shift @_; # class name passed by default |
|
149 my $temp_path = shift @_; |
|
150 my ($href) = shift @_; |
|
151 my $j9filename = shift @_; |
|
152 my $api=""; |
|
153 my $packagename=""; |
|
154 my @packagename=""; |
|
155 my $packagepath=""; |
|
156 print "\n\n--------------Segregating the JSRs --------------\n\n"; |
|
157 mkpath ("$temp_path"); |
|
158 foreach $api(keys (%$href)) |
|
159 { |
|
160 $packagelist = %$href->{$api}->{'package'}; |
|
161 (@packagenames) = split(/\,/,$packagelist); |
|
162 #copies the packages from the source path to the temp work area |
|
163 foreach $packagenames(@packagenames) |
|
164 { |
|
165 $packagenames =~ s/\./\\/g; |
|
166 $packagepath = $packagenames; |
|
167 $ch = getcwd; |
|
168 chdir "$temp_path" or die ("\nERROR: Can not chdir to $temp_path. $!"); |
|
169 mkpath ("$api\\$packagepath"); |
|
170 chdir "$ch" or die ("\nERROR: Can not chdir to $ch. $!"); |
|
171 if(($api=~m/midp20/g) or ($api=~m/midp21/g) or (($api=~m/jsr139/g))) |
|
172 { |
|
173 if($packagepath =~ m/javax\\microedition\\io/gi) |
|
174 { |
|
175 print "Package path : $packagepath\n"; |
|
176 system ("copy $j9filename\\$packagepath $temp_path\\$api\\$packagepath"); |
|
177 next; |
|
178 } |
|
179 } |
|
180 $cmd = CopyTree("$j9filename\\$packagepath","$temp_path\\$api\\$packagepath"); |
|
181 if($cmd!=0) |
|
182 { |
|
183 print "\n ERROR: $j9filename\\$packagepath is NOT copied to $temp_path\\$api\\$packagepath. $cmd"; |
|
184 } |
|
185 print $cmd; |
|
186 } |
|
187 } |
|
188 } |
|
189 |
|
190 #This subroutine generates the manifest file for each jsr mentioned in jsr_specification.xml |
|
191 #First parameter is the log file handle (optional) |
|
192 #Second parameter is the temporary work area for this jars |
|
193 #Third parameter is the hash reference to the information retrieved from jsr_spec.xml |
|
194 |
|
195 sub manifest_file_process |
|
196 { |
|
197 my $self=shift @_; # class name passed by default |
|
198 my $temp_path = shift @_; |
|
199 my ($href) = shift @_; |
|
200 my $api; |
|
201 my $manifilename = "manifest.mf"; |
|
202 my @manifestinfo =""; |
|
203 open OUT_TEMP,">Temp_log.txt"; |
|
204 print "\n\n--------------Manifest file process--------------\n\n"; |
|
205 open OUTMANIFEST,">$manifilename"; |
|
206 foreach $api(keys (%$href)) |
|
207 { |
|
208 foreach $manifest_info(@{%$href->{$api}->{'manifest_info'}}) |
|
209 { |
|
210 $manifest_info =~ s/^\n//; |
|
211 #$manifest_info =~ s/\n$//; |
|
212 if($manifest_info =~ m/API-Type/) |
|
213 { |
|
214 push (@manifestinfo,$manifest_info); |
|
215 print OUT_TEMP $manifest_info; |
|
216 print OUTMANIFEST @manifestinfo; |
|
217 @manifestinfo=""; |
|
218 print OUTMANIFEST "\n"; |
|
219 close OUTMANIFEST; |
|
220 mkpath ("$temp_path\\$api\\META-INF"); |
|
221 $cmd = copy ("$manifilename", "$temp_path\\$api\\META-INF\\$manifilename"); |
|
222 if($cmd!=0) |
|
223 { |
|
224 print "\nCopied $manifilename to $temp_path\\$api\\META-INF\\$manifilename"; |
|
225 } |
|
226 open OUTMANIFEST,">$manifilename"; |
|
227 } |
|
228 else{ |
|
229 push (@manifestinfo,$manifest_info); |
|
230 print OUT_TEMP $manifest_info; |
|
231 } |
|
232 } |
|
233 } |
|
234 close OUTMANIFEST; |
|
235 close OUT_TEMP; |
|
236 $cmd = unlink "$manifilename" or die ("\nERROR: Can not delete $manifilename, $!"); |
|
237 print $cmd; |
|
238 } |
|
239 |
|
240 #This subroutine generates the jars for each jsr and copies to target directory |
|
241 #First parameter is the log file handle (optional) |
|
242 #Second parameter is the temporary work area for this jars |
|
243 #Third parameter is the target directory of the jar files to be copied |
|
244 sub jar_files |
|
245 { |
|
246 my $self=shift @_; # class name passed by default |
|
247 my $temp_path = shift @_; |
|
248 my $target_jar = shift @_; |
|
249 my $dirname=""; |
|
250 my @file=""; |
|
251 my $file=""; |
|
252 my $api_name=""; |
|
253 my $cwd=""; |
|
254 print "\n\n--------------Creating Jars --------------\n\n"; |
|
255 if(-d "$target_jar") |
|
256 { |
|
257 rmtree($target_jar); |
|
258 } |
|
259 mkpath ("$target_jar"); |
|
260 $dirname="$temp_path\\"; |
|
261 opendir DIR, $dirname or print "\n WARNING: Can not open $dirname, $!"; |
|
262 @file = readdir DIR; |
|
263 shift @file; |
|
264 shift @file; |
|
265 foreach $file(@file) |
|
266 { |
|
267 $cwd = getcwd; |
|
268 $api_name = $file; |
|
269 chdir "$temp_path\\$api_name" or die ("\nERROR: Can not chdir to $temp_path\\$api_name. $!"); |
|
270 eval{ |
|
271 $cmd = "jar cvfM $file.jar ./"; |
|
272 print "\n INFO: EXE $cmd \n"; |
|
273 @cmd = `$cmd`; |
|
274 }; |
|
275 if ($@) |
|
276 { |
|
277 print "\nERROR: $cmd is failed: $@"; |
|
278 return 1; |
|
279 } |
|
280 print @cmd; |
|
281 $cmd = copy ("$file.jar","$target_jar"); |
|
282 if($cmd!=0) |
|
283 { |
|
284 print "\nCopied $file.jar to $target_jar\n" ; |
|
285 } |
|
286 @cmd = unlink "$file.jar" or die ("\nERROR: Can not delete $file.jar, $!"); |
|
287 print @cmd; |
|
288 chdir $cwd or die ("\nERROR: Can not chdir to $cwd. $!"); |
|
289 } |
|
290 closedir DIR; |
|
291 } |
|
292 |
|
293 #This subroutine parses the jsr_specification.xml and stores in hash ref |
|
294 #First parameter is the log file handle (optional) |
|
295 #Second parameter is the jsr_specification file name |
|
296 #Third parameter is the hash ref for the jsr information |
|
297 sub parse_xml_file |
|
298 { |
|
299 my $self=shift @_; # class name passed by default |
|
300 my $file = shift @_; |
|
301 my ($href) = shift @_; |
|
302 my $parser = new XML::DOM::Parser; |
|
303 my $doc = $parser->parsefile($file); |
|
304 my $apiname; |
|
305 my @symbol_nodes; |
|
306 my $symbol_node; |
|
307 my $symbol; |
|
308 my @manifest_info; |
|
309 |
|
310 foreach my $jsr ($doc->getElementsByTagName("jsr")) { |
|
311 @symbol_nodes = $jsr->getElementsByTagName("api-name"); |
|
312 $symbol_node = $symbol_nodes[0]; |
|
313 $symbol = $symbol_node->getFirstChild->getData(); |
|
314 print "\n".$symbol."\n"; |
|
315 $apiname = $symbol; |
|
316 |
|
317 @symbol_nodes = $jsr->getElementsByTagName("package"); |
|
318 $symbol_node = $symbol_nodes[0]; |
|
319 $symbol = $symbol_node->getFirstChild->getData(); |
|
320 $symbol =~ s/\s+/\,/g; |
|
321 print $symbol; |
|
322 |
|
323 $href->{$apiname}->{'package'} = $symbol; |
|
324 print $href->{$apiname}->{'package'}; |
|
325 print "\n"; |
|
326 |
|
327 @symbol_nodes = $jsr->getElementsByTagName("manifest_info"); |
|
328 $symbol_node = $symbol_nodes[0]; |
|
329 $symbol = $symbol_node->getFirstChild->getData(); |
|
330 @symbol = split(/\n/,$symbol); |
|
331 foreach $symbol(@symbol) |
|
332 { |
|
333 $symbol =~ s/^\s+//g; |
|
334 $symbol =~ s/\s+$//g; |
|
335 push @{$href->{$apiname}->{'manifest_info'}}, "$symbol\n"; |
|
336 } |
|
337 print @{$href->{$apiname}->{'manifest_info'}}; |
|
338 print "\n"; |
|
339 } |
|
340 } |
|
341 |
|
342 |
|
343 # This subroutine is called from the build script from jsr_jar_segregation |
|
344 # Second Parameter is the target destination directory |
|
345 # Third Parameter is the hash reference for sdk.ini file |
|
346 # Fourth Parameter is the log file directory as "phase" |
|
347 sub jar_segregation |
|
348 { |
|
349 my $self=shift @_; # class name passed by default |
|
350 my $target_jar=shift @_; |
|
351 my ($href) = shift @_; |
|
352 |
|
353 my $j9filename =$$href{Jsr_j9filename}; |
|
354 my $j9path=$$href{j9_filepath}; |
|
355 my $synergypath=$$href{jsr_spec_cm_path}; |
|
356 my $xml_file=$$href{jsr_specfile}; |
|
357 my $jsr_wa = $$href{jsr_wa}; |
|
358 |
|
359 my $temp_path ="$jsr_wa\\new_jars"; |
|
360 my %jsr_info=""; |
|
361 if(!(-d $jsr_wa)) |
|
362 { |
|
363 mkpath ("$jsr_wa"); |
|
364 } |
|
365 $ch = getcwd; |
|
366 $self->parse_xml_file($xml_file,\%jsr_info); |
|
367 chdir "$jsr_wa" or die ("\nERROR: Can not chdir to $jsr_wa. $!"); |
|
368 $self->Prepare_env($j9filename,$j9path,$jsr_wa); |
|
369 $self->segregation($temp_path,\%jsr_info,$j9filename); |
|
370 $self->manifest_file_process($temp_path,\%jsr_info); |
|
371 $self->jar_files($temp_path,$target_jar); |
|
372 chdir "$ch" or die ("\nERROR: Can not chdir to $ch. $!"); |
|
373 } |
|
374 |
|
375 return 1; |