mirror of https://github.com/stijndcl/didier
Use importlib for db scripts
parent
181118aa1d
commit
1fe04b3687
|
@ -4,14 +4,10 @@ This is slightly ugly, but running the scripts directly isn't possible because o
|
||||||
This could be cleaned up a bit using importlib but this is safer
|
This could be cleaned up a bit using importlib but this is safer
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import importlib
|
||||||
import sys
|
import sys
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from database.scripts.db00_example import main as debug_add_courses
|
|
||||||
|
|
||||||
script_mapping: dict[str, Callable] = {"debug_add_courses.py": debug_add_courses}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
scripts = sys.argv[1:]
|
scripts = sys.argv[1:]
|
||||||
if not scripts:
|
if not scripts:
|
||||||
|
@ -19,10 +15,13 @@ if __name__ == "__main__":
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
for script in scripts:
|
for script in scripts:
|
||||||
script_main = script_mapping.get(script.removeprefix("database/scripts/"), None)
|
script = script.replace("/", ".").removesuffix(".py")
|
||||||
if script_main is None:
|
module = importlib.import_module(script)
|
||||||
print(f'Script "{script}" not found.', file=sys.stderr)
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
script_main: Callable = module.main
|
||||||
asyncio.run(script_main())
|
asyncio.run(script_main())
|
||||||
print(f"Successfully ran {script}")
|
print(f"Successfully ran {script}")
|
||||||
|
except AttributeError:
|
||||||
|
print(f'Script "{script}" not found.', file=sys.stderr)
|
||||||
|
exit(1)
|
||||||
|
|
Loading…
Reference in New Issue