waffiles2cmake.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) Contributors to the Open 3D Engine Project.
  4. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR MIT
  7. #
  8. #
  9. from __future__ import (absolute_import, division,
  10. print_function, unicode_literals)
  11. import sys
  12. import json
  13. import os
  14. import subprocess
  15. import argparse
  16. def get_banner():
  17. return """#
  18. # Copyright (c) Contributors to the Open 3D Engine Project.
  19. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  20. #
  21. # SPDX-License-Identifier: Apache-2.0 OR MIT
  22. #
  23. #
  24. """
  25. def convertFile(input_file):
  26. filename = os.path.basename(input_file)
  27. path = os.path.dirname(os.path.abspath(input_file))
  28. outFilename = (os.path.splitext(filename)[0] + '_files.cmake').lower()
  29. output_file = os.path.join(path, outFilename)
  30. print('Converting ' + os.path.abspath(input_file) + ' to ' + output_file)
  31. # parse input file
  32. try:
  33. with open(input_file, 'r') as source_file:
  34. waf_files = json.load(source_file)
  35. except IOError:
  36. print('Error opening ' + input_file)
  37. sys.exit(1)
  38. except ValueError:
  39. print('Error parsing ' + input_file + ': invalid JSON!')
  40. sys.exit(1)
  41. files_list = []
  42. for (i, j) in waf_files.items():
  43. for (k, grp) in j.items():
  44. for fname in grp:
  45. files_list.append(fname)
  46. alreadyExists = os.path.exists(output_file)
  47. if alreadyExists:
  48. subprocess.run(['p4', 'edit', output_file])
  49. # build output file list
  50. try:
  51. fhandle = open(output_file, 'w+')
  52. fhandle.write(get_banner() + '\nset(FILES\n')
  53. for fname in files_list:
  54. fhandle.write(' ' + fname + '\n')
  55. fhandle.write(')\n')
  56. except IOError:
  57. print('Error creating ' + output_file)
  58. if not alreadyExists:
  59. subprocess.run(['p4', 'add', output_file])
  60. def convertPath(input_path):
  61. for dp, dn, filenames in os.walk(input_path):
  62. for f in filenames:
  63. if os.path.splitext(f)[1] == '.waf_files':
  64. convertFile(os.path.join(dp, f))
  65. def main():
  66. """script main function"""
  67. parser = argparse.ArgumentParser(description='wafffiles2cmake.py (will recursively convert all waf_files)\n'
  68. 'output: [file_or_dir. ..].cmake\n', formatter_class=argparse.RawTextHelpFormatter)
  69. parser.add_argument('file_or_dir', type=str, nargs='+',
  70. help='list of files or directories to look for *.waf_files within and convert to cmake files')
  71. args = parser.parse_args()
  72. for input_file in args.file_or_dir:
  73. print(input_file)
  74. if os.path.splitext(input_file)[1] == '.waf_files':
  75. convertFile(input_file)
  76. elif os.path.isdir(input_file):
  77. for dp, dn, filenames in os.walk(input_file):
  78. for f in filenames:
  79. if os.path.splitext(f)[1] == '.waf_files':
  80. convertFile(os.path.join(dp, f))
  81. #entrypoint
  82. if __name__ == '__main__':
  83. main()