split.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. import os
  3. import shutil
  4. import re
  5. def replace_in_file(filename, old, new, flags=None):
  6. with open(filename) as f:
  7. if flags is None:
  8. content = re.sub(old, new, f.read())
  9. else:
  10. content = re.sub(old, new, f.read(), flags=flags)
  11. with open(filename, "w") as f:
  12. f.write(content)
  13. output_dir = "foo"
  14. os.makedirs(output_dir, exist_ok=True)
  15. examples_dir = "examples"
  16. categories = os.listdir(examples_dir)
  17. for category in categories:
  18. category_path = os.path.join(examples_dir, category)
  19. if os.path.isfile(category_path):
  20. continue
  21. if category == "_main":
  22. continue
  23. print("category", category)
  24. for example in os.listdir(category_path):
  25. print(" example", example)
  26. example_out_dir = os.path.join(output_dir, category + "_" + example)
  27. os.makedirs(example_out_dir, exist_ok=True)
  28. shutil.copytree("assets", os.path.join(example_out_dir, "assets"), dirs_exist_ok=True)
  29. shutil.copytree("input", os.path.join(example_out_dir, "input"), dirs_exist_ok=True)
  30. shutil.copyfile("game.project", os.path.join(example_out_dir, "game.project"))
  31. shutil.copyfile("all.texture_profiles", os.path.join(example_out_dir, "all.texture_profiles"))
  32. shutil.copyfile(".gitignore", os.path.join(example_out_dir, ".gitignore"))
  33. example_src = os.path.join(examples_dir, category, example)
  34. example_dst = os.path.join(example_out_dir, "example")
  35. shutil.copytree(example_src, example_dst, dirs_exist_ok=True)
  36. replace_in_file(os.path.join(example_out_dir, "game.project"), r"/examples/main.collectionc", r"/example/" + example + ".collectionc")
  37. for file in os.listdir(example_dst):
  38. file_path = os.path.join(example_dst, file)
  39. name, ext = os.path.splitext(file)
  40. if os.path.isdir(file_path):
  41. continue
  42. if ext in [".png", ".wav", ".ogg", ".jpg"]:
  43. continue
  44. # /examples/animation/basic_tween/
  45. src = r"/examples/" + category + "/" + example + "/"
  46. tgt = r"/example/"
  47. replace_in_file(file_path, src, tgt)
  48. if ext == ".md":
  49. replace_in_file(file_path, r"title", r"tags: " + category + "\ntitle")