Skip to content

Commit

Permalink
scanpypi: add support for licence files checksums
Browse files Browse the repository at this point in the history
Store the list of detected licence files in the main object and
automatically add their sha256 checksums when creating *.hash file.

Bonus: fix wrong indentation.

Signed-off-by: Yegor Yefremov <[email protected]>
Signed-off-by: Thomas Petazzoni <[email protected]>
  • Loading branch information
yegorich authored and tpetazzoni committed Jan 15, 2018
1 parent cfd1c93 commit 13d9466
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions utils/scanpypi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import tempfile
import imp
from functools import wraps

BUF_SIZE = 65536

try:
import spdx_lookup as liclookup
except ImportError:
Expand Down Expand Up @@ -132,6 +134,7 @@ class BuildrootPackage():
self.filename = None
self.url = None
self.version = None
self.license_files = []

def fetch_package_info(self):
"""
Expand Down Expand Up @@ -439,12 +442,12 @@ class BuildrootPackage():

filenames = ['LICENCE', 'LICENSE', 'LICENSE.RST', 'LICENSE.TXT',
'COPYING', 'COPYING.TXT']
license_files = list(find_file_upper_case(filenames, self.tmp_extract))
self.license_files = list(find_file_upper_case(filenames, self.tmp_extract))

lines.append(self.__get_license_names(license_files))
lines.append(self.__get_license_names(self.license_files))

license_files = [license.replace(self.tmp_extract, '')[1:]
for license in license_files]
for license in self.license_files]
if len(license_files) > 0:
if len(license_files) > 1:
print('More than one file found for license:',
Expand Down Expand Up @@ -508,7 +511,7 @@ class BuildrootPackage():
lines = []
if self.used_url['md5_digest']:
md5_comment = '# md5 from {url}, sha256 locally computed\n'.format(
url=self.metadata_url)
url=self.metadata_url)
lines.append(md5_comment)
hash_line = '{method}\t{digest} {filename}\n'.format(
method='md5',
Expand All @@ -522,6 +525,20 @@ class BuildrootPackage():
filename=self.filename)
lines.append(hash_line)

for license_file in self.license_files:
sha256 = hashlib.sha256()
with open(license_file, 'rb') as lic_f:
while True:
data = lic_f.read(BUF_SIZE)
if not data:
break
sha256.update(data)
hash_line = '{method}\t{digest} {filename}\n'.format(
method='sha256',
digest=sha256.hexdigest(),
filename=os.path.basename(license_file))
lines.append(hash_line)

with open(path_to_hash, 'w') as hash_file:
hash_file.writelines(lines)

Expand Down

0 comments on commit 13d9466

Please sign in to comment.