FileValidatorsPlugin.validate()   D
last analyzed

Complexity

Conditions 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 25
rs 4
1
from groundwork_validation.patterns import GwValidatorsPattern
2
3
4
class GwFileValidatorsPattern(GwValidatorsPattern):
5
    """
6
    Allows the creation and validation of hashes for given files.
7
8
    Usage::
9
10
        from groundwork_validation.patterns import GwFileValidatorsPattern
11
12
        class My_Plugin(GwFileValidatorsPattern):
13
            def __init__(self, app, **kwargs):
14
                self.name = "My_Plugin"
15
                super(My_Plugin, self).__init__(app, **kwargs)
16
17
            def activate(self):
18
                my_hash = self.validators.file.hash("/path/to/file.txt")
19
                self.validators.file.validate("/path/to/file.txt", my_hash)
20
21
    """
22
    def __init__(self, app, **kwargs):
23
        super(GwFileValidatorsPattern, self).__init__(app, **kwargs)
24
        self.app = app
25
        self.validators.file = FileValidatorsPlugin(self)
26
27
28
class FileValidatorsPlugin:
29
    def __init__(self, plugin):
30
        self.plugin = plugin
31
        self._validator = None
32
33
    def hash(self, file, validator=None, hash_file=None, blocksize=65536, return_hash_object=False):
34
        """
35
        Creates a hash of a given file.
36
37
        :param file: file path of the hashable file
38
        :param validator: validator, which shall be used. If none is given, a default validator will be used.
39
                          validator should be registered be the GwValidatorsPattern. Default is None
40
        :param hash_file: Path to a file, which is used to store the calculated hash value. Default is None
41
        :param blocksize: Size of each file block, which is used to update the hash. Default is 65536
42
        :param return_hash_object: Returns the hash object instead of the hash itself. Default is False
43
        :return: string, which represents the hash (hexdigest)
44
        """
45
        if validator is None:
46
            if self._validator is None:
47
                self._validator = self.plugin.validators.register("cmd_validator_%s" % self.plugin.name,
48
                                                                  "CMD validator for plugin %s" % self.plugin.name)
49
            validator = self._validator
50
51
        with open(file, 'rb') as afile:
52
            buf = afile.read(blocksize)
53
            hash_object = validator.get_hash_object()
54
            while len(buf) > 0:
55
                hash_object = validator.hash(buf, hash_object=hash_object, return_hash_object=True)
56
                buf = afile.read(blocksize)
57
58
        if hash_file is not None:
59
            with open(hash_file, "w") as hfile:
60
                hfile.write(hash_object.hexdigest())
61
62
        if return_hash_object:
63
            return hash_object
64
        else:
65
            return hash_object.hexdigest()
66
67
    def validate(self, file, hash_value=None, hash_file=None, validator=None, blocksize=65536):
68
        """
69
        Validates a file against a given hash.
70
        The given hash can be a string or a hash file, which must contain the hash on the first row.
71
72
        :param file: file path as string
73
        :param hash_value: hash, which is used for comparision
74
        :param hash_file:  file, which contains a hash value
75
        :param validator: groundwork validator, which shall be used. If None is given, a default one is used.
76
        :param blocksize: Size of each file block, which is used to update the hash.
77
        :return: True, if validation is correct. Otherwise False
78
        """
79
        if hash_value is None and hash_file is None:
80
            raise ValueError("hash_value or hash_file must be set.")
81
        if hash_value is not None and hash_file is not None:
82
            raise ValueError("Only hash_value OR hash_file must be set.")
83
84
        if hash_file is not None:
85
            with open(hash_file) as hfile:
86
                hash_value = hfile.readline()
87
88
        current_hash = self.hash(file, validator=validator, blocksize=blocksize)
89
        if current_hash == hash_value:
90
            return True
91
        return False
92