Merged revisions 2136-2200,2202-2290,2292-2301 via svnmerge from
[scons.git] / src / test_interrupts.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Verify that the SCons source code contains only correct handling of
29 keyboard interrupts (e.g. Ctrl-C).
30 """
31
32 import os
33 import os.path
34 import re
35 import string
36 import time
37
38 import TestSCons
39
40 test = TestSCons.TestSCons()
41
42 # We do not want statements of the form:
43 # try:
44 #   # do something, e.g.
45 #   return a['x']
46 # except:
47 #   # do the exception handling
48 #   a['x'] = getx()
49 #   return a['x']
50 #
51 # The code above may catch a KeyboardInterrupt exception, which was not
52 # intended by the programmer. We check for these situations in all python
53 # source files.
54
55 try:
56     cwd = os.environ['SCONS_CWD']
57 except KeyError:
58     scons_lib_dir = os.environ['SCONS_LIB_DIR']
59     MANIFEST = os.path.join(scons_lib_dir, 'MANIFEST.in')
60 else:
61     #cwd = os.getcwd()
62     scons_lib_dir = os.path.join(cwd, 'build', 'scons')
63     MANIFEST = os.path.join(scons_lib_dir, 'MANIFEST')
64
65 try:
66     fp = open(MANIFEST)
67 except IOError:
68     test.skip_test('%s does not exist; skipping test.\n' % MANIFEST)
69 else:
70     files = string.split(fp.read())
71     files = filter(lambda f: f[-3:] == '.py', files)
72
73 # some regexps to parse the python files
74 tryexc_pat = re.compile(
75 r'^(?P<try_or_except>(?P<indent> *)(try|except)( [^\n]*)?:.*)',re.MULTILINE)
76 keyboardint_pat = re.compile(r' *except +([^,],)*KeyboardInterrupt([ ,][^\n]*)?:[^\n]*')
77 exceptall_pat   = re.compile(r' *except *:[^\n]*')
78
79 uncaughtKeyboardInterrupt = 0
80 for f in files:
81     contents = open(os.path.join(scons_lib_dir, f)).read()
82     try_except_lines = {}
83     lastend = 0
84     while 1:
85         match = tryexc_pat.search( contents, lastend )
86         if match is None:
87             break
88         #print match.groups()
89         lastend = match.end()
90         try:
91             indent_list = try_except_lines[match.group('indent')]
92         except:
93             indent_list = []
94         line_num = 1 + string.count(contents[:match.start()], '\n')
95         indent_list.append( (line_num, match.group('try_or_except') ) )
96         try_except_lines[match.group('indent')] = indent_list
97     for indent in try_except_lines.keys():
98         exc_keyboardint_seen = 0
99         exc_all_seen = 0
100         for (l,statement) in try_except_lines[indent] + [(-1,indent + 'try')]:
101             #print "%4d %s" % (l,statement),
102             m1 = keyboardint_pat.match(statement)
103             m2 = exceptall_pat.match(statement)
104             if string.find(statement, indent + 'try') == 0:
105                 if exc_all_seen and not exc_keyboardint_seen:
106                     uncaughtKeyboardInterrupt = 1
107                     print "File %s:%d: Uncaught KeyboardInterrupt!" % (f,line)
108                 exc_keyboardint_seen = 0
109                 exc_all_seen = 0
110                 line = l
111                 #print " -> reset"
112             elif not m1 is None:
113                 exc_keyboardint_seen = 1
114                 #print " -> keyboard -> ", m1.groups()
115             elif not m2 is None:
116                 exc_all_seen = 1
117                 #print " -> all -> ", m2.groups()
118             else:
119                 pass
120                 #print "Warning: unknown statement %s" % statement
121
122 test.fail_test(uncaughtKeyboardInterrupt)
123
124 test.pass_test()