didier/run_db_scripts.py

34 lines
953 B
Python
Raw Normal View History

2022-09-17 23:20:46 +02:00
"""Script to run database-related scripts
This is slightly ugly, but running the scripts directly isn't possible because of imports
This could be cleaned up a bit using importlib but this is safer
"""
import asyncio
2022-09-20 01:07:36 +02:00
import importlib
2022-09-17 23:20:46 +02:00
import sys
from typing import Callable
2022-10-19 22:37:17 +02:00
async def main():
"""Try to parse all command-line arguments into modules and run them sequentially"""
2022-09-17 23:20:46 +02:00
scripts = sys.argv[1:]
if not scripts:
print("No scripts provided.", file=sys.stderr)
exit(1)
for script in scripts:
2022-09-20 01:07:36 +02:00
script = script.replace("/", ".").removesuffix(".py")
module = importlib.import_module(script)
try:
script_main: Callable = module.main
2022-10-19 22:37:17 +02:00
await script_main()
2022-09-20 01:07:36 +02:00
print(f"Successfully ran {script}")
except AttributeError:
2022-09-17 23:20:46 +02:00
print(f'Script "{script}" not found.', file=sys.stderr)
exit(1)
2022-10-19 22:37:17 +02:00
if __name__ == "__main__":
asyncio.run(main())