modified the init file to work on local directory; added logging to init.py
This commit is contained in:
81
app/init.py
81
app/init.py
@@ -1,10 +1,12 @@
|
||||
import paramiko
|
||||
from os.path import isdir
|
||||
from stat import S_ISDIR, S_ISREG
|
||||
import re
|
||||
import pathlib
|
||||
|
||||
# from base64 import decodebytes
|
||||
import json
|
||||
import mariadb
|
||||
import logging
|
||||
|
||||
CATEGORIES = [
|
||||
"Prüfungen",
|
||||
@@ -16,7 +18,18 @@ CATEGORIES = [
|
||||
"Multimedia",
|
||||
]
|
||||
SUBCAT_CATEGORIES = ["Klausuren", "Übungen", "Labore"]
|
||||
unizeug_path = "/mnt/save/daten/Unizeug/"
|
||||
unizeug_path = "/home/wildarch/web/fet_unizeug/unizeug/"
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
logging.basicConfig(
|
||||
filename="init.log",
|
||||
level=logging.INFO,
|
||||
format="[%(asctime)s, %(filename)s:%(lineno)s -> %(funcName)10s() ]%(levelname)s: %(message)s",
|
||||
)
|
||||
debug = log.debug
|
||||
info = log.info
|
||||
error = log.error
|
||||
|
||||
db = mariadb.connect(
|
||||
host="localhost", user="wildserver", password="DBPassword", database="Unizeug"
|
||||
)
|
||||
@@ -53,29 +66,31 @@ db.commit()
|
||||
|
||||
|
||||
def get_dirstruct():
|
||||
with open("app/pwfile.json", "r") as f:
|
||||
cred = json.load(f)
|
||||
ssh = paramiko.SSHClient()
|
||||
print(cred["sftpurl"])
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
# with open("app/pwfile.json", "r") as f:
|
||||
# cred = json.load(f)
|
||||
# ssh = paramiko.SSHClient()
|
||||
# print(cred["sftpurl"])
|
||||
# ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
# key=paramiko.RSAKey(data=decodebytes(bytes(cred["key"],"utf-8")))
|
||||
# ssh.get_host_keys().add(cred["sftpurl"], 'ssh-rsa', key)
|
||||
ssh.connect(cred["sftpurl"], username=cred["sftpuser"], password=cred["sftpPW"])
|
||||
sftp = ssh.open_sftp()
|
||||
folders = sftp.listdir_attr(unizeug_path)
|
||||
for entry in folders:
|
||||
# ssh.connect(cred["sftpurl"], username=cred["sftpuser"], password=cred["sftpPW"])
|
||||
# sftp = ssh.open_sftp()
|
||||
# folders = sftp.listdir_attr(unizeug_path)
|
||||
folders=pathlib.Path(unizeug_path)
|
||||
for entry in folders.iterdir():
|
||||
if entry is None:
|
||||
continue
|
||||
if not S_ISDIR(entry.st_mode):
|
||||
if not entry.is_dir():
|
||||
continue
|
||||
fname = str(entry.filename)
|
||||
fname = str(entry.name)
|
||||
regex = re.compile(r"Multimedia_only")
|
||||
if regex.search(fname):
|
||||
continue
|
||||
# print(fname)
|
||||
lvid = re.search(r"[a-zA-Z0-9]{3}\.[a-zA-Z0-9]{3}", fname)
|
||||
print(lvid)
|
||||
# print(lvid)
|
||||
if lvid is None:
|
||||
error(f"Didnt Find LVA ID in Directory {fname}")
|
||||
continue
|
||||
lvid = lvid.group()[:3] + lvid.group()[4:]
|
||||
# name = fname[:-8]
|
||||
@@ -89,39 +104,35 @@ def get_dirstruct():
|
||||
cur.execute("SELECT id FROM LVAs WHERE lvid=?", (lvid,))
|
||||
lid = cur.fetchone()[0]
|
||||
db.commit()
|
||||
for profsdir in sftp.listdir_attr(unizeug_path + fname + "/"):
|
||||
if profsdir is None or not S_ISDIR(profsdir.st_mode):
|
||||
for profsdir in entry.iterdir():
|
||||
if profsdir is None:
|
||||
continue
|
||||
if not profsdir.is_dir():
|
||||
continue
|
||||
# print(profsdir.filename)
|
||||
try:
|
||||
lastname, firstname = re.split(r"[_\-\s]", str(profsdir.filename))
|
||||
lastname, firstname = re.split(r"[_\-\s]", str(profsdir.name))
|
||||
pid = link_prof(firstname, lastname, lid)
|
||||
except ValueError:
|
||||
print(f"{name} is broken")
|
||||
error(f"Couldnt get Profs from {fname}")
|
||||
continue
|
||||
for cat in sftp.listdir_attr(
|
||||
unizeug_path + fname + "/" + profsdir.filename + "/"
|
||||
):
|
||||
if cat is None or not S_ISDIR(cat.st_mode):
|
||||
for cat in profsdir.iterdir():
|
||||
if cat is None:
|
||||
continue
|
||||
if cat.filename not in SUBCAT_CATEGORIES:
|
||||
if not cat.is_dir():
|
||||
continue
|
||||
idx = CATEGORIES.index(cat.filename)
|
||||
for subcat in sftp.listdir_attr(
|
||||
unizeug_path
|
||||
+ fname
|
||||
+ "/"
|
||||
+ profsdir.filename
|
||||
+ "/"
|
||||
+ cat.filename
|
||||
+ "/"
|
||||
):
|
||||
if subcat is None or not S_ISDIR(subcat.st_mode):
|
||||
if cat.name not in SUBCAT_CATEGORIES:
|
||||
continue
|
||||
idx = CATEGORIES.index(cat.name)
|
||||
for subcat in cat.iterdir():
|
||||
if subcat is None:
|
||||
continue
|
||||
if not subcat.is_dir():
|
||||
continue
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
"INSERT INTO SubCats (LId,PId,cat,name) VALUES(?,?,?,?)",
|
||||
(lid, pid, idx, subcat.filename),
|
||||
(lid, pid, idx, subcat.name),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user