lbry-sdk/lbry/extras/cli.py

343 lines
12 KiB
Python
Raw Normal View History

import os
2018-07-26 05:27:53 +02:00
import sys
import shutil
2019-03-11 18:35:58 +01:00
import signal
import pathlib
import json
import asyncio
import argparse
2019-01-23 22:41:14 +01:00
import logging
2019-01-22 23:44:17 +01:00
import logging.handlers
2018-07-26 05:27:53 +02:00
2019-01-23 22:41:14 +01:00
import aiohttp
from aiohttp.web import GracefulExit
from docopt import docopt
2019-01-23 22:41:14 +01:00
from lbry import __version__ as lbrynet_version
2019-06-21 02:55:47 +02:00
from lbry.extras.daemon.loggly_handler import get_loggly_handler
from lbry.extras.daemon.daemon import Daemon
2020-01-03 08:50:28 +01:00
from lbry.conf import Config, CLIConfig
2018-07-26 05:27:53 +02:00
log = logging.getLogger('lbry')
2019-01-21 21:55:50 +01:00
def display(data):
2019-01-22 05:28:26 +01:00
print(json.dumps(data, indent=2))
2018-07-26 05:27:53 +02:00
2019-01-22 15:43:03 +01:00
2019-04-23 20:40:10 +02:00
async def execute_command(conf, method, params, callback=display):
2019-01-23 22:41:14 +01:00
async with aiohttp.ClientSession() as session:
try:
message = {'method': method, 'params': params}
async with session.get(conf.api_connection_url, json=message) as resp:
try:
data = await resp.json()
if 'result' in data:
2019-04-23 20:40:10 +02:00
return callback(data['result'])
2019-01-23 22:41:14 +01:00
elif 'error' in data:
return callback(data['error'])
2019-01-23 22:41:14 +01:00
except Exception as e:
log.exception('Could not process response from server:', exc_info=e)
except aiohttp.ClientConnectionError:
print("Could not connect to daemon. Are you sure it's running?")
2018-07-26 05:27:53 +02:00
def normalize_value(x, key=None):
2018-07-26 05:27:53 +02:00
if not isinstance(x, str):
return x
2019-09-20 19:29:41 +02:00
if key in ('uri', 'channel_name', 'name', 'file_name', 'claim_name', 'download_directory'):
2018-07-26 05:27:53 +02:00
return x
if x.lower() == 'true':
2018-07-26 05:27:53 +02:00
return True
if x.lower() == 'false':
2018-07-26 05:27:53 +02:00
return False
if x.isdigit():
2018-07-26 05:27:53 +02:00
return int(x)
return x
2018-07-26 05:27:53 +02:00
def remove_brackets(key):
if key.startswith("<") and key.endswith(">"):
return str(key[1:-1])
return key
def set_kwargs(parsed_args):
kwargs = {}
for key, arg in parsed_args.items():
if arg is None:
continue
2019-09-24 17:53:23 +02:00
k = None
if key.startswith("--") and remove_brackets(key[2:]) not in kwargs:
2018-07-26 05:27:53 +02:00
k = remove_brackets(key[2:])
elif remove_brackets(key) not in kwargs:
k = remove_brackets(key)
kwargs[k] = normalize_value(arg, k)
2018-07-26 05:27:53 +02:00
return kwargs
2019-01-25 00:22:53 +01:00
def split_subparser_argument(parent, original, name, condition):
new_sub_parser = argparse._SubParsersAction(
original.option_strings,
original._prog_prefix,
original._parser_class,
metavar=original.metavar
)
new_sub_parser._name_parser_map = original._name_parser_map
new_sub_parser._choices_actions = [
a for a in original._choices_actions if condition(original._name_parser_map[a.dest])
]
group = argparse._ArgumentGroup(parent, name)
group._group_actions = [new_sub_parser]
return group
2019-01-23 19:00:58 +01:00
class ArgumentParser(argparse.ArgumentParser):
2019-01-25 00:22:53 +01:00
def __init__(self, *args, group_name=None, **kwargs):
super().__init__(*args, formatter_class=HelpFormatter, add_help=False, **kwargs)
2019-01-23 19:00:58 +01:00
self.add_argument(
'--help', dest='help', action='store_true', default=False,
2019-01-25 00:22:53 +01:00
help='Show this help message and exit.'
)
self._optionals.title = 'Options'
if group_name is None:
self.epilog = (
f"Run 'lbrynet COMMAND --help' for more information on a command or group."
)
else:
self.epilog = (
f"Run 'lbrynet {group_name} COMMAND --help' for more information on a command."
)
self.set_defaults(group=group_name, group_parser=self)
def format_help(self):
formatter = self._get_formatter()
formatter.add_usage(
self.usage, self._actions, self._mutually_exclusive_groups
)
formatter.add_text(self.description)
# positionals, optionals and user-defined groups
for action_group in self._granular_action_groups:
formatter.start_section(action_group.title)
formatter.add_text(action_group.description)
formatter.add_arguments(action_group._group_actions)
formatter.end_section()
formatter.add_text(self.epilog)
return formatter.format_help()
@property
def _granular_action_groups(self):
if self.prog != 'lbrynet':
yield from self._action_groups
return
yield self._optionals
action: argparse._SubParsersAction = self._positionals._group_actions[0]
yield split_subparser_argument(
self, action, "Grouped Commands", lambda parser: 'group' in parser._defaults
)
yield split_subparser_argument(
self, action, "Commands", lambda parser: 'group' not in parser._defaults
)
def error(self, message):
self.print_help(argparse._sys.stderr)
self.exit(2, f"\n{message}\n")
2019-01-25 00:22:53 +01:00
class HelpFormatter(argparse.HelpFormatter):
def add_usage(self, usage, actions, groups, prefix='Usage: '):
super().add_usage(
usage, [a for a in actions if a.option_strings != ['--help']], groups, prefix
2019-01-23 19:00:58 +01:00
)
def add_command_parser(parent, command):
subcommand = parent.add_parser(
command['name'],
help=command['doc'].strip().splitlines()[0]
)
subcommand.set_defaults(
api_method_name=command['api_method_name'],
command=command['name'],
doc=command['doc'],
replaced_by=command.get('replaced_by', None)
)
2019-01-21 21:55:50 +01:00
def get_argument_parser():
2020-01-03 07:48:36 +01:00
root = ArgumentParser(
'lbrynet', description='An interface to the LBRY Network.', allow_abbrev=False,
)
2020-01-03 07:48:36 +01:00
root.add_argument(
2019-01-25 00:22:53 +01:00
'-v', '--version', dest='cli_version', action="store_true",
2019-01-21 21:55:50 +01:00
help='Show lbrynet CLI version and exit.'
)
2020-01-03 07:48:36 +01:00
root.set_defaults(group=None, command=None)
CLIConfig.contribute_to_argparse(root)
sub = root.add_subparsers(metavar='COMMAND')
2019-01-25 01:16:39 +01:00
start = sub.add_parser(
'start',
usage='lbrynet start [--config FILE] [--data-dir DIR] [--wallet-dir DIR] [--download-dir DIR] ...',
2019-01-26 02:28:25 +01:00
help='Start LBRY Network interface.'
2019-01-25 01:16:39 +01:00
)
2019-01-21 21:55:50 +01:00
start.add_argument(
'--quiet', dest='quiet', action="store_true",
help='Disable all console output.'
)
start.add_argument(
'--no-logging', dest='no_logging', action="store_true",
help='Disable all logging of any kind.'
)
2019-01-21 21:55:50 +01:00
start.add_argument(
'--verbose', nargs="*",
2019-09-23 22:15:23 +02:00
help=('Enable debug output for lbry logger and event loop. Optionally specify loggers for which debug output '
2019-01-21 21:55:50 +01:00
'should selectively be applied.')
)
start.add_argument(
'--initial-headers', dest='initial_headers',
help='Specify path to initial blockchain headers, faster than downloading them on first run.'
)
2019-01-26 04:27:10 +01:00
Config.contribute_to_argparse(start)
start.set_defaults(command='start', start_parser=start, doc=start.format_help())
2019-01-23 19:00:58 +01:00
2019-01-21 21:55:50 +01:00
api = Daemon.get_api_definitions()
2019-01-23 19:00:58 +01:00
groups = {}
for group_name in sorted(api['groups']):
2019-01-25 00:22:53 +01:00
group_parser = sub.add_parser(group_name, group_name=group_name, help=api['groups'][group_name])
groups[group_name] = group_parser.add_subparsers(metavar='COMMAND')
nicer_order = ['stop', 'get', 'publish', 'resolve']
2019-01-23 19:00:58 +01:00
for command_name in sorted(api['commands']):
2019-01-25 00:22:53 +01:00
if command_name not in nicer_order:
nicer_order.append(command_name)
for command_name in nicer_order:
2019-01-23 19:00:58 +01:00
command = api['commands'][command_name]
if command['group'] is None:
add_command_parser(sub, command)
else:
add_command_parser(groups[command['group']], command)
2020-01-03 07:48:36 +01:00
return root
2019-01-21 21:55:50 +01:00
def ensure_directory_exists(path: str):
if not os.path.isdir(path):
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
LOG_MODULES = 'lbry', 'aioupnp'
2019-10-18 16:23:35 +02:00
def setup_logging(logger: logging.Logger, args: argparse.Namespace, conf: Config):
default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
file_handler = logging.handlers.RotatingFileHandler(conf.log_file_path, maxBytes=2097152, backupCount=5)
file_handler.setFormatter(default_formatter)
2019-10-18 16:23:35 +02:00
for module_name in LOG_MODULES:
logger.getChild(module_name).addHandler(file_handler)
if not args.quiet:
handler = logging.StreamHandler()
handler.setFormatter(default_formatter)
2019-10-18 16:23:35 +02:00
for module_name in LOG_MODULES:
logger.getChild(module_name).addHandler(handler)
2019-10-18 16:23:35 +02:00
logger.getChild('lbry').setLevel(logging.INFO)
logger.getChild('aioupnp').setLevel(logging.WARNING)
logger.getChild('aiohttp').setLevel(logging.CRITICAL)
2019-10-02 09:58:11 +02:00
if args.verbose is not None:
if len(args.verbose) > 0:
for module in args.verbose:
2019-10-18 16:23:35 +02:00
logger.getChild(module).setLevel(logging.DEBUG)
2019-10-02 09:58:11 +02:00
else:
2019-10-18 16:23:35 +02:00
logger.getChild('lbry').setLevel(logging.DEBUG)
if conf.share_usage_data:
loggly_handler = get_loggly_handler()
loggly_handler.setLevel(logging.ERROR)
2019-10-18 16:23:35 +02:00
logger.getChild('lbry').addHandler(loggly_handler)
2019-10-05 20:44:14 +02:00
def run_daemon(args: argparse.Namespace, conf: Config):
loop = asyncio.get_event_loop()
2019-10-18 16:23:35 +02:00
if args.verbose is not None:
loop.set_debug(True)
if not args.no_logging:
setup_logging(logging.getLogger(), args, conf)
daemon = Daemon(conf)
def __exit():
raise GracefulExit()
try:
loop.add_signal_handler(signal.SIGINT, __exit)
loop.add_signal_handler(signal.SIGTERM, __exit)
except NotImplementedError:
pass # Not implemented on Windows
try:
loop.run_until_complete(daemon.start())
loop.run_forever()
except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError):
pass
finally:
loop.run_until_complete(daemon.stop())
logging.shutdown()
if hasattr(loop, 'shutdown_asyncgens'):
loop.run_until_complete(loop.shutdown_asyncgens())
def main(argv=None):
argv = argv or sys.argv[1:]
2019-01-21 21:55:50 +01:00
parser = get_argument_parser()
2019-01-22 21:00:13 +01:00
args, command_args = parser.parse_known_args(argv)
2019-01-23 22:41:14 +01:00
2019-01-22 05:28:26 +01:00
conf = Config.create_from_arguments(args)
for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir):
ensure_directory_exists(directory)
2019-01-21 21:55:50 +01:00
if args.cli_version:
print(f"lbrynet {lbrynet_version}")
2019-01-21 21:55:50 +01:00
elif args.command == 'start':
2019-01-23 20:56:54 +01:00
if args.help:
args.start_parser.print_help()
2019-01-23 22:41:14 +01:00
else:
if args.initial_headers:
ledger_path = os.path.join(conf.wallet_dir, 'lbc_mainnet')
ensure_directory_exists(ledger_path)
current_size = 0
headers_path = os.path.join(ledger_path, 'headers')
if os.path.exists(headers_path):
current_size = os.stat(headers_path).st_size
if os.stat(args.initial_headers).st_size > current_size:
log.info('Copying header from %s to %s', args.initial_headers, headers_path)
shutil.copy(args.initial_headers, headers_path)
run_daemon(args, conf)
2019-01-21 21:55:50 +01:00
elif args.command is not None:
2019-01-23 19:00:58 +01:00
doc = args.doc
api_method_name = args.api_method_name
if args.replaced_by:
print(f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}.")
doc = args.replaced_by['doc']
api_method_name = args.replaced_by['api_method_name']
if args.help:
print(doc)
2019-01-22 05:28:26 +01:00
else:
2019-01-23 19:00:58 +01:00
parsed = docopt(doc, command_args)
params = set_kwargs(parsed)
asyncio.get_event_loop().run_until_complete(execute_command(conf, api_method_name, params))
2019-01-23 19:00:58 +01:00
elif args.group is not None:
args.group_parser.print_help()
2019-01-21 21:55:50 +01:00
else:
parser.print_help()
2019-01-23 22:41:14 +01:00
2018-07-26 05:27:53 +02:00
return 0
if __name__ == "__main__":
2018-07-27 13:30:42 +02:00
sys.exit(main())