-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·45 lines (37 loc) · 1.13 KB
/
Copy pathutils.py
File metadata and controls
executable file
·45 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from pathlib import Path
from filehash import FileHash
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def file_size(path):
'''
Function to get size of file given as path
path : string or Path object of the path of the file
'''
logger.info(f"Get file size of {path}")
size = None
if isinstance(path, Path):
size = path.stat().st_size
elif(type(path) == str):
size = Path(path).stat().st_size
else:
logger.error(f"Unknown type of path = {path}")
logger.debug("Size of file {path} = {size}")
return size
def file_hash(path):
'''
Function to return sha1 hash of a file
path: string or Path object of the path of the file
'''
logger.info(f"Get hash of {path}")
hash = ''
sha1hasher = FileHash('sha1')
hash = sha1hasher.hash_file(path)
return hash
def verify_hash(hash, path):
'''
Function to verify hash of the file
path: string or Path object of the path of the file
'''
logger.info(f"Verify hash of file {path}")
return hash == file_hash(path)