forked from ethereum/populus
/
utils.py
138 lines (103 loc) · 3.28 KB
/
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import shutil
import json
import socket
import time
import signal
import operator
import functools
import itertools
CONTRACTS_DIR = "./contracts/"
def get_contracts_dir(project_dir):
contracts_dir = os.path.join(project_dir, CONTRACTS_DIR)
return os.path.abspath(contracts_dir)
BUILD_DIR = "./build/"
def ensure_path_exists(dir_path):
"""
Make sure that a path exists
"""
if not os.path.exists(dir_path):
os.mkdir(dir_path)
return True
return False
def remove_file_if_exists(path):
if os.path.isfile(path):
os.remove(path)
return True
return False
def remove_dir_if_exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
return True
return False
def get_build_dir(project_dir):
build_dir_path = os.path.join(project_dir, BUILD_DIR)
ensure_path_exists(build_dir_path)
return build_dir_path
def load_contracts(project_dir):
compiled_contracts_path = os.path.join(project_dir, BUILD_DIR, 'contracts.json')
if not os.path.exists(compiled_contracts_path):
raise ValueError("No compiled contracts found")
with open(compiled_contracts_path) as contracts_file:
contracts = json.loads(contracts_file.read())
return contracts
def is_executable_available(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath = os.path.dirname(program)
if fpath:
if is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return False
def get_open_port():
sock = socket.socket()
sock.bind(('127.0.0.1', 0))
port = sock.getsockname()[1]
sock.close()
return str(port)
def wait_for_popen(proc, max_wait=5):
wait_till = time.time() + 5
while proc.poll() is None and time.time() < wait_till:
time.sleep(0.1)
def kill_proc(proc):
try:
if proc.poll() is None:
proc.send_signal(signal.SIGINT)
wait_for_popen(proc, 5)
if proc.poll() is None:
proc.terminate()
wait_for_popen(proc, 2)
if proc.poll() is None:
proc.kill()
wait_for_popen(proc, 1)
except KeyboardInterrupt:
proc.kill()
def get_contract_address_from_txn(blockchain_client, txn_hash, max_wait=0):
txn_receipt = blockchain_client.wait_for_transaction(txn_hash, max_wait)
return txn_receipt['contractAddress']
def merge_dependencies(*dependencies):
"""
Takes dictionaries of key => set(...) and merges them all into a single
dictionary where each key is the union of all of the sets for that key
across all dictionaries.
"""
return {
k: set(functools.reduce(
operator.or_,
(d.get(k, set()) for d in dependencies)
))
for k in itertools.chain.from_iterable((d.keys() for d in dependencies))
}
def get_dependencies(contract_name, dependencies):
return set(itertools.chain(
dependencies.get(contract_name, set()), *(
get_dependencies(dep, dependencies)
for dep in dependencies.get(contract_name, set())
)
))