aboutsummaryrefslogtreecommitdiff
blob: 3ff8406f06d63803a33e4422474034ccd3be4a59 (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
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-

"""
    checksum.py
    ~~~~~~~~~~~
    
    This module implements functions for compute/generate SHA1 checksums
    for DESCRIPTION files.
    
    :copyright: (c) 2010 by Rafael Goncalves Martins
    :license: GPL-2, see LICENSE for more details.
"""

from __future__ import absolute_import

__all__ = [
    'sha1_compute',
    'sha1_check',
    'sha1_check_db',
]

import hashlib
import json
import os

from .config import Config
config = Config()


def sha1_compute(filename):
    '''Computes the SHA1 checksum of a file'''
    with open(filename, 'rb') as fp:
        return hashlib.sha1(fp.read()).hexdigest()

def sha1_check(db, p):
    '''Checks if the SHA1 checksum of the package is OK.'''
    description = db[p]
    manifest = {}
    with open(os.path.join(config.db, 'manifest.json')) as fp:
        manifest = json.load(fp)
    if p not in manifest:
        return False
    return manifest[p] == description.sha1sum()

def sha1_check_db(db):
    '''Checks if the SHA1 checksums of the package database are OK.'''
    for cat in db.pkg_list:
        for pkg in db.pkg_list[cat]:
            p = pkg['name']+'-'+pkg['version']
            if not sha1_check(db, p):
                return False
    return True