Changes in tests/pybin/tools.py [a468e1e9:8364209]
- File:
-
- 1 edited
-
tests/pybin/tools.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/pybin/tools.py
ra468e1e9 r8364209 73 73 ) 74 74 75 return proc.returncode, out.decode(" latin-1") if out else None75 return proc.returncode, out.decode("utf-8") if out else None 76 76 except subprocess.TimeoutExpired: 77 if settings.timeout2gdb: 78 print("Process {} timeout".format(proc.pid)) 79 proc.communicate() 80 return 124, str(None) 81 else: 82 proc.send_signal(signal.SIGABRT) 83 proc.communicate() 84 return 124, str(None) 77 proc.send_signal(signal.SIGABRT) 78 proc.communicate() 79 return 124, str(None) 85 80 86 81 except Exception as ex: … … 88 83 raise 89 84 90 def is_empty(fname):91 if not os.path.isfile(fname):92 return True93 94 if os.stat(fname).st_size == 0:95 return True96 97 return False98 99 85 def is_ascii(fname): 100 86 if settings.dry_run: 101 87 print("is_ascii: %s" % fname) 102 return (True, "")88 return True 103 89 104 90 if not os.path.isfile(fname): 105 return (False, "No file")106 107 code, out = sh("file ",fname, output_file=subprocess.PIPE)91 return False 92 93 code, out = sh("file %s" % fname, output_file=subprocess.PIPE) 108 94 if code != 0: 109 return (False, "'file EXPECT' failed with code {}".format(code))95 return False 110 96 111 97 match = re.search(".*: (.*)", out) 112 98 113 99 if not match: 114 return (False, "Unreadable file type: '{}'".format(out)) 115 116 if "ASCII text" in match.group(1): 117 return (True, "") 118 119 return (False, "File type should be 'ASCII text', was '{}'".format(match.group(1))) 100 return False 101 102 return match.group(1).startswith("ASCII text") 120 103 121 104 def is_exe(fname): … … 132 115 return None 133 116 134 file = open(file, mode , encoding="latin-1") # use latin-1 so all chars mean something.117 file = open(file, mode) 135 118 exitstack.push(file) 136 119 return file … … 181 164 '-s' if silent else None, 182 165 test_param, 183 settings.ast.flags,184 166 settings.arch.flags, 185 167 settings.debug.flags, … … 192 174 return sh(*cmd, output_file=output_file, error=error) 193 175 194 def make_recon(target):195 cmd = [196 *settings.make,197 '-W',198 os.path.abspath(os.path.join(settings.BUILDDIR, '../driver/cfa')),199 '--recon',200 target201 ]202 cmd = [s for s in cmd if s]203 return sh(*cmd, output_file=subprocess.PIPE)204 205 176 def which(program): 206 fpath, fname = os.path.split(program) 207 if fpath: 208 if is_exe(program): 209 return program 210 else: 211 for path in os.environ["PATH"].split(os.pathsep): 212 exe_file = os.path.join(path, program) 213 if is_exe(exe_file): 214 return exe_file 215 return None 177 fpath, fname = os.path.split(program) 178 if fpath: 179 if is_exe(program): 180 return program 181 else: 182 for path in os.environ["PATH"].split(os.pathsep): 183 exe_file = os.path.join(path, program) 184 if is_exe(exe_file): 185 return exe_file 186 187 return None 216 188 217 189 @contextlib.contextmanager … … 262 234 # helper function to check if a files contains only a specific string 263 235 def file_contains_only(file, text) : 264 with open(file , encoding="latin-1") as f: # use latin-1 so all chars mean something.236 with open(file) as f: 265 237 ff = f.read().strip() 266 238 result = ff == text.strip() … … 270 242 # transform path to canonical form 271 243 def canonical_path(path): 272 abspath = os.path.abspath( os.path.realpath(__main__.__file__))244 abspath = os.path.abspath(__main__.__file__) 273 245 dname = os.path.dirname(abspath) 274 246 return os.path.join(dname, os.path.normpath(path) ) … … 351 323 raise argparse.ArgumentTypeError(msg) 352 324 353 # Convert a function that converts a string to one that converts comma separated string.354 def comma_separated(elements):355 return lambda string: [elements(part) for part in string.split(',')]356 357 325 def fancy_print(text): 358 326 column = which('column') … … 397 365 398 366 class Timed: 399 def __enter__(self):400 self.start = time.time()401 return self402 403 def __exit__(self, *args):404 self.end = time.time()405 self.duration = self.end - self.start367 def __enter__(self): 368 self.start = time.time() 369 return self 370 371 def __exit__(self, *args): 372 self.end = time.time() 373 self.duration = self.end - self.start 406 374 407 375 def timed(src, timeout): 408 376 expire = time.time() + timeout 409 377 i = iter(src) 410 with contextlib.suppress(StopIteration): 411 while True: 412 yield i.next(max(expire - time.time(), 0)) 413 414 def fmtDur( duration ): 415 if duration : 416 hours, rem = divmod(duration, 3600) 417 minutes, rem = divmod(rem, 60) 418 seconds, millis = divmod(rem, 1) 419 return "%2d:%02d.%03d" % (minutes, seconds, millis * 1000) 420 return " n/a" 378 while True: 379 yield i.next(max(expire - time.time(), 0))
Note:
See TracChangeset
for help on using the changeset viewer.