fix_tabs.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import fnmatch
  10. import os
  11. handled_file_patterns = [
  12. '*.c', '*.cc', '*.cpp', '*.cxx', '*.h', '*.hpp', '*.hxx', '*.inl', '*.m', '*.mm', '*.cs', '*.java',
  13. '*.py', '*.lua', '*.bat', '*.cmd', '*.sh', '*.js',
  14. '*.cmake', 'CMakeLists.txt'
  15. ]
  16. def fixTabs(input_file):
  17. try:
  18. basename = os.path.basename(input_file)
  19. for pattern in handled_file_patterns:
  20. if fnmatch.fnmatch(basename, pattern):
  21. with open(input_file, 'r') as source_file:
  22. fileContents = source_file.read()
  23. if '\t' in fileContents:
  24. newFileContents = fileContents.replace('\t', ' ')
  25. with open(input_file, 'w') as destination_file:
  26. destination_file.write(newFileContents)
  27. print(f'[INFO] Patched {input_file}')
  28. break
  29. except (IOError, UnicodeDecodeError) as err:
  30. print('[ERROR] reading {}: {}'.format(input_file, err))
  31. return
  32. def main():
  33. """script main function"""
  34. parser = argparse.ArgumentParser(description='This script replaces tabs with spaces',
  35. formatter_class=argparse.RawTextHelpFormatter)
  36. parser.add_argument('file_or_dir', type=str, nargs='+',
  37. help='list of files or directories to search within for files to fix up tabs')
  38. args = parser.parse_args()
  39. for input_file in args.file_or_dir:
  40. if os.path.isdir(input_file):
  41. for dp, dn, filenames in os.walk(input_file):
  42. for f in filenames:
  43. fixTabs(os.path.join(dp, f))
  44. else:
  45. fixTabs(input_file)
  46. #entrypoint
  47. if __name__ == '__main__':
  48. main()