-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_db_scripts.py
33 lines (26 loc) · 953 Bytes
/
run_db_scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""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
import importlib
import sys
from typing import Callable
async def main():
"""Try to parse all command-line arguments into modules and run them sequentially"""
scripts = sys.argv[1:]
if not scripts:
print("No scripts provided.", file=sys.stderr)
exit(1)
for script in scripts:
script = script.replace("/", ".").removesuffix(".py")
module = importlib.import_module(script)
try:
script_main: Callable = module.main
await script_main()
print(f"Successfully ran {script}")
except AttributeError:
print(f'Script "{script}" not found.', file=sys.stderr)
exit(1)
if __name__ == "__main__":
asyncio.run(main())