27 pass |
27 pass |
28 |
28 |
29 def depcrunch(file,extensions,assume): |
29 def depcrunch(file,extensions,assume): |
30 target_pattern = r"^\s*(\S+):\s+" |
30 target_pattern = r"^\s*(\S+):\s+" |
31 target_re = re.compile(target_pattern) |
31 target_re = re.compile(target_pattern) |
32 extension_pattern = "[ \t]([^\/\\ \t]+\.(" + "|".join(["("+ t + ")" for t in extensions]) + "))\\b" |
32 # Not the use of (?i) in the following expression. re.I seems to cause re.findall |
|
33 # to not actually find all files matching the extension whereas (?i) provides |
|
34 # case insensitivity at the right point and it works. Really don't understand this. |
|
35 extension_pattern = r"\s([^/ \t]+\.((?i)" + "|".join([t for t in extensions]) + r"))\b" |
33 extension_re = re.compile(extension_pattern) |
36 extension_re = re.compile(extension_pattern) |
34 |
37 |
35 target = None |
38 target = None |
36 |
39 |
37 deps = [] |
40 deps = [] |
38 |
41 |
39 for l in file.xreadlines(): |
42 # Read through the dependencies. |
|
43 for l in file: |
40 l = l.replace("\\","/").rstrip("\n\r") |
44 l = l.replace("\\","/").rstrip("\n\r") |
41 |
45 |
|
46 # Look out for the target name if |
|
47 # we have not found it yet |
42 if not target: |
48 if not target: |
43 t = target_re.match(l) |
49 t = target_re.match(l) |
44 if t: |
50 if t: |
45 target = t.groups()[0] |
51 target = t.groups()[0] |
46 |
52 |
47 m = extension_re.match(l) |
53 # Look for prerequisites matching the |
|
54 # extensions. There may be one or more on |
|
55 # the same line as the target name. |
|
56 # Don't use re.I - somehow prevents |
|
57 # all but one match in a line which may have several files |
|
58 m = extension_re.findall(l) |
48 if m: |
59 if m: |
49 deps.append(m.groups()[0]) |
60 deps.extend([d[0] for d in m]) |
50 |
61 |
51 if not target: |
62 if not target: |
52 raise NoTargetException() |
63 raise NoTargetException() |
53 |
64 |
54 if len(deps) > 0: |
65 if len(deps) > 0: |
73 |
84 |
74 (options, args) = parser.parse_args() |
85 (options, args) = parser.parse_args() |
75 |
86 |
76 |
87 |
77 if not options.extensions: |
88 if not options.extensions: |
78 parser.error("you must specify a comma-separated list of file extensions with the -t option.") |
89 parser.error("you must specify a comma-separated list of file extensions with the -e option.") |
79 sys.exit(1) |
90 sys.exit(1) |
80 |
91 |
81 if not options.assume: |
92 if not options.assume: |
82 parser.error("you must specify an 'assumed directory' for correcting missing dependencies with the -a option.") |
93 parser.error("you must specify an 'assumed directory' for correcting missing dependencies with the -a option.") |
83 sys.exit(1) |
94 sys.exit(1) |
89 else: |
100 else: |
90 file = sys.stdin |
101 file = sys.stdin |
91 try: |
102 try: |
92 depcrunch(file,options.extensions.split(","), options.assume) |
103 depcrunch(file,options.extensions.split(","), options.assume) |
93 except NoTargetException,e: |
104 except NoTargetException,e: |
94 sys.stderr.write("Target name not found in dependency file"); |
105 sys.stderr.write("Target name not found in dependency file\n"); |
95 sys.exit(2) |
106 sys.exit(2) |
96 |
107 |
97 |
108 |
98 if file != sys.stdin: |
109 if file != sys.stdin: |
99 file.close() |
110 file.close() |