From 01838f382085dfc1a4ddb9c02f07fa9dec9d232a Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 7 Mar 2019 18:17:39 +0100 Subject: [PATCH] partitionen angefangen --- dirkules/__init__.py | 9 +++ dirkules/driveManagement/driveController.py | 65 +++++++++------------ dirkules/models.py | 8 +-- dirkules/views.py | 4 +- setup.py | 1 - 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/dirkules/__init__.py b/dirkules/__init__.py index 3b3646c..2714a77 100644 --- a/dirkules/__init__.py +++ b/dirkules/__init__.py @@ -10,4 +10,13 @@ db = SQLAlchemy(app) import dirkules.models db.create_all() +from dirkules.models import Time +from sqlalchemy.orm.exc import NoResultFound + +try: + Time.query.one() +except NoResultFound: + db.session.add(Time("Drives")) + db.session.commit() + import dirkules.views diff --git a/dirkules/driveManagement/driveController.py b/dirkules/driveManagement/driveController.py index 61a8f66..03ac4cc 100644 --- a/dirkules/driveManagement/driveController.py +++ b/dirkules/driveManagement/driveController.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -import psutil import subprocess import os from dirkules.models import Drive @@ -9,7 +8,7 @@ from dirkules import db def getAllDrives(): #vorbereitung drives = [] - driveDict = [] + driveDict = [] # ist eine Liste, enthält für jede HDD ein dict keys = ['device', 'name', 'smart', 'size'] blkid = subprocess.Popen(["hwinfo --disk --short"], @@ -28,6 +27,7 @@ def getAllDrives(): break blkid.stdout.close() for line in drives: + # Effizienter machen mit newLine = ' '.join(line.split()) values = [] line = line.replace(" ", "", 15) values.append(line[:8]) @@ -63,7 +63,6 @@ def smartPassed(device): def getTotalSize(device): - # Hier könnte man auch die Partitionen mit abfragen drives = [] fdisk = subprocess.Popen(["fdisk -l"], stdout=subprocess.PIPE, @@ -85,19 +84,18 @@ def getTotalSize(device): return size -#nicht verwenden -def OLDgetAllDrives(): - - #vorbereitung +def getPartitions(device): + partDict = [] # ist eine Liste, enthält für jede part ein dict drives = [] - driveDict = [] - keys = ['device', 'mountpoint', 'fstype', 'label'] + # name = sda1 zum Beispiel + keys = ['name', 'fs', 'size', 'uuid', 'mountpoint', 'label'] - blkid = subprocess.Popen(["blkid"], + fdisk = subprocess.Popen(["fdisk -l"], stdout=subprocess.PIPE, + shell=True, universal_newlines=True) - grepedDrives = subprocess.Popen(["grep", "/dev/sd"], - stdin=blkid.stdout, + grepedDrives = subprocess.Popen(["grep", device], + stdin=fdisk.stdout, stdout=subprocess.PIPE, universal_newlines=True) while True: @@ -106,31 +104,22 @@ def OLDgetAllDrives(): drives.append(line.rstrip()) else: break - blkid.stdout.close() + fdisk.stdout.close() + del drives[0] for line in drives: - values = [] - #Informationen aufbereiten - arrayline = line.split(' ') - values.append(arrayline[0][:-1]) - if any("LABEL" in s for s in arrayline): - if any("UUID_SUB" in s for s in arrayline): - values.append("Was weiß ich...") - values.append(arrayline[4][6:-1]) - values.append("Weiß ich auch noch nicht...") - else: - values.append("Was weiß ich...") - values.append(arrayline[3][6:-1]) - values.append("Weiß ich auch noch nicht...") - elif any(not "LABEL" in s - for s in arrayline) and any("UUID_SUB" in s - for s in arrayline): - values.append("Was weiß ich...") - values.append(arrayline[3][6:-1]) - values.append("(keiner)") + line = line.replace("*", "") + newLine = ' '.join(line.split()) + newLine = newLine.split(" ") + if newLine[5] != 5: #Ungültige Partitionen herausfiltern + # Für jede Partition muss nun blkid ausgeführt werden, um weitere Informationen zu erhalten + values = [] + values.append(newLine[0]) + values.append("fs") + values.append(newLine[4]) + values.append("uuid") + values.append("mountpoint") + values.append("label") + partDict.append(dict(zip(keys, values))) else: - values.append("Was weiß ich...") - values.append(arrayline[2][6:-1]) - values.append("(keiner)") - #Dict für Jinja anfügen - driveDict.append(dict(zip(keys, values))) - return driveDict + pass + return partDict diff --git a/dirkules/models.py b/dirkules/models.py index 9c9dea7..05174f0 100644 --- a/dirkules/models.py +++ b/dirkules/models.py @@ -26,17 +26,17 @@ class Partitions(db.Model): drive_id = db.Column(db.Integer, db.ForeignKey('drives.id')) name = db.Column(db.String) fs = db.Column(db.String) + size =db.Column(db.String) uuid = db.Column(db.String) mountpoint = db.Column(db.String) label = db.Column(db.String) -class Times(db.Model): - __tablename__ = 'times' +class Time(db.Model): + __tablename__ = 'time' id = db.Column(db.Integer, primary_key=True) desc = db.Column(db.String) time = db.Column(db.Integer, default=0, onupdate=1) - def __init__(self, desc, time): + def __init__(self, desc): self.desc = desc - self.time = time diff --git a/dirkules/views.py b/dirkules/views.py index 9af4a7c..b5e996c 100644 --- a/dirkules/views.py +++ b/dirkules/views.py @@ -12,7 +12,9 @@ def index(): @app.route('/drives', methods=['GET']) def drives(): drives = drico.getAllDrives() - print(Drive.query.filter_by(device='/dev/sda').all()) + #print(Drive.query.filter_by(device='/dev/sda').all()) + test = drico.getPartitions("/dev/sdb") + print(test) return render_template('drives.html', drives=drives) diff --git a/setup.py b/setup.py index 94ea7b9..8f3ee4a 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,5 @@ setup( install_requires=[ 'flask', 'Flask-SQLAlchemy', - 'psutil', ], )