45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
''' pyf - a simple python framework '''
|
|
|
|
|
|
def log(message, log_file):
|
|
''' write @message to @log_file '''
|
|
with open(TMP, 'a') as f:
|
|
f.write('{} {}\n'.format(time.ctime(), message))
|
|
|
|
|
|
def prt(message):
|
|
''' print a message with style '''
|
|
print('::: {}'.format(message))
|
|
|
|
|
|
|
|
def download(url, file_name, progress=True):
|
|
''' download @url to @file_name '''
|
|
from urllib.request import urlretrieve
|
|
if progress:
|
|
from tqdm import tqdm
|
|
prt('downloading {}'.format(url))
|
|
class TqdmUpTo(tqdm):
|
|
def update_to(self, b=1, bsize=1, tsize=None):
|
|
if tsize is not None:
|
|
self.total = tsize
|
|
self.update(b * bsize - self.n)
|
|
|
|
with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=file_name) as t:
|
|
urlretrieve(url, filename=file_name, reporthook=t.update_to)
|
|
else:
|
|
urlretrieve(url, filename=file_name)
|
|
|
|
|
|
def sha256(file_name):
|
|
''' return @string sha256sum of @file_name '''
|
|
import hashlib
|
|
block = 65536
|
|
hasher = hashlib.sha256()
|
|
with open(file_name, 'rb') as f:
|
|
buf = f.read(block)
|
|
while len(buf) > 0:
|
|
hasher.update(buf)
|
|
buf = f.read(block)
|
|
return hasher.hexdigest()
|