-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbox.py
66 lines (55 loc) · 2.23 KB
/
toolbox.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from datetime import timedelta
from typing import Union, Tuple
import semver
"""
summary
-------
This module provides useful methods which didn't fit in any other module
"""
test_bed_path = os.path.realpath(os.path.dirname(__file__))
tools_path = '{}/resources/tools'.format(test_bed_path)
test_contracts_rel_path = 'resources/tests/contracts'
server = False
test_mode = False
def get_range_for_installed_solcs(min_version: Union[semver.VersionInfo, str],
max_version: Union[semver.VersionInfo, str]) -> Tuple[
semver.VersionInfo, semver.VersionInfo]:
"""finds the minimal and maximal installed solidity compiler version between min_version and max_version
Parameters
----------
min_version : semver.VersionInfo or str
max_version : semver.VersionInfo or str
Returns
-------
Tuple[semver.VersionInfo, semver.VersionInfo]
Raises
-------
NotImplementedError
If there are no installed solidity compilers between min_version and max_version
"""
if type(min_version) == str:
min_version = semver.VersionInfo.parse(min_version)
if type(max_version) == str:
max_version = semver.VersionInfo.parse(max_version)
versions = [semver.VersionInfo.parse(v) for v in os.listdir('{}/resources/solc-versions'.format(test_bed_path))]
min_installed_version = min(versions)
max_installed_version = max(versions)
try:
new_min_version = min(filter(lambda v: min_version <= v, versions))
new_max_version = max(filter(lambda v: v <= max_version, versions))
except ValueError:
raise NotImplementedError(
'There is no installed version >={} and <={}. The minimal (maximal) installed version is {} ({})'.format(
min_version, max_version, min_installed_version, max_installed_version))
return new_min_version, new_max_version
def timedelta_to_string(td: timedelta):
minutes, seconds = divmod(td.total_seconds(), 60)
milliseconds = (td / timedelta(milliseconds=1)) % 1000
string = ''
if minutes > 0:
string += f'{int(minutes)} min. and '
if seconds > 0:
string += f'{int(seconds)} secs. and '
string += f'{int(milliseconds)} millisecs.'
return string