sbsv2/raptor/bin/depcrunch.py
author timothy.murphy@nokia.com
Tue, 11 May 2010 20:22:35 +0100
branchfix
changeset 549 d633be326c9f
parent 546 e6381a1f4952
child 551 b41ce675e7b2
permissions -rw-r--r--
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
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 os
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    24
import re
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    25
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    26
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
    27
	pass
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    28
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    29
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
    30
	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
    31
	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
    32
	# 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
    33
	# 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
    34
	# 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
    35
	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
    36
	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
    37
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    38
	target = None
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    39
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    40
	deps = []
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    41
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
    42
	# 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
    43
	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
    44
		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
    45
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
    46
		# 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
    47
		# 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
    48
		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
    49
			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
    50
			if t:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    51
				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
    52
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
		# 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
    54
		# 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
    55
		# 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
    56
		# 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
    57
		# 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
    58
		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
    59
		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
    60
			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
    61
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    62
	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
    63
		raise NoTargetException()
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    64
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    65
	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
    66
		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
    67
		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
    68
			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
    69
		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
    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
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    74
## 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
    75
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    76
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
    77
	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
    78
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    79
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
    80
	 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
    81
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    82
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
    83
	 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
    84
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    85
(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
    86
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    87
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    88
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
    89
	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
    90
	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
    91
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    92
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
    93
	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
    94
	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
    95
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    96
depfilename="stdin"
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
    97
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
    98
	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
    99
	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
   100
else:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   101
	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
   102
try:
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   103
	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
   104
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
   105
	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
   106
	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
   107
	
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   108
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   109
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
   110
	file.close()
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   111
e6381a1f4952 fix tests for resources. Add python dependency manipulation because it's understandable to humans.
timothy.murphy@nokia.com
parents:
diff changeset
   112
sys.exit(0)