async def main() -> None: args = parser.parse_args() boot_info = BootInfo.from_namespace(args) if not boot_info.base_dir.exists(): if boot_info.is_ephemeral or get_xdg_data_home( ) in boot_info.base_dir.parents: boot_info.base_dir.mkdir(exist_ok=True) else: raise FileNotFoundError( "Not creating DDHT root directory as it is not present and is " f"not under the $XDG_DATA_HOME: {boot_info.base_dir}") if args.log_file is None: logdir = log_file = get_xdg_ddht_root() / "logs" logdir.mkdir(parents=True, exist_ok=True) log_file = logdir / "ddht.log" else: log_file = args.log_file setup_logging(log_file, args.log_level_file, args.log_level_stderr) logger.info(DDHT_HEADER) try: await args.func(boot_info) finally: if boot_info.is_ephemeral: shutil.rmtree(boot_info.base_dir)
def get_xdg_alexandria_root() -> Path: """ Returns the base directory under which alexandria will store data. """ try: return Path(os.environ["XDG_ALEXANDRIA_ROOT"]) except KeyError: return get_xdg_ddht_root() / "alexandria"
def _cli_args_to_boot_info_kwargs(args: argparse.Namespace) -> BootInfoKwargs: protocol_version = args.protocol_version is_ephemeral = args.ephemeral is True is_upnp_enabled = not args.disable_upnp if args.base_dir is not None: base_dir = args.base_dir.expanduser().resolve() elif is_ephemeral: base_dir = pathlib.Path(tempfile.TemporaryDirectory().name) else: base_dir = get_xdg_ddht_root() if args.port is not None: port = args.port elif is_ephemeral: port = get_open_port() else: port = DEFAULT_PORT listen_on: Optional[AnyIPAddress] if args.listen_address is None: listen_on = None else: listen_on = args.listen_address if args.bootnodes is None: if protocol_version is ProtocolVersion.v5: bootnodes = tuple( ENR.from_repr(enr_repr) for enr_repr in DEFAULT_V5_BOOTNODES) elif protocol_version is ProtocolVersion.v5_1: bootnodes = tuple( ENR.from_repr(enr_repr) for enr_repr in DEFAULT_V51_BOOTNODES) else: raise Exception( f"Unsupported protocol version: {protocol_version}") else: bootnodes = args.bootnodes private_key: Optional[keys.PrivateKey] if args.private_key is not None: private_key = keys.PrivateKey(decode_hex(args.private_key)) else: private_key = None return BootInfoKwargs( protocol_version=protocol_version, base_dir=base_dir, port=port, listen_on=listen_on, bootnodes=bootnodes, private_key=private_key, is_ephemeral=is_ephemeral, is_upnp_enabled=is_upnp_enabled, )