Проверка шаблона шины для iOS
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

generate-platforms 2.5KB

pirms 11 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import json
  5. import subprocess
  6. from datetime import datetime
  7. from hashlib import sha256
  8. from tempfile import TemporaryDirectory
  9. from shutil import copyfile
  10. FILENAME = 'Platfile'
  11. LOCKFILE = FILENAME + '.lock'
  12. VERSION = '2.0'
  13. def initialize(dir: str):
  14. print('Initializing ', FILENAME)
  15. dir_modules = os.path.join(dir, '..', '..', 'Modules')
  16. path = os.path.join(dir, FILENAME)
  17. with open(path, 'w') as file:
  18. entries = os.listdir(dir_modules)
  19. for entry in entries:
  20. if os.path.isfile(os.path.join(dir_modules, entry, entry + '.yml')):
  21. file.write(entry + '\n')
  22. def generate(dir: str, generator: str):
  23. path = os.path.join(dir, FILENAME)
  24. with open(path) as file:
  25. modules = file.readlines()
  26. modules = set(m.strip() for m in modules if m.strip())
  27. modules = sorted(modules)
  28. dir_modules = os.path.join(dir, '..', '..', 'Modules')
  29. print(f'Generating modules: {len(modules)}')
  30. generator = generator or os.path.join(dir, 'generate-platform')
  31. for module in modules:
  32. src_dir = os.path.join(dir_modules, module, 'src')
  33. with TemporaryDirectory() as tmpdir:
  34. subprocess.call([generator, module, '-o', tmpdir])
  35. _merge_changes(tmpdir, src_dir)
  36. if os.path.exists(LOCKFILE):
  37. os.remove(LOCKFILE)
  38. def _merge_changes(src_dir: str, dst_dir: str):
  39. for file_name in os.listdir(src_dir):
  40. src_file = os.path.join(src_dir, file_name)
  41. src_hash = _hash_file(src_file)
  42. dst_file = os.path.join(dst_dir, file_name)
  43. if os.path.exists(dst_file):
  44. dst_hash = _hash_file(dst_file)
  45. if src_hash == dst_hash:
  46. return
  47. copyfile(src_file, dst_file)
  48. print('Updated file: ', file_name)
  49. def _hash_file(path: str):
  50. alg = sha256()
  51. with open(path, 'rb') as file:
  52. for line in file.readlines():
  53. if line:
  54. alg.update(line)
  55. return alg.hexdigest()
  56. def main():
  57. from argparse import ArgumentParser
  58. parser = ArgumentParser(
  59. prog='GeneratePlatforms',
  60. description='Automatic code generator'
  61. )
  62. parser.add_argument('-i', '--init', action='store_true')
  63. parser.add_argument('-g', '--generator', type=str)
  64. args = parser.parse_args()
  65. dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  66. if args.init:
  67. initialize(dir)
  68. else:
  69. generate(dir, args.generator)
  70. if __name__ == "__main__":
  71. main()