forked from stx-labs/stacks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
208 lines (156 loc) · 5.56 KB
/
cli.py
File metadata and controls
208 lines (156 loc) · 5.56 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Blockstack-client
~~~~~
copyright: (c) 2014-2015 by Halfmoon Labs, Inc.
copyright: (c) 2016 by Blockstack.org
This file is part of Blockstack-client.
Blockstack-client is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Blockstack-client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Blockstack-client. If not, see <http://www.gnu.org/licenses/>.
"""
"""
IMPROTANT: READ THIS FIRST
Do NOT add CLI commands to this file.
Instead, define the appropriate method in the `actions.py` file
in this module.
This module will load and register each appropriate method from `actions.py`
as a command-line option.
"""
import argparse
import sys
import json
import traceback
import os
import re
import pybitcoin
import subprocess
from socket import error as socket_error
from time import sleep
from getpass import getpass
import requests
requests.packages.urllib3.disable_warnings()
import logging
logging.disable(logging.CRITICAL)
# Hack around absolute paths
current_dir = os.path.abspath(os.path.dirname(__file__))
parent_dir = os.path.abspath(current_dir + "/../")
sys.path.insert(0, parent_dir)
from blockstack_client import config
from blockstack_client.client import session
from blockstack_client.config import WALLET_PATH, WALLET_PASSWORD_LENGTH, CONFIG_PATH
from blockstack_client.method_parser import parse_methods, build_method_subparsers
import blockstack_client.actions as builtin_methods
from pybitcoin import is_b58check_address
from binascii import hexlify
from wallet import *
from utils import exit_with_error, pretty_dump, print_result
log = config.get_logger()
def get_methods(prefix, module):
"""
Get the built-in CLI methods
"""
methods = []
for attr in dir(module):
if attr.startswith(prefix):
method = getattr(module, attr)
if callable(method) or hasattr(method, '__call__'):
methods.append( method )
return methods
def get_plugin_methods( module_name, prefix ):
"""
Load methods from a given module
Return the list on success
Return None on error
"""
try:
mod = __import__(module_name)
except ImportError:
log.error("Failed to import '%s'" % module_name)
return None
return get_methods( prefix, mod )
def get_cli_basic_methods():
"""
Get the basic built-in CLI methods
"""
return get_methods("cli_", builtin_methods )
def get_cli_advanced_methods():
"""
Get the advanced usage built-in CLI methods
"""
return get_methods("cli_advanced_", builtin_methods )
def run_cli(argv=None, config_path=CONFIG_PATH):
"""
Run a CLI command from arguments (defaults to sys.argv)
Return the result of the command on success.
The result will be a dict, and will have 'error' defined on error condition.
"""
if argv is None:
argv = sys.argv
# alternative config path?
if '-c' in argv or '--config' in argv:
i = 1
while i < len(argv):
if argv[i] == '-c' or argv[i] == '--config':
if i + 1 >= len(argv):
print >> sys.stderr, "%s: missing path" % argv[i]
sys.exit(1)
config_path = argv[i+1]
argv.pop(i)
argv.pop(i)
else:
i+=1
conf = config.get_config(path=config_path)
if conf is None:
return {'error': 'Failed to load config'}
advanced_mode = conf['advanced_mode']
parser = argparse.ArgumentParser(
description='Blockstack cli version {}'.format(config.VERSION))
all_methods = []
subparsers = parser.add_subparsers(dest='action')
# add basic methods
basic_methods = get_cli_basic_methods()
basic_method_info = parse_methods( basic_methods )
build_method_subparsers( subparsers, basic_method_info )
all_methods = basic_method_info
if advanced_mode:
# add advanced methods
advanced_methods = get_cli_advanced_methods()
advanced_method_info = parse_methods( advanced_methods )
build_method_subparsers( subparsers, advanced_method_info )
all_methods += advanced_method_info
# Print default help message, if no argument is given
if len(argv) == 1:
parser.print_help()
return {}
args, unknown_args = parser.parse_known_args(args=argv[1:])
result = {}
blockstack_server = conf['server']
blockstack_port = conf['port']
# initialize blockstack connection
session(conf=conf, server_host=blockstack_server,
server_port=blockstack_port, set_global=True)
# dispatch to the apporpriate method
for method_info in all_methods:
if args.action != method_info['command']:
continue
method = method_info['method']
result = method( args, config_path=config_path )
return result
# not found
return {'error': "No such command '%s'" % args.action}
if __name__ == '__main__':
result = run_cli()
if 'error' in result:
exit_with_error(result['error'])
else:
print_result(result)
sys.exit(0)