summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/mozharness/mozilla/checksums.py
blob: e7071d506a41f5e95bd771911cc14f4d16d2cf0e (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import six


def parse_checksums_file(checksums):
    """
    Parses checksums files that the build system generates and uploads:
    https://hg.mozilla.org/mozilla-central/file/default/build/checksums.py
    """
    fileInfo = {}
    for line in checksums.splitlines():
        hash_, type_, size, file_ = line.split(None, 3)
        type_ = six.ensure_str(type_)
        file_ = six.ensure_str(file_)
        size = int(size)
        if size < 0:
            raise ValueError("Found negative value (%d) for size." % size)
        if file_ not in fileInfo:
            fileInfo[file_] = {"hashes": {}}
        # If the file already exists, make sure that the size matches the
        # previous entry.
        elif fileInfo[file_]["size"] != size:
            raise ValueError(
                "Found different sizes for same file %s (%s and %s)"
                % (file_, fileInfo[file_]["size"], size)
            )
        # Same goes for the hash.
        elif (
            type_ in fileInfo[file_]["hashes"]
            and fileInfo[file_]["hashes"][type_] != hash_
        ):
            raise ValueError(
                "Found different %s hashes for same file %s (%s and %s)"
                % (type_, file_, fileInfo[file_]["hashes"][type_], hash_)
            )
        fileInfo[file_]["size"] = size
        fileInfo[file_]["hashes"][type_] = hash_
    return fileInfo