metatools/sysdeftools/checklinks.pl
changeset 624 f70b728ea30c
equal deleted inserted replaced
621:96fee2635b19 624:f70b728ea30c
       
     1 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Script to validate the unit links in a system definition or package definition XML file
       
    15 #!/usr/bin/perl
       
    16 
       
    17 use strict;
       
    18 
       
    19 
       
    20 use FindBin;		# for FindBin::Bin
       
    21 use lib $FindBin::Bin;
       
    22 use lib "$FindBin::Bin/lib";
       
    23 
       
    24 use Cwd;
       
    25 use Cwd 'abs_path';
       
    26 use Getopt::Long;
       
    27 use File::Basename;
       
    28 use File::Spec;
       
    29 use XML::DOM;
       
    30 
       
    31 my $output;
       
    32 my $path;
       
    33 my %defineParams;
       
    34 my %defines;
       
    35 my $defaultns = 'http://www.symbian.org/system-definition';	# needed if no DTD
       
    36 my $realloc;
       
    37 
       
    38 # need to add options for controlling which metas are filtered out and which are included inline
       
    39 GetOptions
       
    40 	(
       
    41 	'path=s' => $path,
       
    42 	 'effective-sysdef=s' => \$realloc
       
    43 	);
       
    44 
       
    45 # -path specifies the full system-model path to the file which is being processed. 
       
    46 #	This must be an absolute path if you're processing a root sysdef.
       
    47 #	If processing a pkgdef file, you can use "./package_definition.xml" to leave all links relative. Though I can't really see the use case for this.
       
    48 
       
    49 
       
    50 # if config is not set, no confguration will be done.
       
    51 # If it is set, all configuration metadata will be processed and stripped from the output, even if the confguration data is empty
       
    52 
       
    53  if($path eq '') {$path = '/os/deviceplatformrelease/foundation_system/system_model/system_definition.xml'}
       
    54 
       
    55 ($#ARGV == -1 ) && &help();
       
    56 my $sysdef = &abspath(shift);	# resolve the location of the root sysdef
       
    57 
       
    58 $realloc = $realloc || $sysdef;
       
    59 
       
    60 my %unitmap;
       
    61 my @p1=reverse(split(/[\\\/]/,$path));
       
    62 my @p2=reverse(split(/[\\\/]/,$realloc));
       
    63 
       
    64 shift(@p1);shift(@p2); # don't care abt file name
       
    65 while(lc($p1[0]) eq lc($p2[0])) {shift(@p1);shift(@p2)}
       
    66 
       
    67 $unitmap{join('/',reverse(@p1))} = join("/",reverse(@p2));
       
    68 
       
    69 my @p1=reverse(split(/[\\\/]/,$sysdef));
       
    70 my @p2=reverse(split(/[\\\/]/,$realloc));
       
    71 
       
    72 shift(@p1);shift(@p2); # don't care abt file name
       
    73 while(lc($p1[0]) eq lc($p2[0]) && scalar(@p1)) {shift(@p1);shift(@p2)}
       
    74 
       
    75 $unitmap{join('/',reverse(@p1))} = join("/",reverse(@p2));
       
    76 
       
    77 
       
    78 # rootmap is a mapping from the filesystem to the paths in the doc
       
    79 my %rootmap = &rootMap($path,$sysdef);	
       
    80 my %nsmap;
       
    81 my %urimap;
       
    82 
       
    83 my $parser = new XML::DOM::Parser;
       
    84 my   $sysdefdoc;
       
    85 eval {
       
    86 	$sysdefdoc = $parser->parsefile ($sysdef);
       
    87 };
       
    88 if(!$sysdefdoc) {
       
    89 	die "ERROR: could not open $sysdef\n";
       
    90 }
       
    91 
       
    92 
       
    93 
       
    94 my $maxschema = $sysdefdoc->getDocumentElement()->getAttribute('schema');	# don't check value, just store it.
       
    95 
       
    96 my $docroot =  $sysdefdoc->getDocumentElement;
       
    97 
       
    98 my $ns = $docroot->getAttribute('id-namespace');
       
    99 if(!$ns && $nsmap{''})
       
   100 	{
       
   101 	$docroot->setAttribute('id-namespace',$nsmap{''});
       
   102 	}
       
   103 
       
   104 $docroot->setAttribute('schema',$maxschema);	# output has the largest syntax version of all includes
       
   105 
       
   106 
       
   107 while(my($pre,$uri) = each(%nsmap))
       
   108 	{
       
   109 	$pre ne '' || next ;
       
   110 	$docroot->setAttribute("xmlns:$pre",$uri);
       
   111 	}
       
   112 
       
   113 &walk($sysdef,$docroot);	# process the XML
       
   114 
       
   115  
       
   116 sub abspath
       
   117 	{ 	# normalize the path into an absolute one
       
   118 	my  ($name,$path) = fileparse($_[0]);
       
   119 	$path=~tr,\\,/,;
       
   120 	if( -e $path)
       
   121 		{
       
   122 		return abs_path($path)."/$name";
       
   123 		}
       
   124 	my @dir = split('/',$_[0]);
       
   125 	my @new;
       
   126 	foreach my $d (@dir)
       
   127 		{
       
   128 		if($d eq '.') {next}
       
   129 		if($d eq '..')
       
   130 			{
       
   131 			pop(@new);
       
   132 			next;
       
   133 			}
       
   134 		push(@new,$d)
       
   135 		}
       
   136 	return join('/',@new);
       
   137 	}
       
   138 
       
   139 sub rootMap {
       
   140 	my @pathdirs = split(/\//,$_[0]);
       
   141 	my @rootdirs = split(/\//,$_[1]);
       
   142 
       
   143 	while(lc($rootdirs[$#rootdirs])  eq lc($pathdirs[$#pathdirs])  )
       
   144 		{
       
   145 		pop(@rootdirs);
       
   146 		pop(@pathdirs);
       
   147 		}
       
   148 	return (join('/',@rootdirs)  => join('/',@pathdirs) );
       
   149 	}
       
   150 
       
   151 sub rootMapMeta {
       
   152 	# find all the explict path mapping from the link-mapping metadata
       
   153 	my $node = shift;
       
   154 	foreach my $child (@{$node->getChildNodes})
       
   155 			{
       
   156 			if ($child->getNodeType==1 && $child->getTagName eq 'map-prefix')
       
   157 				{
       
   158 				my $from = $child->getAttribute('link');
       
   159 				my $to = $child->getAttribute('to');		# optional, but blank if not set
       
   160 				$rootmap{$from} = $to;
       
   161 				}
       
   162 			}
       
   163 	# once this is processed we have no more need for it. Remove from output
       
   164 	$node->getParentNode->removeChild($node);
       
   165 	}
       
   166 
       
   167 
       
   168 sub walk
       
   169 	{ 	# walk through the doc, resolving all links
       
   170 	my $file = shift;
       
   171 	my $node = shift;
       
   172 	my $type = $node->getNodeType;
       
   173 	if($type!=1) {return}
       
   174 	my $tag = $node->getTagName;
       
   175 	if($tag=~/^(layer|package|collection|component)$/ )
       
   176 		{
       
   177 		my $link= $node->getAttribute('href');
       
   178 		if($link)
       
   179 			{
       
   180 			my $file = &resolvePath($file,$link); 
       
   181 			if(-e $file)
       
   182 				{
       
   183 				&combineLink($node,$file);
       
   184 				}
       
   185 			else
       
   186 				{
       
   187 				print  "Note: $file not found\n";
       
   188 				$node->removeAttribute('href');
       
   189 				}
       
   190 			return;
       
   191 			}
       
   192 		}
       
   193 	elsif($tag=~/^(SystemDefinition|systemModel)$/ )
       
   194 		{
       
   195 		}
       
   196 	elsif($tag eq 'unit')
       
   197 		{
       
   198 		my %at = &atts($node);
       
   199 		my $pro;
       
   200 		foreach my $o (keys(%at)) 
       
   201 			{
       
   202 			if($o eq 'proFile' || $o=~/:proFile$/)
       
   203 				{
       
   204 				$pro = $at{$o};
       
   205 				last;
       
   206 				}
       
   207 			}
       
   208 		my $filter=$node->getParentNode()->getAttribute('filter');
       
   209 		if($filter ne '' && $at{'filter'}) {$filter.=','.$at{'filter'}}
       
   210 		elsif($at{'filter'}) {$filter=$at{'filter'}}
       
   211 		if($filter ne '') {$filter="\t($filter)"}
       
   212 		foreach my $atr ('bldFile','mrp','base')
       
   213 			{
       
   214 			my $ext;
       
   215 			my $link= $at{$atr};
       
   216 			if($atr eq 'bldFile') {
       
   217 				$ext = ($pro ne '') ? "/$pro" : '/bld.inf'
       
   218 			}
       
   219 			if($link ne '')
       
   220 				{
       
   221 				my $ok = 0;
       
   222 				my $trylink;
       
   223 				if($link && !($link=~/^\//))
       
   224 					{
       
   225 					$link= &abspath(File::Basename::dirname($file)."/$link");
       
   226 					$ok = (-e "$link$ext");
       
   227 					if(!$ok)	
       
   228 						{
       
   229 						foreach my $a (keys %rootmap)
       
   230 							{
       
   231 							$link=~s,^$a,$rootmap{$a},ie;
       
   232 							# remove leading ./  which is used to indicate that paths should remain relative
       
   233 							$link=~s,^\./([^/]),$1,; 
       
   234 							}
       
   235 
       
   236 						}
       
   237 					}
       
   238 				if(!$ok)
       
   239 					{
       
   240 					foreach my $a (keys %unitmap) {
       
   241 						if($a eq substr($link,0,length($a))) {
       
   242 							$trylink = $unitmap{$a}.substr($link,length($a));
       
   243 							if(-e "$trylink$ext") {
       
   244 								$ok=1;
       
   245 								$link = $trylink;
       
   246 								last;
       
   247 							}
       
   248 						}
       
   249 					}
       
   250 					}
       
   251 				if(!$ok)
       
   252 					{
       
   253 					print "Error: $atr not found in ",($trylink ne '') ? $trylink : $link,"$filter\n";
       
   254 					}				
       
   255 				}
       
   256 			}
       
   257 		}
       
   258 	elsif($tag eq 'meta')
       
   259 		{
       
   260 		my $rel= $node->getAttribute('rel') || 'Generic';
       
   261 		my $link= $node->getAttribute('href');
       
   262 		$link=~s,^file://(/([a-z]:/))?,$2,; # convert file URI to absolute path
       
   263 		if ($link ne '' ) 
       
   264 			{ 
       
   265 			if($link=~/^[\/]+:/)
       
   266 				{
       
   267 				print  "Note: Remote URL $link not validated\n";
       
   268 				next; # do not alter children
       
   269 				}
       
   270 			if(! ($link=~/^\//))
       
   271 				{
       
   272 				$link= &abspath(File::Basename::dirname($file)."/$link");
       
   273 				}
       
   274 			if(! -e $link) 
       
   275 				{
       
   276 				if(! -e &realPath($link)) {
       
   277 					print  "Warning: Local metadata file not found: $link\n";
       
   278 				}
       
   279 				next; # do not alter children
       
   280 				}
       
   281 			}
       
   282 		if($node->getAttribute('rel') eq 'link-mapping')
       
   283 			{# need to process this now
       
   284 			&rootMapMeta($node);
       
   285 			}
       
   286 		return;
       
   287 		}
       
   288 	else {return}
       
   289 	my $checkversion=0;
       
   290 	foreach my $item (@{$node->getChildNodes})
       
   291 		{
       
   292 		#print $item->getNodeType,"\n";
       
   293 		&walk($file,$item);
       
   294 		}
       
   295 
       
   296 
       
   297 
       
   298 	}
       
   299 
       
   300 
       
   301 sub realPath
       
   302 	{
       
   303 	my $link = shift;
       
   304 	foreach my $a (keys %unitmap)
       
   305 		{
       
   306 		if($a eq substr($link,0,length($a))) 
       
   307 			{
       
   308 			my $trylink = $unitmap{$a}.substr($link,length($a));
       
   309 			if(-e $trylink) {return $trylink}
       
   310 			}
       
   311 		}
       
   312 	}
       
   313 
       
   314 sub combineLink
       
   315 	{
       
   316 	# combine data from linked sysdef fragment w/ equivalent element in parent document
       
   317 	my $node = shift;
       
   318 	my $file = shift;
       
   319 	my $getfromfile = &localfile($file);
       
   320 	$getfromfile eq '' && return;  # already raised warning, no need to repeat
       
   321 	my $doc;
       
   322 	eval {
       
   323 		$doc = $parser->parsefile ($getfromfile);
       
   324 	};
       
   325 	if(!$doc) {
       
   326 		print "ERROR: could not open $getfromfile\n";
       
   327 		return;
       
   328 	}
       
   329 	my $item =&firstElement($doc->getDocumentElement);
       
   330 	$item || die "badly formatted $file";	
       
   331 	my @upid = &getNamespaceAndValue($node,'id');
       
   332 	my @downid = &getNamespaceAndValue($item,'id');
       
   333 	(($upid[0] eq $downid[0]) && ($upid[1] eq $downid[1]))  || die "$upid[1] ($upid[0]) differs from $downid[1] ($downid[0]) ";	# make sure the link is valid
       
   334 	&walk($getfromfile,$item);
       
   335 	}
       
   336 
       
   337 
       
   338 sub copyInto
       
   339 	{
       
   340 	# make a deep copy the node (2nd arg) into the element (1st arg)
       
   341 	my $parent=shift;
       
   342 	my $item = shift;
       
   343 	my $doc = $parent->getOwnerDocument;
       
   344 	my $type = $item->getNodeType;
       
   345 	my $new;
       
   346 	if($type==1) 
       
   347 		{
       
   348 		$new = $doc->createElement($item->getTagName);
       
   349 		my %down = &atts($item);
       
   350 		foreach my $ordered ('id','name','bldFile','mrp','level','levels','introduced','deprecated','filter')
       
   351 			{
       
   352 			if($down{$ordered})
       
   353 				{
       
   354 				$new->setAttribute($ordered,$down{$ordered});
       
   355 				delete $down{$ordered}
       
   356 				}
       
   357 			}
       
   358 		while(my($a,$b) = each(%down))
       
   359 			{
       
   360 			$new->setAttribute($a,$b);
       
   361 			}
       
   362 		foreach my $child (@{$item->getChildNodes})
       
   363 			{
       
   364 			&copyInto($new,$child);
       
   365 			}
       
   366 		}
       
   367 	elsif($type==3) 
       
   368 		{
       
   369 		$new = $doc->createTextNode ($item->getData);
       
   370 		}
       
   371 	elsif($type==8) 
       
   372 		{
       
   373 		$new = $doc->createComment  ($item->getData);
       
   374 		}
       
   375 	if($new)
       
   376 		{
       
   377 		$parent->appendChild($new);
       
   378 		}
       
   379 	}
       
   380 
       
   381 
       
   382 sub getNamespaceAndValue
       
   383 	{
       
   384 	my $node = shift;
       
   385 	my $attr = shift || 'id';
       
   386 	my $id = $node->getAttribute($attr);
       
   387 	if($id eq '') {return}
       
   388 	my $ns;
       
   389 	if($id=~s/^(.*)://)
       
   390 		{ # it's got a ns, find out what it is
       
   391 		$ns=&getNs($node,$1);
       
   392 		}
       
   393 	else
       
   394 		{
       
   395 		$ns = $node->getOwnerDocument->getDocumentElement->getAttribute("id-namespace") ||
       
   396 			$defaultns;
       
   397 		}
       
   398 	return ($ns,$id);;
       
   399 	}
       
   400 
       
   401 sub getNs
       
   402 	{
       
   403 	# find the namespace URI that applies to the specified prefix.
       
   404 	my $node = shift;
       
   405 	my $pre = shift;
       
   406 	my $uri = $node->getAttribute("xmlns:$pre");
       
   407 	if($uri) {return $uri}
       
   408 	my $parent = $node->getParentNode;
       
   409 	if($parent && $parent->getNodeType==1)
       
   410 		{
       
   411 		return getNs($parent,$pre);
       
   412 		}
       
   413 	}
       
   414 
       
   415 
       
   416 
       
   417 sub firstElement {
       
   418 	# return the first element in this node
       
   419 	my $node = shift;
       
   420 	foreach my $item (@{$node->getChildNodes}) {
       
   421 		if($item->getNodeType==1) {return $item}
       
   422 	}
       
   423 }
       
   424 
       
   425 
       
   426 sub atts {
       
   427 	# return a hash of all attribtues defined for this element
       
   428 	my $node = shift;
       
   429 	my %at = $node->getAttributes;
       
   430 	my %list;
       
   431 	foreach my $a (keys %{$node->getAttributes}) 
       
   432 		{
       
   433 		if($a ne '')
       
   434 			{
       
   435 			$list{$a} = $node->getAttribute ($a);
       
   436 			}
       
   437 		}
       
   438 	return %list;
       
   439 }
       
   440 
       
   441 
       
   442 sub ns 
       
   443 	{
       
   444 	# return a hash of ns prefix and uri -- the xmlns: part is stripped off
       
   445 	my $node = shift;
       
   446 	my %list;
       
   447 	foreach my $a (keys %{$node->getAttributes}) 
       
   448 		{
       
   449 		my $pre = $a;
       
   450 		if($pre=~s/^xmlns://)
       
   451 			{
       
   452 			$list{$pre} = $node->getAttribute ($a);
       
   453 			}
       
   454 		}
       
   455 	return %list;
       
   456 	}
       
   457 
       
   458 
       
   459 sub resolvePath
       
   460 	{
       
   461 	# return full path to 2nd arg relative to first (path or absolute URI)
       
   462 	my $base = shift;
       
   463 	my $path = shift;
       
   464 	if($path=~m,^/,) {return $path } # path is absolute, but has no drive. Let OS deal with it.
       
   465 	if($path=~s,^file:///([a-zA-Z]:/),$1,) {return $path } # file URI with drive letter
       
   466 	if($path=~m,^file://,) {return $path } # file URI with no drive letter (unit-style). Just pass on as is with leading / and let OS deal with it
       
   467 	if($path=~m,^[a-z0-9][a-z0-9]+:,i) {return $path } # absolute URI -- no idea how to handle, so just return
       
   468 	return &abspath(File::Basename::dirname($base)."/$path");
       
   469 	}
       
   470 
       
   471 
       
   472 sub resolveURI
       
   473 	{
       
   474 	# return full path to 2nd arg relative to first (path or absolute URI)
       
   475 	my $base = shift;
       
   476 	my $path = shift;
       
   477 	if($path=~m,[a-z0-9][a-z0-9]+:,i) {return $path } # absolute URI -- just return
       
   478 	if($path=~m,^/,) {return $path } # path is absolute, but has no drive. Let OS deal with it.
       
   479 	return &abspath(File::Basename::dirname($base)."/$path");
       
   480 	}
       
   481 
       
   482 sub localfile
       
   483 	{
       
   484 	my $file = shift;
       
   485 	if($file=~s,file:///([a-zA-Z]:/),$1,) {return $file } # file URI with drive letter
       
   486 	if($file=~m,file://,) {return $file } # file URI with no drive letter (unit-style). Just pass on as is with leading / and let OS deal with it
       
   487 	if($file=~m,^([a-z0-9][a-z0-9]+):,i)
       
   488 		{
       
   489 		print  "ERROR: $1 scheme not supported\n";
       
   490 		return;  # return empty string if not supported.
       
   491 		} 
       
   492 	return $file
       
   493 	}
       
   494 
       
   495 
       
   496 
       
   497 	
       
   498 
       
   499 sub help
       
   500 	{
       
   501 	my $name= $0; $name=~s,^.*[\\/],,;
       
   502 my $text;
       
   503 format STDERR =
       
   504  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       
   505   $text,
       
   506      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
       
   507     $text
       
   508 .
       
   509 print STDERR "usage: $name  [options...] sysdef\n  valid options are:\n\n";
       
   510 	foreach (
       
   511 		"-path [sm-path]\tspecifies the full system-model path to the file which is being processed. By default this is  \"/os/deviceplatformrelease/foundation_system/system_model/system_definition.xml\"",
       
   512 			"   This must be an absolute path if you're processing a root sysdef.",
       
   513 			"   If processing a pkgdef file, you can use \"./package_definition.xml\" to leave all links relative.",
       
   514 		"effective-sysdef [local-file]\tspecifies another local filesystem location the sysdef should be considered when resolving linked metas and unit paths, but not system model item hrefs. This is mainly used for testing system-wide changes to pkgdefs since it allows the pkgdefs to exist in a separate location to the rest of the codeline"
       
   515 		) {
       
   516 		$text = $_;
       
   517 		write STDERR;
       
   518 		print STDERR "\n";
       
   519 	}
       
   520 
       
   521 	exit(1);
       
   522 	}
       
   523 
       
   524 
       
   525