perf script python: Add Python3 support to tests/attr.py

Support both Python 2 and Python 3 in tests/attr.py

The use of "except as" syntax implies the minimum supported Python2 version is
now v2.6

Committer testing:

$ make -C tools/perf PYTHON3=python install-bin

Before:

# perf test attr
16: Setup struct perf_event_attr : FAILED!
48: Synthesize attr update : Ok
[root@quaco ~]# perf test -v attr
16: Setup struct perf_event_attr :
--- start ---
test child forked, pid 3121
File "/home/acme/libexec/perf-core/tests/attr.py", line 324
except Unsup, obj:
^
SyntaxError: invalid syntax
test child finished with -1
---- end ----
Setup struct perf_event_attr: FAILED!
48: Synthesize attr update :
--- start ---
test child forked, pid 3124
test child finished with 0
---- end ----
Synthesize attr update: Ok
#

After:

# perf test attr
16: Setup struct perf_event_attr : Ok
48: Synthesize attr update : Ok
#

Signed-off-by: Tony Jones <tonyj@suse.de>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Cc: Seeteena Thoufeek <s1seetee@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20190124005229.16146-7-tonyj@suse.de
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by Tony Jones and committed by Arnaldo Carvalho de Melo 8f2f350c 6ab3bc24

+19 -13
+19 -13
tools/perf/tests/attr.py
··· 1 #! /usr/bin/python 2 # SPDX-License-Identifier: GPL-2.0 3 4 import os 5 import sys 6 import glob ··· 10 import tempfile 11 import logging 12 import shutil 13 - import ConfigParser 14 15 def data_equal(a, b): 16 # Allow multiple values in assignment separated by '|' ··· 106 def equal(self, other): 107 for t in Event.terms: 108 log.debug(" [%s] %s %s" % (t, self[t], other[t])); 109 - if not self.has_key(t) or not other.has_key(t): 110 return False 111 if not data_equal(self[t], other[t]): 112 return False 113 return True 114 115 def optional(self): 116 - if self.has_key('optional') and self['optional'] == '1': 117 return True 118 return False 119 120 def diff(self, other): 121 for t in Event.terms: 122 - if not self.has_key(t) or not other.has_key(t): 123 continue 124 if not data_equal(self[t], other[t]): 125 log.warning("expected %s=%s, got %s" % (t, self[t], other[t])) ··· 140 # - expected values assignments 141 class Test(object): 142 def __init__(self, path, options): 143 - parser = ConfigParser.SafeConfigParser() 144 parser.read(path) 145 146 log.warning("running '%s'" % path) ··· 199 return True 200 201 def load_events(self, path, events): 202 - parser_event = ConfigParser.SafeConfigParser() 203 parser_event.read(path) 204 205 # The event record section header contains 'event' word, ··· 213 # Read parent event if there's any 214 if (':' in section): 215 base = section[section.index(':') + 1:] 216 - parser_base = ConfigParser.SafeConfigParser() 217 parser_base.read(self.test_dir + '/' + base) 218 base_items = parser_base.items('event') 219 ··· 328 for f in glob.glob(options.test_dir + '/' + options.test): 329 try: 330 Test(f, options).run() 331 - except Unsup, obj: 332 log.warning("unsupp %s" % obj.getMsg()) 333 - except Notest, obj: 334 log.warning("skipped %s" % obj.getMsg()) 335 336 def setup_log(verbose): ··· 369 parser.add_option("-p", "--perf", 370 action="store", type="string", dest="perf") 371 parser.add_option("-v", "--verbose", 372 - action="count", dest="verbose") 373 374 options, args = parser.parse_args() 375 if args: ··· 379 setup_log(options.verbose) 380 381 if not options.test_dir: 382 - print 'FAILED no -d option specified' 383 sys.exit(-1) 384 385 if not options.test: ··· 388 try: 389 run_tests(options) 390 391 - except Fail, obj: 392 - print "FAILED %s" % obj.getMsg(); 393 sys.exit(-1) 394 395 sys.exit(0)
··· 1 #! /usr/bin/python 2 # SPDX-License-Identifier: GPL-2.0 3 4 + from __future__ import print_function 5 + 6 import os 7 import sys 8 import glob ··· 8 import tempfile 9 import logging 10 import shutil 11 + 12 + try: 13 + import configparser 14 + except ImportError: 15 + import ConfigParser as configparser 16 17 def data_equal(a, b): 18 # Allow multiple values in assignment separated by '|' ··· 100 def equal(self, other): 101 for t in Event.terms: 102 log.debug(" [%s] %s %s" % (t, self[t], other[t])); 103 + if t not in self or t not in other: 104 return False 105 if not data_equal(self[t], other[t]): 106 return False 107 return True 108 109 def optional(self): 110 + if 'optional' in self and self['optional'] == '1': 111 return True 112 return False 113 114 def diff(self, other): 115 for t in Event.terms: 116 + if t not in self or t not in other: 117 continue 118 if not data_equal(self[t], other[t]): 119 log.warning("expected %s=%s, got %s" % (t, self[t], other[t])) ··· 134 # - expected values assignments 135 class Test(object): 136 def __init__(self, path, options): 137 + parser = configparser.SafeConfigParser() 138 parser.read(path) 139 140 log.warning("running '%s'" % path) ··· 193 return True 194 195 def load_events(self, path, events): 196 + parser_event = configparser.SafeConfigParser() 197 parser_event.read(path) 198 199 # The event record section header contains 'event' word, ··· 207 # Read parent event if there's any 208 if (':' in section): 209 base = section[section.index(':') + 1:] 210 + parser_base = configparser.SafeConfigParser() 211 parser_base.read(self.test_dir + '/' + base) 212 base_items = parser_base.items('event') 213 ··· 322 for f in glob.glob(options.test_dir + '/' + options.test): 323 try: 324 Test(f, options).run() 325 + except Unsup as obj: 326 log.warning("unsupp %s" % obj.getMsg()) 327 + except Notest as obj: 328 log.warning("skipped %s" % obj.getMsg()) 329 330 def setup_log(verbose): ··· 363 parser.add_option("-p", "--perf", 364 action="store", type="string", dest="perf") 365 parser.add_option("-v", "--verbose", 366 + default=0, action="count", dest="verbose") 367 368 options, args = parser.parse_args() 369 if args: ··· 373 setup_log(options.verbose) 374 375 if not options.test_dir: 376 + print('FAILED no -d option specified') 377 sys.exit(-1) 378 379 if not options.test: ··· 382 try: 383 run_tests(options) 384 385 + except Fail as obj: 386 + print("FAILED %s" % obj.getMsg()) 387 sys.exit(-1) 388 389 sys.exit(0)