Nils
4 years ago
9 changed files with 170 additions and 14 deletions
@ -0,0 +1,47 @@ |
|||
#! /usr/bin/env python3 |
|||
# -*- coding: utf-8 -*- |
|||
""" |
|||
Copyright 2021, Nils Hilbricht, Germany ( https://www.hilbricht.net ) |
|||
|
|||
The Non-Session-Manager by Jonathan Moore Liles <male@tuxfamily.org>: http://non.tuxfamily.org/nsm/ |
|||
New Session Manager, by LinuxAudio.org: https://github.com/linuxaudio/new-session-manager |
|||
With help from code fragments from https://github.com/attwad/python-osc ( DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE v2 ) |
|||
|
|||
This file is part of the Laborejo Software Suite ( https://www.laborejo.org ). |
|||
|
|||
This application is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
This program is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
""" |
|||
|
|||
#https://stackoverflow.com/questions/24937495/how-can-i-calculate-a-hash-for-a-filesystem-directory-using-python |
|||
|
|||
import hashlib |
|||
from _hashlib import HASH as Hash |
|||
from pathlib import Path |
|||
from typing import Union |
|||
|
|||
def md5_update_from_dir(directory, hash): |
|||
assert Path(directory).is_dir() |
|||
for path in sorted(Path(directory).iterdir(), key=lambda p: str(p).lower()): |
|||
hash.update(path.name.encode()) |
|||
if path.is_file(): |
|||
with open(path, "rb") as f: |
|||
for chunk in iter(lambda: f.read(4096), b""): |
|||
hash.update(chunk) |
|||
elif path.is_dir(): |
|||
hash = md5_update_from_dir(path, hash) |
|||
return hash |
|||
|
|||
|
|||
def md5_dir(directory): |
|||
return md5_update_from_dir(directory, hashlib.md5()).hexdigest() |
Loading…
Reference in new issue