2022-01-13 07:08:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from datetime import datetime
|
|
|
|
|
2022-01-13 10:45:45 +00:00
|
|
|
import subprocess
|
|
|
|
|
2022-01-13 07:08:54 +00:00
|
|
|
from settings import *
|
|
|
|
from logger import Logger
|
|
|
|
|
|
|
|
|
|
|
|
logger = Logger()
|
|
|
|
|
|
|
|
|
|
|
|
def trigger_export(courseid, categoryid):
|
2022-01-13 16:19:32 +00:00
|
|
|
output_name = f"course_{courseid}_{categoryid}_{int(datetime.now().timestamp())}.mbz"
|
|
|
|
result = subprocess.run([MOOSH_PATH, "-p", MOODLE_PATH, "course-backup",
|
|
|
|
"-p", OUTPUT_PATH, "-f", output_name, "--template", courseid],
|
2022-01-14 07:10:33 +00:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
for message in result.stdout.decode().splitlines():
|
|
|
|
logger.debug(message)
|
|
|
|
for error in result.stderr.decode().splitlines():
|
|
|
|
logger.error(error)
|
2022-01-13 07:08:54 +00:00
|
|
|
return output_name
|
|
|
|
|
|
|
|
|
2022-01-14 07:10:33 +00:00
|
|
|
def trigger_categories_export():
|
|
|
|
OUTPUT_PATH = Path(OUTPUT_PATH)
|
|
|
|
|
|
|
|
output_name = f"categories_{int(datetime.now().timestamp())}.xml"
|
|
|
|
result = subprocess.run([MOOSH_PATH, "-p", MOODLE_PATH, "category-export", "0"],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(OUTPUT_PATH / output_name, "wb") as xmlfile:
|
|
|
|
xmlfile.write(result.stdout)
|
|
|
|
except IOError as e:
|
|
|
|
logger.error(
|
|
|
|
f"Could not write categories to {OUTPUT_PATH / output_name}: {e}")
|
|
|
|
|
|
|
|
for error in result.stderr.decode().splitlines():
|
|
|
|
logger.error(error)
|
|
|
|
|
|
|
|
|
2022-01-13 07:08:54 +00:00
|
|
|
def main():
|
2022-01-13 09:53:36 +00:00
|
|
|
global SIGNAL_PATH, OUTPUT_PATH
|
2022-01-13 07:08:54 +00:00
|
|
|
logger.info("Moodle Export Worker starting")
|
|
|
|
SIGNAL_PATH = Path(SIGNAL_PATH)
|
2022-01-13 10:52:03 +00:00
|
|
|
OUTPUT_PATH = Path(OUTPUT_PATH)
|
2022-01-13 07:08:54 +00:00
|
|
|
|
2022-01-13 09:53:36 +00:00
|
|
|
if SIGNAL_PATH.exists():
|
|
|
|
if not SIGNAL_PATH.is_dir():
|
2022-01-13 07:08:54 +00:00
|
|
|
logger.fatal(
|
|
|
|
f"Cannot create signalling directory {SIGNAL_PATH} because a file with that name exists.")
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
try:
|
2022-01-13 09:53:36 +00:00
|
|
|
SIGNAL_PATH.mkdir()
|
2022-01-13 07:08:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.fatal(
|
|
|
|
f"Cannot create signalling directory {SIGNAL_PATH}: {e}")
|
|
|
|
exit(1)
|
|
|
|
|
2022-01-13 09:53:36 +00:00
|
|
|
for f in SIGNAL_PATH.glob("*-*.mew"):
|
2022-01-13 07:08:54 +00:00
|
|
|
logger.debug(f"Found file {f.name} - start processing")
|
|
|
|
try:
|
|
|
|
pre = datetime.now()
|
|
|
|
output_name = trigger_export(*f.name.split(".")[0].split("-"))
|
|
|
|
dur = datetime.now() - pre
|
|
|
|
size = Path(OUTPUT_PATH / output_name).stat().st_size
|
|
|
|
f.unlink()
|
|
|
|
logger.debug(
|
2022-01-13 16:19:32 +00:00
|
|
|
f"Created file {output_name} ({size} bytes) in {dur.seconds} seconds")
|
2022-01-13 07:08:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Export of course {f.name.split('.')[0].split('-')[0]} failed: {e}")
|
|
|
|
|
2022-01-14 07:10:33 +00:00
|
|
|
if (SIGNAL_PATH / "categories.mew").exists():
|
|
|
|
logger.debug(f"Found file categories.mew - processing categories")
|
|
|
|
try:
|
|
|
|
pre = datetime.now()
|
|
|
|
output_name = trigger_categories_export()
|
|
|
|
dur = datetime.now() - pre
|
|
|
|
size = Path(OUTPUT_PATH / output_name).stat().st_size
|
|
|
|
f.unlink()
|
|
|
|
logger.debug(
|
|
|
|
f"Created file {output_name} ({size} bytes) in {dur.seconds} seconds")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Export of categories failed: {e}")
|
|
|
|
|
2022-01-13 09:53:36 +00:00
|
|
|
logger.info("Moodle Export Worker done")
|
|
|
|
|
2022-01-13 07:08:54 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-01-13 16:19:32 +00:00
|
|
|
main()
|