manage.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import argparse
  2. import os
  3. import shutil
  4. import subprocess
  5. import sys
  6. from anonflow import __version_str__, paths
  7. def check_cmd(cmd: str):
  8. return shutil.which(cmd) is not None
  9. def check_translations():
  10. mo_files, po_files = set(), set()
  11. for root, dirs, files in os.walk(paths.TRANSLATIONS_DIR):
  12. for file in files:
  13. if file.endswith(".mo"):
  14. mo_files.add(file[:-3])
  15. if file.endswith(".po"):
  16. po_files.add(file[:-3])
  17. return mo_files == po_files
  18. def compile_translations():
  19. if not check_cmd("pybabel"):
  20. raise RuntimeError("pybabel not found.")
  21. subprocess.run(["pybabel", "compile", "-d", paths.TRANSLATIONS_DIR], check=True)
  22. print("Translations compilation done.")
  23. def make_migrations():
  24. if not check_cmd("alembic"):
  25. raise RuntimeError("alembic not found.")
  26. subprocess.run(["alembic", "revision", "--autogenerate"], check=True)
  27. def migrate(revision: str):
  28. if not check_cmd("alembic"):
  29. raise RuntimeError("alembic not found.")
  30. subprocess.run(["alembic", "upgrade", revision], check=True)
  31. def main():
  32. os.environ["VERSION"] = __version_str__
  33. parser = argparse.ArgumentParser(description="Anonflow project manager")
  34. subparsers = parser.add_subparsers(dest="command", required=True)
  35. subparsers.add_parser("start", help="Run the application locally")
  36. deploy_parser = subparsers.add_parser("deploy", help="Build and manage containers via docker compose")
  37. deploy_parser.add_argument("docker_args", nargs=argparse.REMAINDER, help="Additional docker compose commands")
  38. subparsers.add_parser("makemigrations", help="Generate new Alembic migration")
  39. migrate_parser = subparsers.add_parser("migrate", help="Apply all Alembic migrations")
  40. migrate_parser.add_argument("revision", nargs="?", default="head", help="Revision to upgrade to (default: head)")
  41. args = parser.parse_args()
  42. try:
  43. if not check_translations():
  44. compile_translations()
  45. if args.command == "start":
  46. print("Running 'python -m anonflow'")
  47. subprocess.run([sys.executable, "-m", "anonflow"], check=True)
  48. elif args.command == "deploy":
  49. if not args.docker_args:
  50. print("Running 'docker compose up --build'")
  51. subprocess.run(["docker", "compose", "up", "--build"])
  52. else:
  53. print(f"Running 'docker compose {' '.join(args.docker_args)}'")
  54. subprocess.run(["docker", "compose"] + args.docker_args)
  55. elif args.command == "makemigrations":
  56. make_migrations()
  57. elif args.command == "migrate":
  58. migrate(args.revision)
  59. except KeyboardInterrupt:
  60. pass
  61. if __name__ == "__main__":
  62. main()