forked from GlacierProtocol/GlacierProtocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin_cli.py
72 lines (55 loc) · 1.73 KB
/
bitcoin_cli.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
"""
Helpers for running bitcoin-cli subprocesses.
"""
from decimal import Decimal
import json as systemjson
import shlex
import subprocess
verbose_mode = False
cli_args = None
def _verbose(content):
"""
Print content iff verbose_mode is enabled.
"""
if verbose_mode:
print(content)
def _run_subprocess(exe, *args):
"""
Run a subprocess (bitcoind or bitcoin-cli).
Returns => (command, return code, output)
exe: executable file name (e.g. bitcoin-cli)
args: arguments to exe
"""
cmd_list = [exe] + cli_args + list(args)
_verbose("bitcoin cli call:\n {0}\n".format(" ".join(shlex.quote(x) for x in cmd_list)))
with subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as pipe:
output, _ = pipe.communicate()
output = output.decode('utf-8')
retcode = pipe.returncode
_verbose("bitcoin cli call return code: {0} output:\n {1}\n".format(retcode, output))
return (cmd_list, retcode, output)
def call(*args):
"""
Run `bitcoin-cli`, return OS return code.
"""
_, retcode, _ = _run_subprocess("bitcoin-cli", *args)
return retcode
def checkoutput(*args):
"""
Run `bitcoin-cli`, fail if OS return code nonzero, return output.
"""
cmd_list, retcode, output = _run_subprocess("bitcoin-cli", *args)
if retcode != 0:
raise subprocess.CalledProcessError(retcode, cmd_list, output=output)
return output
def json(*args):
"""
Run `bitcoin-cli`, parse output as JSON.
"""
return systemjson.loads(checkoutput(*args), parse_float=Decimal)
def bitcoind_call(*args):
"""
Run `bitcoind`, return OS return code.
"""
_, retcode, _ = _run_subprocess("bitcoind", *args)
return retcode