1 --- ./deheader (original)
2 +++ ./deheader (refactored)
4 The last line of the output is a statistical summary of operations.
7 -import sys, os, getopt, time, re, operator, commands
8 +import sys, os, getopt, time, re, operator, subprocess
12 @@ -1243,19 +1243,19 @@
13 if not os.path.isdir(root):
14 if excludes and excludes.search(root):
16 - print "deheader: %s excluded" % root
17 + print("deheader: %s excluded" % root)
18 elif InclusionMap.c_source(root):
19 self.files.append(root)
21 - print >>sys.stderr, "deheader: can't analyze %s" % root
22 + print("deheader: can't analyze %s" % root, file=sys.stderr)
24 for root, dirs, files in os.walk(root):
25 - dirs = filter(lambda x: not x.startswith("."), dirs)
26 + dirs = [x for x in dirs if not x.startswith(".")]
28 path = os.path.join(root, name)
29 if excludes and excludes.search(path):
31 - print "deheader: %s excluded" % root
32 + print("deheader: %s excluded" % root)
33 elif InclusionMap.c_source(path):
34 self.files.append(path)
36 @@ -1277,15 +1277,15 @@
37 elif line.startswith("#include"):
38 if verbosity >= PROGRESS_DEBUG:
40 - print "deheader: %s includes %s" % (sourcefile, name)
41 + print("deheader: %s includes %s" % (sourcefile, name))
42 if ignore and ignore.search(line):
43 if verbosity >= PROGRESS_DEBUG:
44 - print "deheader: ignoring %s (exclusion match with %s)." % (name, ignore.pattern)
45 + print("deheader: ignoring %s (exclusion match with %s)." % (name, ignore.pattern))
47 if not conditions or conditions == ["S_SPLINT_S"]:
50 - print "deheader: ignoring %s (conditional inclusion)" % name
51 + print("deheader: ignoring %s (conditional inclusion)" % name)
52 for (r, c, h) in compiled:
54 if not set(h).issubset(set(seen)):
56 trimmedcount[ref] = trimmedcount.get(ref, 0) + 1
57 for ref in trimmedcount:
58 if trimmedcount[ref] > 1:
59 - print "deheader: %s has more than one inclusion of %s" % (sourcefile, ref)
60 + print("deheader: %s has more than one inclusion of %s" % (sourcefile, ref))
61 def forget(self, sourcefile, header):
62 "Forget a header dependency."
63 self.depends_on[sourcefile].remove(header)
65 elif trimmed[0] == '<':
66 return trimmed.split('>')[0] + ">"
71 def testcompile(source, maker, msg="", verbosity=0, showerrs=False):
72 "Test-compile a sourcefile. Return the status and the compilation time"
75 command = maker + " " + derived
77 - (status, output) = commands.getstatusoutput(command)
78 + (status, output) = subprocess.getstatusoutput(command)
80 if (os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0 and showerrs) or verbosity >= COMMAND_DEBUG:
81 sys.stdout.write(output)
85 if verbosity >= PROGRESS_DEBUG:
86 - print "deheader: %s%s %s." \
87 - % (sourcefile, msg, explain)
88 + print("deheader: %s%s %s." \
89 + % (sourcefile, msg, explain))
90 if os.path.exists(derived):
92 return (status, end - start)
94 for required in requirements:
95 if required in header:
96 if verbosity >= PROGRESS_DEBUG:
97 - print "deheader: in %s, %s prevents uninclusion of %s" % (sourcefile, trigger, trim(header))
98 + print("deheader: in %s, %s prevents uninclusion of %s" % (sourcefile, trigger, trim(header)))
101 saveit.remove_headers([header])
102 @@ -1404,10 +1404,10 @@
104 # Missing-require detection. Can't be merged with duplicate-header
105 # detection because this has to be done after unneeded headers are removed.
106 - stillhere = map(trim, includes)
107 + stillhere = list(map(trim, includes))
108 for (requirement, trigger) in requires:
109 if not set(requirement).issubset(stillhere):
110 - print "deheader: in %s, %s portability requires %s." % (sourcefile, trigger, ",".join(requirement))
111 + print("deheader: in %s, %s portability requires %s." % (sourcefile, trigger, ",".join(requirement)))
114 def deheader(sourcefile, maker, includes, requires, remove, verbose):
115 @@ -1421,7 +1421,7 @@
116 includes[:], requires, verbose)
118 for line in unneeded:
119 - print "deheader: remove %s from %s" % (trim(line), sourcefile)
120 + print("deheader: remove %s from %s" % (trim(line), sourcefile))
122 remove_it = SaveForModification(sourcefile)
123 remove_it.remove_headers(unneeded)
124 @@ -1429,7 +1429,7 @@
126 return Summary([sourcefile], includes, unneeded)
128 - print >>sys.stderr, "deheader: basic compilation failed on %s" % (sourcefile,)
129 + print("deheader: basic compilation failed on %s" % (sourcefile,), file=sys.stderr)
130 return Summary([sourcefile], includes, [])
132 # After-action analysis starts here
133 @@ -1472,8 +1472,8 @@
134 elif switch in ('-v', '--verbose'):
136 elif switch in ('-V', '--version'):
137 - print "deheader", version
138 - raise SystemExit, 0
139 + print("deheader", version)
140 + raise SystemExit(0)
141 elif switch in ('-x', '--exclude'):
144 @@ -1497,7 +1497,7 @@
146 for summary in summaries:
147 stats = stats + summary
148 - print "deheader: saw", stats
149 - raise SystemExit, 0
150 + print("deheader: saw", stats)
151 + raise SystemExit(0)