3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 def checksum_string(s
):
22 return hashlib
.md5(s
.encode('utf-8')).hexdigest()
26 """ Calculate a md5 checksum of fd file handle
29 fd: A file descriptor return from open() call
32 A md5 checksum of the file
37 current
= hashlib
.md5()
39 data
= fd
.read(2 ** 16)
41 return current
.hexdigest()
47 class ArchiveChecksums(dict):
49 def from_file_desc(cls
, fd
):
50 checksum_pattern
= re
.compile(r
"(\S+)\s+(\S+)")
55 for line
in (line
.decode('utf-8').strip() for line
in fd
if line
):
58 if line
.startswith('#'):
61 # Skip lines that do not contain the pattern we are looking for
62 result
= checksum_pattern
.search(line
)
66 chksum
, filepath
= result
.groups()
67 checksums
[filepath
] = chksum
76 for file_name
, file_checksum
in self
.items():
77 string
+= "{} {}\n".format(file_checksum
, file_name
)