aboutsummaryrefslogtreecommitdiff
blob: 99541299ccfe8ec10602c562994c170df90e24ec (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
# -*- coding: utf-8 -*-

"""
    g_octave.checksum
    ~~~~~~~~~~~~~~~~~
    
    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.get(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 pkg in db:
        if not sha1_check(db, pkg.P):
            return False
    return True