Ulrich
1 year ago
1 changed files with 140 additions and 0 deletions
@ -0,0 +1,140 @@ |
|||
""" |
|||
Dieses Programm durchlaeuft das angegebene Programmverzeichnis und ermittelt zu jeder Datei den md5-Hash. |
|||
Wenn neben diesem Programm eine Datei *md5Hash.txt liegt, werden die Werte gegen diese Datei verglichen. |
|||
weitere Feature: |
|||
* in Anwendung ueberfuehren, z.B. eine jar |
|||
* aufrubar ueber cli und Dialog |
|||
* config zu Standardeingaben --path, --work; |
|||
* --name mit Aufbauregel Release + Name |
|||
* Namensliste hinterlegen mit: unterverzeichnis, repo-name und repo-branch |
|||
* Methoden zum Annehmen einer Lieferung (unzip Subzips, pruefen, git-push nach korrekter Pruefung |
|||
* Methoden zum Erzeugen einer Lieferung |
|||
Definition *_md5protokoll.txt: datei \t md5checksum \n |
|||
""" |
|||
import argparse |
|||
import datetime |
|||
import hashlib |
|||
import os |
|||
|
|||
def openLog(args): |
|||
startTime = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
|||
path = os.path.join(getattr(args, "work"), getattr(args, "name") + "_" + startTime + ".txt") |
|||
logfile = open(path, 'w', encoding="utf-8") |
|||
logfile.write("* * * * * * * * * * PROTOKOLLL MD5-Checksum-Pruefung * * * * * * * * * * * * * * *\n") |
|||
logfile.write("Name: " + getattr(args, "name") + "\n") |
|||
logfile.write("Path: " + getattr(args, "path") + "\n") |
|||
logfile.write("Dir : " + getattr(args, "dir") + "\n") |
|||
return logfile |
|||
|
|||
def openResult(args, mode, suffix): |
|||
path = os.path.join(getattr(args, "work"), getattr(args, "name") + "_"+suffix+".txt") |
|||
if mode == "r" and not os.path.exists(path): |
|||
return None |
|||
resultfile = open(path, mode, encoding="utf-8") |
|||
return resultfile |
|||
|
|||
def traverseDir(logfile, resultfile, path, rootpath): |
|||
logfile.write("traverse " + path + "\n") |
|||
for f in sorted(os.listdir(path)): |
|||
if f[:1] == ".": |
|||
continue |
|||
if f[:2] == "__": |
|||
continue |
|||
if os.path.isfile(os.path.join(path, f)): |
|||
fname = os.path.join(path, f) |
|||
lname = fname.replace(rootpath, "") |
|||
logfile.write(". " + lname + "\n") |
|||
resultfile.write(lname + "\t" + getMD5Hash(fname) + "\n") |
|||
elif os.path.isdir(os.path.join(path, f)): |
|||
traverseDir(logfile, resultfile, os.path.join(path, f), rootpath) |
|||
|
|||
def getMD5Hash(path): |
|||
hash_md5 = hashlib.md5() |
|||
with open(path, "rb") as f: |
|||
for chunk in iter(lambda: f.read(4096), b""): |
|||
hash_md5.update(chunk) |
|||
return hash_md5.hexdigest() |
|||
|
|||
def compareLists(logfile, args): |
|||
protokollfile = openResult(args, "r", "md5protokoll") |
|||
if protokollfile is None: |
|||
logfile.write("Kein Vergleich, da Protokolldatei fehlt! \n") |
|||
return |
|||
resultfile = openResult(args, "r", "md5result") |
|||
protLines = protokollfile.readlines() |
|||
protokollfile.close() |
|||
resultLines = resultfile.readlines() |
|||
resultfile.close() |
|||
p = 0 |
|||
r = 0 |
|||
error = False |
|||
while (True): |
|||
# print("p " + str(p) + " r " + str(r)) |
|||
if len(protLines) > p: |
|||
protRow = protLines[p].replace("\r","").split("\t") |
|||
else: |
|||
protRow = None |
|||
if len(resultLines) > r: |
|||
resRow = resultLines[r].replace("\r","").split("\t") |
|||
else: |
|||
resRow = None |
|||
if protRow is None and resRow is None: |
|||
break |
|||
elif protRow is None and resRow is not None: |
|||
error = True |
|||
logfile.write("ERROR Result " + resRow[0] + ": ist ueberzaehlig\n") |
|||
r += 1 |
|||
elif resRow is not None and resRow is not None and protRow[0] > resRow[0]: |
|||
error = True |
|||
logfile.write("ERROR Result " + resRow[0] + ": ist ueberzaehlig\n") |
|||
r += 1 |
|||
elif resRow is None and protRow is not None: |
|||
error = True |
|||
logfile.write("ERROR Protokoll " + protRow[0] + ": ist ueberzaehlig\n") |
|||
p += 1 |
|||
elif protRow is not None and resRow is not None and protRow[0] < resRow[0]: |
|||
error = True |
|||
logfile.write("ERROR Protokoll " + protRow[0] + ": ist ueberzaehlig\n") |
|||
p += 1 |
|||
elif protRow is not None and resRow is not None and protRow[0] == resRow[0]: |
|||
if protRow[1] != resRow[1]: |
|||
error = True |
|||
logfile.write("ERROR "+protRow[0]+": md5Hash unterscheiden sich (" + protRow[1] + "!=" + resRow[1].strip() + ")\n") |
|||
r += 1 |
|||
p += 1 |
|||
if error: |
|||
logfile.write("\n+--------------------------------------------------------+\n") |
|||
logfile.write("| Fehler aufgetreten, die Dateien unterscheiden sich |\n") |
|||
logfile.write("+--------------------------------------------------------+\n") |
|||
else: |
|||
logfile.write("\nDateien unterscheiden sich nicht\n") |
|||
|
|||
|
|||
def readParameter(): |
|||
""" |
|||
--dir das zu durchsuchende Verzeichnis |
|||
--name Namenszusatz fuer das zu untersuchende Programmpaket |
|||
--work Arbeitserzeichnis mit: |
|||
<name>_md5result.txt erstellte Ergebnisdatei |
|||
<name>_md5protokoll.txt mitgelieferte Vergleichsdatei |
|||
<name>_JJJJMMTT_hhmmss.txt Protokolldatei |
|||
""" |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument('-p', '--path', required=True, action='store') |
|||
parser.add_argument('-d', '--dir', required=True, action='store') |
|||
parser.add_argument('-n', '--name', required=True, action='store') |
|||
parser.add_argument('-w', '--work', required=True, action='store') |
|||
args = parser.parse_args() |
|||
return args |
|||
|
|||
if __name__ == '__main__': |
|||
args = readParameter() |
|||
logfile = openLog(args) |
|||
logfile.write("\n") |
|||
resultfile = openResult(args, "w", "md5result") |
|||
path = os.path.join(getattr(args, "path")) |
|||
traverseDir(logfile, resultfile, path, path) |
|||
resultfile.close() |
|||
logfile.write("\n") |
|||
compareLists(logfile, args) |
|||
logfile.close() |
Loading…
Reference in new issue