Initial commit
This commit is contained in:
commit
d94895c6d7
3 changed files with 62 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
config.ini
|
||||||
|
*.pyc
|
||||||
|
__pycache__/
|
5
config.dist.ini
Normal file
5
config.dist.ini
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[IMPORTER]
|
||||||
|
SourcePath = /replication/courses/
|
||||||
|
PHPScript = /var/www/html/moodle/local/replication/import_cli.php
|
||||||
|
|
||||||
|
[STATUS]
|
54
worker.py
Normal file
54
worker.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
from pathlib import Path
|
||||||
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
|
||||||
|
config = ConfigParser()
|
||||||
|
|
||||||
|
config.read("config.ini")
|
||||||
|
|
||||||
|
source = Path(config["IMPORTER"]["SourcePath"])
|
||||||
|
completed = source / "completed"
|
||||||
|
completed.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
courses = dict()
|
||||||
|
|
||||||
|
mbzre = re.compile(r"course_(?P<courseid>\d+)_(?P<categoryid>\d+)_(?P<timestamp>\d+).mbz")
|
||||||
|
|
||||||
|
def call_php(courseid: int, categoryid: int, timestamp: int):
|
||||||
|
try:
|
||||||
|
subprocess.run(["php", config["IMPORTER"]["PHPScript"], f"--courseid={courseid}", f"--categoryid={categoryid}", f"--timestamp={timestamp}"], cwd=Path(config["IMPORTER"]["PHPScript"]).parent, check=True)
|
||||||
|
return True
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
for mbz in source.glob("course_*_*_*.mbz"):
|
||||||
|
meta = mbzre.search(mbz).groupdict()
|
||||||
|
|
||||||
|
if not meta["courseid"] in courses.keys():
|
||||||
|
courses[int(meta["courseid"])] = list()
|
||||||
|
|
||||||
|
courses[int(meta["courseid"])].append((int(meta["categoryid"]), int(meta["timestamp"])))
|
||||||
|
|
||||||
|
|
||||||
|
for course, data in sorted(courses.items(), key = lambda x: x[0]):
|
||||||
|
current = config.getint("STATUS", str(course), fallback=0)
|
||||||
|
|
||||||
|
byage = sorted(data, key = lambda x: -x[1])
|
||||||
|
|
||||||
|
if byage[0][1] > current:
|
||||||
|
if not call_php(course, byage[0][0], byage[0][1]):
|
||||||
|
print("Something went wrong while importing {course}.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
config["STATUS"][str(course)] = str(byage[0][1])
|
||||||
|
|
||||||
|
for categoryid, timestamp in byage:
|
||||||
|
shutil.move(source / f"course_{course}_{categoryid}_{timestamp}.mbz", completed)
|
||||||
|
|
||||||
|
with open("config.ini", "w") as configfile:
|
||||||
|
config.write(configfile)
|
Loading…
Reference in a new issue