1
0

time-killer.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from collections import defaultdict, Counter
  2. from pathlib import Path
  3. SALT_PATH = Path('~/util/tests').expanduser().resolve()
  4. def skip_class(classname):
  5. filename, _, classname = classname.rpartition('.')
  6. filename = SALT_PATH / (filename.replace('.', '/')+'.py')
  7. classline = "class "+classname
  8. print('Updating:', filename, classname)
  9. with filename.open(mode='r+') as f:
  10. lines = f.readlines()
  11. last_import_line = 0
  12. has_import = False
  13. class_line_idx = 0
  14. for i, line in enumerate(lines):
  15. if line.startswith('import ') or line.startswith('from '):
  16. last_import_line = i
  17. if 'tests.support.unit' in line and 'skipIf' in line:
  18. has_import = True
  19. if classline in line:
  20. class_line_idx = i
  21. if not has_import:
  22. lines.insert(last_import_line, 'from tests.support.unit import skipIf # WAR ROOM temp import\n')
  23. class_line_idx += 1
  24. f.seek(0)
  25. f.truncate()
  26. f.write(''.join(lines))
  27. if 'WAR ROOM' not in lines[class_line_idx-1]:
  28. lines.insert(class_line_idx, '@skipIf(True, "WAR ROOM TEMPORARY SKIP")\n')
  29. f.seek(0)
  30. f.truncate()
  31. f.write(''.join(lines))
  32. def skip_file(filename):
  33. filename = SALT_PATH / (filename.replace('.', '/')+'.py')
  34. print('Updating:', filename)
  35. with filename.open(mode='r+') as f:
  36. lines = f.readlines()
  37. last_import_line = 0
  38. has_import = False
  39. has_skip = False
  40. for i, line in enumerate(lines):
  41. if line.startswith('import ') or line.startswith('from '):
  42. last_import_line = i
  43. if 'tests.support.unit' in line and 'skipIf' in line:
  44. has_import = True
  45. if 'skipIf(True, "WAR ROOM"' in line:
  46. has_skip = True
  47. if not has_import:
  48. lines.insert(last_import_line, 'from tests.support.unit import skipIf; skipIf(True, "WAR ROOM TEMPORARY SKIP") # pylint: disable=C0321,E8702\n')
  49. f.seek(0)
  50. f.truncate()
  51. f.write(''.join(lines))
  52. elif not has_skip:
  53. lines.insert(last_import_line, 'skipIf(True, "WAR ROOM TEMPORARY SKIP")')
  54. if __name__ == '__main__':
  55. with open('/tmp/times.txt') as f:
  56. # Couple of header lines
  57. f.readline()
  58. f.readline()
  59. tests = f.readlines()[:100]
  60. by_module = defaultdict(list)
  61. for test in tests:
  62. name = test.split(None, 1)[0]
  63. module = name.rpartition('.')[0]
  64. by_module[module].append(name)
  65. counts = Counter()
  66. for mod in by_module:
  67. counts[mod] = len(by_module[mod])
  68. for name, count in counts.most_common():
  69. if count == 1:
  70. skip_class(by_module[name][0])
  71. else:
  72. skip_file(name)