Implement category export
This commit is contained in:
parent
eeb4580f99
commit
7dc57a8abc
1 changed files with 37 additions and 4 deletions
41
worker.py
41
worker.py
|
@ -13,16 +13,35 @@ logger = Logger()
|
|||
|
||||
|
||||
def trigger_export(courseid, categoryid):
|
||||
global MOODLE_PATH, OUTPUT_PATH
|
||||
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],
|
||||
capture_output=True)
|
||||
for line in str(result.stdout).splitlines():
|
||||
logger.debug(line)
|
||||
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)
|
||||
return output_name
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def main():
|
||||
global SIGNAL_PATH, OUTPUT_PATH
|
||||
logger.info("Moodle Export Worker starting")
|
||||
|
@ -56,6 +75,20 @@ def main():
|
|||
logger.error(
|
||||
f"Export of course {f.name.split('.')[0].split('-')[0]} failed: {e}")
|
||||
|
||||
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}")
|
||||
|
||||
logger.info("Moodle Export Worker done")
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue