aboutsummaryrefslogtreecommitdiff
blob: c5ead44ec9c47b87a88c8a9e282477528d9b25d4 (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
# -*- 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',
]

from hashlib import sha1
import json
import os

from .config import Config
config = Config(True)


def sha1_compute(filename):
    with open(filename) as fp:
        return sha1(fp.read()).hexdigest()

def sha1_check(db, p):
    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):
    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