sbsv2/raptor/bin/depcrunch.py
author Daniel Jacobs <daniel.jacobs@nokia.com>
Mon, 17 May 2010 11:07:30 +0100
changeset 554 e7dd8533063f
parent 551 b41ce675e7b2
permissions -rw-r--r--
Version 2.14.0 (candidate one).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     1
#
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     2
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     3
# All rights reserved.
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     4
# This component and the accompanying materials are made available
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     6
# which accompanies this distribution, and is available
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     8
#
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
     9
# Initial Contributors:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    11
#
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    12
# Contributors:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    13
#
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    14
# Description: 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    15
# Minimise the dependencies in a C preprocessor dependency file to
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    16
# those that CPP could not find.  Then add in an assumption about 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    17
# where to find them.  Output is assumed to be relevant to only one target
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    18
# even if multiple dep files are analysed.
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    19
#
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    20
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    21
import sys
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    22
from  optparse import OptionParser
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    23
import re
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    24
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    25
class NoTargetException(Exception):
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    26
	pass
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    27
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    28
def depcrunch(file,extensions,assume):
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    29
	target_pattern = r"^\s*(\S+):\s+"
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    30
	target_re = re.compile(target_pattern)
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    31
	# Not the use of (?i) in the following expression.  re.I seems to cause re.findall
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    32
	# to not actually find all files matching the extension whereas (?i) provides
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    33
	# case insensitivity at the right point and it works.  Really don't understand this.
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    34
	extension_pattern = r"\s([^/ \t]+\.((?i)" + "|".join([t for t in extensions]) + r"))\b"
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    35
	extension_re = re.compile(extension_pattern)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    36
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    37
	target = None
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    38
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    39
	deps = []
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    40
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    41
	# Read through the dependencies.
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    42
	for l in file:
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    43
		l = l.replace("\\","/").rstrip("\n\r")
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    44
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    45
		# Look out for the target name if 
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    46
		# we have not found it yet
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    47
		if not target:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    48
			t = target_re.match(l)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    49
			if t:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    50
				target = t.groups()[0]
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    51
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    52
		# Look for prerequisites matching the 
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    53
		# extensions.  There may be one or more on 
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    54
		# the same line as the target name.
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    55
		# Don't use re.I - somehow prevents 
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    56
		# all but one match in a line which may have several files
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    57
		m = extension_re.findall(l)
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    58
		if m:
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    59
			deps.extend([d[0] for d in m])
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    60
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    61
	if not target:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    62
		raise NoTargetException()
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    63
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    64
	if len(deps) > 0:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    65
		print "%s: \\" % target
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    66
		for d in deps[:-1]:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    67
			print " %s \\" % (assume + "/" + d)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    68
		print " %s " % (assume + "/" + deps[-1])
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    69
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    70
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    71
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    72
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    73
## Command Line Interface ####################################################
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    74
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    75
parser = OptionParser(prog = "depcrunch",
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    76
	usage = "%prog [-h | options] [<depfile>]")
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    77
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    78
parser.add_option("-e", "--extensions", 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    79
	 action="store", dest="extensions", type='string', help="comma separated list of file extensions of missing files to keep in the crunched dep file.") 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    80
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    81
parser.add_option("-a", "--assume", 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    82
	 action="store", dest="assume", type='string', help="when cpp reports missing dependencies, assume that they are in this directory") 
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    83
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    84
(options, args) = parser.parse_args()
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    85
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    86
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    87
if not options.extensions:
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
    88
	parser.error("you must specify a comma-separated list of file extensions with the -e option.")
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    89
	sys.exit(1)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    90
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    91
if not options.assume:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    92
	parser.error("you must specify an 'assumed directory' for correcting missing dependencies with the -a option.")
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    93
	sys.exit(1)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    94
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    95
depfilename="stdin"
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    96
if len(args) > 0:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    97
	depfilename=args[0]
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    98
	file = open(depfilename,"r")
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    99
else:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   100
	file = sys.stdin
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   101
try:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   102
	depcrunch(file,options.extensions.split(","), options.assume)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   103
except NoTargetException,e:
549
d633be326c9f fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents: 546
diff changeset
   104
	sys.stderr.write("Target name not found in dependency file\n");
546
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   105
	sys.exit(2)
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   106
	
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   107
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   108
if file != sys.stdin:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   109
	file.close()
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   110
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   111
sys.exit(0)