/
utils.py
32 lines (23 loc) · 997 Bytes
/
utils.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Module to store various helper functions used in mozdownload."""
from __future__ import absolute_import, unicode_literals
import hashlib
def urljoin(*fragments):
"""Concatenate multi part strings into urls."""
# Strip possible already existent final slashes of fragments except for the last one
parts = [fragment.rstrip('/') for fragment in fragments[:len(fragments) - 1]]
parts.append(fragments[-1])
return '/'.join(parts)
def create_md5(path):
"""Create the md5 hash of a file using the hashlib library."""
m = hashlib.md5()
# rb necessary to run correctly in windows.
with open(path, "rb") as f:
while True:
data = f.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()