forked from vishakh/blockinstruments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·72 lines (60 loc) · 2.68 KB
/
Copy pathdeploy.py
File metadata and controls
executable file
·72 lines (60 loc) · 2.68 KB
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
#! /usr/bin/env python3
# Author: Kapil Thadani (kapil.thadani@gmail.com)
import argparse
import os
import subprocess
import tempfile
def replace_name(contract_name, line):
"""Replace name in contract line.
"""
i = line.find(contract_name)
return line[:i] + "Example" + line[i+len(contract_name):]
def read_contract(contract_path, verbose=False, seen_libs=None, replace=True):
"""Read a Solidity file and recursively expand imported contracts inline.
"""
if seen_libs is None:
seen_libs = set()
# Separate into path and file
contract_dir, contract_file = os.path.split(contract_path)
contract_name = contract_file[:-4]
output_lines = []
with open(contract_path) as f:
for line in f:
if line.startswith('import '):
lib_name = line[line.find('"')+1:line.rfind('"')]
lib_path = os.path.join(contract_dir, lib_name)
if lib_path not in seen_libs:
seen_libs.add(lib_path)
output_lines.extend(read_contract(lib_path,
verbose=False,
seen_libs=seen_libs,
replace=False))
output_lines.append('\n')
print("Importing", lib_name)
elif replace and (line.startswith('contract ' + contract_name) or \
line.startswith('function ' + contract_name)):
output_lines.append(replace_name(contract_name, line))
else:
output_lines.append(line)
if verbose:
print(''.join(output_lines))
return output_lines
def deploy_contract(contract_lines):
"""Compile and deploy a contract using truffle with imports expanded inline.
"""
# Initiate a temporary truffle directory
with tempfile.TemporaryDirectory() as truffle_dir:
os.chdir(truffle_dir)
subprocess.call('truffle init', shell=True)
# Overwrite Example.sol in the truffle directory
output_path = os.path.join(truffle_dir, 'contracts', 'Example.sol')
with open(output_path, 'w') as f:
f.writelines(contract_lines)
subprocess.call('truffle deploy', shell=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Compile and deploy contracts.')
parser.add_argument('contract_path', help='path to .sol file')
parser.add_argument('-v', '--verbose', action='store_true',
help='display the final contract')
args = parser.parse_args()
deploy_contract(read_contract(args.contract_path, verbose=args.verbose))