Commit 772b18a9 authored by Cyril Roelandt's avatar Cyril Roelandt

upt.Archive: add support for sha256 in base64 notation.

parent a657b371
......@@ -6,6 +6,7 @@ Based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- upt.Archive: add a "filename" attribute
- upt.Archive: add support for sha256 in base64 notation
## [0.6] - 2019-02-08
### Added
......
# Copyright 2018 Cyril Roelandt
#
# Licensed under the 3-clause BSD license. See the LICENSE file.
import base64
import binascii
import hashlib
......@@ -40,11 +42,17 @@ def _compute_sha256_checksum(filepath):
return _compute_checksum(hashlib.sha256, filepath)
def _compute_sha256_base64_checksum(filepath):
sha256 = _compute_sha256_checksum(filepath)
return base64.b64encode(binascii.a2b_hex(sha256)).decode('ascii')
def compute_checksum(filepath, hash_name):
hash_functions = {
'md5': _compute_md5_checksum,
'rmd160': _compute_rmd160_checksum,
'sha256': _compute_sha256_checksum,
'sha256_base64': _compute_sha256_base64_checksum,
}
try:
return hash_functions[hash_name](filepath)
......
......@@ -11,6 +11,7 @@ from upt import checksum
class TestChecksum(unittest.TestCase):
MD5 = '3858f62230ac3c915f300c664312c63f'
SHA256 = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2'
SHA256_BASE64 = 'w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI='
def setUp(self):
self.path = '/fake/path'
......@@ -22,6 +23,10 @@ class TestChecksum(unittest.TestCase):
self.assertEqual(checksum._compute_sha256_checksum(self.path),
self.SHA256)
def test_sha256_base64_checksum(self, open_fn):
self.assertEqual(checksum._compute_sha256_base64_checksum(self.path),
self.SHA256_BASE64)
def test_compute_checksum(self, open_fn):
self.assertEqual(checksum.compute_checksum(self.path, 'md5'), self.MD5)
......
......@@ -279,8 +279,9 @@ class TestArchive(unittest.TestCase):
@mock.patch('upt.checksum.compute_checksum', return_value='hash-output')
def test_checksums_provided(self, compute_checksum_fn):
archive = upt.Archive('url', md5='md5', rmd160='rmd160',
sha256='sha256')
sha256='sha256', sha256_base64='sha256_base64')
self.assertEqual(archive.md5, 'md5')
self.assertEqual(archive.rmd160, 'rmd160')
self.assertEqual(archive.sha256, 'sha256')
self.assertEqual(archive.sha256_base64, 'sha256_base64')
compute_checksum_fn.assert_not_called()
......@@ -40,7 +40,7 @@ class Archive(object):
This can be a source tarball, a Python wheel, a Ruby gem, a binary, etc.
'''
def __init__(self, url, archive_type=ArchiveType.SOURCE_TARGZ, size=0,
md5=None, sha256=None, rmd160=None):
md5=None, sha256=None, rmd160=None, sha256_base64=None):
self.url = url
self.archive_type = archive_type
self._size = size
......@@ -53,6 +53,8 @@ class Archive(object):
self.sha256 = sha256
if rmd160 is not None:
self.rmd160 = rmd160
if sha256_base64 is not None:
self.sha256_base64 = sha256_base64
@property
def filepath(self):
......@@ -105,6 +107,14 @@ class Archive(object):
def sha256(self, value):
self._hashes['sha256'] = value
@property
def sha256_base64(self):
return self._checksum('sha256_base64')
@sha256_base64.setter
def sha256_base64(self, value):
self._hashes['sha256_base64'] = value
class PackageRequirement(object):
"""Package Requirement.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment