dtoolcore 3.18.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dtoolcore/compare.py ADDED
@@ -0,0 +1,79 @@
1
+ """Module with helper functions for comparing datasets."""
2
+
3
+
4
+ def diff_identifiers(a, b):
5
+ """Return list of tuples where identifiers in datasets differ.
6
+
7
+ Tuple structure:
8
+ (identifier, present in a, present in b)
9
+
10
+ :param a: first :class:`dtoolcore.DataSet`
11
+ :param b: second :class:`dtoolcore.DataSet`
12
+ :returns: list of tuples where identifiers in datasets differ
13
+ """
14
+
15
+ a_ids = set(a.identifiers)
16
+ b_ids = set(b.identifiers)
17
+
18
+ difference = []
19
+
20
+ for i in a_ids.difference(b_ids):
21
+ difference.append((i, True, False))
22
+ for i in b_ids.difference(a_ids):
23
+ difference.append((i, False, True))
24
+
25
+ return difference
26
+
27
+
28
+ def diff_sizes(a, b, progressbar=None):
29
+ """Return list of tuples where sizes differ.
30
+
31
+ Tuple structure:
32
+ (identifier, size in a, size in b)
33
+
34
+ Assumes list of identifiers in a and b are identical.
35
+
36
+ :param a: first :class:`dtoolcore.DataSet`
37
+ :param b: second :class:`dtoolcore.DataSet`
38
+ :returns: list of tuples for all items with different sizes
39
+ """
40
+ difference = []
41
+
42
+ for i in a.identifiers:
43
+ a_size = a.item_properties(i)["size_in_bytes"]
44
+ b_size = b.item_properties(i)["size_in_bytes"]
45
+ if a_size != b_size:
46
+ difference.append((i, a_size, b_size))
47
+ if progressbar:
48
+ progressbar.update(1)
49
+
50
+ return difference
51
+
52
+
53
+ def diff_content(a, reference, progressbar=None):
54
+ """Return list of tuples where content differ.
55
+
56
+ Tuple structure:
57
+ (identifier, hash in a, hash in reference)
58
+
59
+ Assumes list of identifiers in a and b are identical.
60
+
61
+ Storage broker of reference used to generate hash for files in a.
62
+
63
+ :param a: first :class:`dtoolcore.DataSet`
64
+ :param b: second :class:`dtoolcore.DataSet`
65
+ :returns: list of tuples for all items with different content
66
+ """
67
+ difference = []
68
+
69
+ for i in a.identifiers:
70
+ fpath = a.item_content_abspath(i)
71
+ calc_hash = reference._storage_broker.hasher(fpath)
72
+ ref_hash = reference.item_properties(i)["hash"]
73
+ if calc_hash != ref_hash:
74
+ info = (i, calc_hash, ref_hash)
75
+ difference.append(info)
76
+ if progressbar:
77
+ progressbar.update(1)
78
+
79
+ return difference
@@ -0,0 +1,89 @@
1
+ """Module for generating file hashes."""
2
+
3
+ import hashlib
4
+
5
+
6
+ class FileHasher(object):
7
+ """Class for associating hash functions with names."""
8
+
9
+ def __init__(self, hash_func):
10
+ self.func = hash_func
11
+ self.name = hash_func.__name__
12
+
13
+ def __call__(self, filename):
14
+ return self.func(filename)
15
+
16
+
17
+ def _hash_the_file(hasher, filename):
18
+ """Helper function for creating hash functions.
19
+
20
+ See implementation of :func:`dtoolcore.filehasher.shasum`
21
+ for more usage details.
22
+ """
23
+ BUF_SIZE = 65536
24
+ with open(filename, 'rb') as f:
25
+ buf = f.read(BUF_SIZE)
26
+ while len(buf) > 0:
27
+ hasher.update(buf)
28
+ buf = f.read(BUF_SIZE)
29
+ return hasher
30
+
31
+
32
+ def hashsum_hexdigest(hasher, filename):
33
+ """Helper function for creating hash functions.
34
+
35
+ See implementation of :func:`dtoolcore.filehasher.shasum`
36
+ for more usage details.
37
+ """
38
+ hasher = _hash_the_file(hasher, filename)
39
+ return hasher.hexdigest()
40
+
41
+
42
+ def hashsum_digest(hasher, filename):
43
+ """Helper function for creating hash functions.
44
+
45
+ See implementation of :func:`dtoolcore.filehasher.shasum`
46
+ for more usage details.
47
+ """
48
+ hasher = _hash_the_file(hasher, filename)
49
+ return hasher.digest()
50
+
51
+
52
+ def sha1sum_hexdigest(filename):
53
+ """Return hex digest of SHA-1 hash of file.
54
+
55
+ :param filename: path to file
56
+ :returns: shasum of file
57
+ """
58
+ hasher = hashlib.sha1()
59
+ return hashsum_hexdigest(hasher, filename)
60
+
61
+
62
+ def sha256sum_hexdigest(filename):
63
+ """Return hex digest of SHA-256 hash of file.
64
+
65
+ :param filename: path to file
66
+ :returns: shasum of file
67
+ """
68
+ hasher = hashlib.sha256()
69
+ return hashsum_hexdigest(hasher, filename)
70
+
71
+
72
+ def md5sum_hexdigest(filename):
73
+ """Return hex digest of MD5sum of file.
74
+
75
+ :param filename: path to file
76
+ :returns: shasum of file
77
+ """
78
+ hasher = hashlib.md5()
79
+ return hashsum_hexdigest(hasher, filename)
80
+
81
+
82
+ def md5sum_digest(filename):
83
+ """Return digest of MD5sum of file.
84
+
85
+ :param filename: path to file
86
+ :returns: shasum of file
87
+ """
88
+ hasher = hashlib.md5()
89
+ return hashsum_digest(hasher, filename)