dircondenser.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. '''Renames test case directories using Git from this:
  13. 1 something
  14. 3 other
  15. 3 foo
  16. 3 bar
  17. to this:
  18. 1 something
  19. 2 other
  20. 3 foo
  21. 4 bar
  22. This directory must be run from source root as it touches run_unittests.py.
  23. '''
  24. import typing as T
  25. import os
  26. import sys
  27. import subprocess
  28. from glob import glob
  29. def get_entries() -> T.List[T.Tuple[int, str]]:
  30. entries = []
  31. for e in glob('*'):
  32. if not os.path.isdir(e):
  33. raise SystemExit('Current directory must not contain any files.')
  34. (number, rest) = e.split(' ', 1)
  35. try:
  36. numstr = int(number)
  37. except ValueError:
  38. raise SystemExit(f'Dir name {e} does not start with a number.')
  39. entries.append((numstr, rest))
  40. entries.sort()
  41. return entries
  42. def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]) -> None:
  43. with open(sourcefile, encoding='utf-8') as f:
  44. contents = f.read()
  45. for old_name, new_name in replacements:
  46. contents = contents.replace(old_name, new_name)
  47. with open(sourcefile, 'w', encoding='utf-8') as f:
  48. f.write(contents)
  49. def condense(dirname: str) -> None:
  50. curdir = os.getcwd()
  51. os.chdir(dirname)
  52. entries = get_entries()
  53. replacements = []
  54. for _i, e in enumerate(entries):
  55. i = _i + 1
  56. if e[0] != i:
  57. old_name = str(e[0]) + ' ' + e[1]
  58. new_name = str(i) + ' ' + e[1]
  59. #print('git mv "%s" "%s"' % (old_name, new_name))
  60. subprocess.check_call(['git', 'mv', old_name, new_name])
  61. replacements.append((old_name, new_name))
  62. # update any appearances of old_name in expected stdout in test.json
  63. json = os.path.join(new_name, 'test.json')
  64. if os.path.isfile(json):
  65. replace_source(json, [(old_name, new_name)])
  66. os.chdir(curdir)
  67. replace_source('run_unittests.py', replacements)
  68. replace_source('run_project_tests.py', replacements)
  69. for f in glob('unittests/*.py'):
  70. replace_source(f, replacements)
  71. if __name__ == '__main__':
  72. if len(sys.argv) != 1:
  73. raise SystemExit('This script takes no arguments.')
  74. for d in glob('test cases/*'):
  75. condense(d)