Skip to content

Commit

Permalink
Adds linux deps msg + cleaner shutdown of price
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheem-opentensor committed Jan 25, 2025
1 parent 81f3f9b commit f2b135e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
5 changes: 5 additions & 0 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
prompt_for_identity,
validate_uri,
prompt_for_subnet_identity,
print_linux_dependency_message,
is_linux,
)
from typing_extensions import Annotated
from textwrap import dedent
Expand Down Expand Up @@ -3944,6 +3946,9 @@ def subnets_price(
if all_netuids:
html_output = True

if html_output and is_linux():
print_linux_dependency_message()

return self._run_command(
price.price(
self.initialize_chain(network),
Expand Down
30 changes: 25 additions & 5 deletions bittensor_cli/src/bittensor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import os
import sqlite3
import platform
import webbrowser
import sys
from pathlib import Path
Expand Down Expand Up @@ -1202,17 +1203,36 @@ def is_valid_contact(contact: str) -> bool:

def get_subnet_name(subnet_info) -> str:
"""Get the subnet name, prioritizing subnet_identity.subnet_name over subnet.subnet_name.
Args:
subnet: The subnet dynamic info
Returns:
str: The subnet name or empty string if no name is found
"""
return (
subnet_info.subnet_identity.subnet_name
if hasattr(subnet_info, 'subnet_identity')
and subnet_info.subnet_identity is not None
if hasattr(subnet_info, "subnet_identity")
and subnet_info.subnet_identity is not None
and subnet_info.subnet_identity.subnet_name is not None
else (subnet_info.subnet_name if subnet_info.subnet_name is not None else "")
)
)


def print_linux_dependency_message():
"""Prints the WebKit dependency message for Linux systems."""
console.print("[red]This command requires WebKit dependencies on Linux.[/red]")
console.print(
"\nPlease install the required packages using one of the following commands based on your distribution:"
)
console.print("\nArch Linux / Manjaro:")
console.print("[green]sudo pacman -S webkit2gtk[/green]")
console.print("\nDebian / Ubuntu:")
console.print("[green]sudo apt install libwebkit2gtk-4.0-dev[/green]")
console.print("\nFedora / CentOS / AlmaLinux:")
console.print("[green]sudo dnf install gtk3-devel webkit2gtk3-devel[/green]")


def is_linux():
"""Returns True if the operating system is Linux."""
return platform.system().lower() == "linux"
27 changes: 21 additions & 6 deletions bittensor_cli/src/commands/subnets/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,9 @@ async def _generate_html_output(
subnet_data, block_numbers, interval_hours, log_scale
)
title = "Subnets Price Chart"

console.print(
"[dark_sea_green3]Opening price chart in a window. Press Ctrl+C to close.[/dark_sea_green3]"
)

handler = PyWry()
handler.send_html(
html=html_content,
Expand All @@ -754,14 +752,22 @@ async def _generate_html_output(
height=800,
)
handler.start()
await asyncio.sleep(5)

# TODO: Improve this logic
try:
while True:
if _has_exited(handler):
break
await asyncio.sleep(1)
except KeyboardInterrupt:
try:
handler.close()
except (ProcessLookupError, Exception):
pass
pass
finally:
if not _has_exited(handler):
try:
handler.close()
except Exception:
pass
except Exception as e:
print_error(f"Error generating price chart: {e}")

Expand Down Expand Up @@ -850,3 +856,12 @@ def color_label(text):
)

console.print(stats_text)


def _has_exited(handler) -> bool:
"""Check if PyWry process has cleanly exited with returncode 0."""
return (
hasattr(handler, "runner")
and handler.runner is not None
and handler.runner.returncode == 0
)
14 changes: 7 additions & 7 deletions bittensor_cli/src/commands/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def new_hotkey(
use_password=use_password,
overwrite=False,
)
console.print(f"[dark_sea_green]Hotkey created[/dark_sea_green]")
console.print("[dark_sea_green]Hotkey created[/dark_sea_green]")
except KeyFileError:
print_error("KeyFileError: File is not writable")

Expand Down Expand Up @@ -178,7 +178,7 @@ async def new_coldkey(
use_password=use_password,
overwrite=False,
)
console.print(f"[dark_sea_green]Coldkey created[/dark_sea_green]")
console.print("[dark_sea_green]Coldkey created[/dark_sea_green]")
except KeyFileError:
print_error("KeyFileError: File is not writable")

Expand All @@ -193,11 +193,11 @@ async def wallet_create(
if uri:
try:
keypair = Keypair.create_from_uri(uri)
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False)
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False)
wallet.set_hotkey(keypair=keypair, encrypt=False, overwrite=False)
except Exception as e:
print_error(f"Failed to create keypair from URI: {str(e)}")
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False)
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False)
wallet.set_hotkey(keypair=keypair, encrypt=False, overwrite=False)
console.print(
f"[dark_sea_green]Wallet created from URI: {uri}[/dark_sea_green]"
)
Expand All @@ -208,7 +208,7 @@ async def wallet_create(
use_password=use_password,
overwrite=False,
)
console.print(f"[dark_sea_green]Coldkey created[/dark_sea_green]")
console.print("[dark_sea_green]Coldkey created[/dark_sea_green]")
except KeyFileError:
print_error("KeyFileError: File is not writable")

Expand All @@ -218,7 +218,7 @@ async def wallet_create(
use_password=False,
overwrite=False,
)
console.print(f"[dark_sea_green]Hotkey created[/dark_sea_green]")
console.print("[dark_sea_green]Hotkey created[/dark_sea_green]")
except KeyFileError:
print_error("KeyFileError: File is not writable")

Expand Down

0 comments on commit f2b135e

Please sign in to comment.