Skip to content

Commit 310e1df

Browse files
committed
add ubuntu version warning on console launch
add ubuntu warning test
1 parent 429488a commit 310e1df

File tree

5 files changed

+138
-40
lines changed

5 files changed

+138
-40
lines changed

mytonctrl/mytonctrl.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from mytonctrl.console_cmd import add_command, check_usage_one_arg, check_usage_args_min_max_len
4646
from mytonctrl.migrate import run_migrations
4747
from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt, \
48-
pop_user_from_args, pop_arg_from_args
48+
pop_user_from_args, pop_arg_from_args, get_clang_major_version, get_os_version
4949
from mytoninstaller.archive_blocks import download_blocks
5050
from mytoninstaller.utils import get_ton_storage_port
5151

@@ -403,37 +403,6 @@ def upgrade_btc_teleport(local, ton, reinstall=False, branch: str = 'master', us
403403
local.try_function(module.init, args=[reinstall, branch, user])
404404

405405

406-
def get_clang_major_version():
407-
try:
408-
process = subprocess.run(["clang", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
409-
text=True, timeout=3)
410-
if process.returncode != 0:
411-
return None
412-
413-
output = process.stdout
414-
415-
lines = output.strip().split('\n')
416-
if not lines:
417-
return None
418-
419-
first_line = lines[0]
420-
if "clang version" not in first_line:
421-
return None
422-
423-
version_part = first_line.split("clang version")[1].strip()
424-
major_version = version_part.split('.')[0]
425-
426-
major_version = ''.join(c for c in major_version if c.isdigit())
427-
428-
if not major_version:
429-
return None
430-
431-
return int(major_version)
432-
except Exception as e:
433-
print(f"Error checking clang version: {type(e)}: {e}")
434-
return None
435-
436-
437406
def rollback_to_mtc1(local, ton, args):
438407
color_print("{red}Warning: this is dangerous, please make sure you've backed up mytoncore's db.{endc}")
439408
a = input("Do you want to continue? [Y/n]\n")
@@ -558,16 +527,22 @@ def check_adnl(local, ton):
558527
print_warning(local, error)
559528
#end define
560529

561-
def warnings(local, ton):
530+
def check_ubuntu_version(local: MyPyClass):
531+
distro, ver = get_os_version()
532+
if distro == 'ubuntu':
533+
if ver not in ['22.04', '24.04']:
534+
warning = local.translate("ubuntu_version_warning").format(ver)
535+
print_warning(local, warning)
536+
537+
def warnings(local: MyPyClass, ton: MyTonCore):
562538
local.try_function(check_disk_usage, args=[local, ton])
563539
local.try_function(check_sync, args=[local, ton])
564540
local.try_function(check_adnl, args=[local, ton])
565541
local.try_function(check_validator_balance, args=[local, ton])
566542
local.try_function(check_vps, args=[local, ton])
567543
local.try_function(check_tg_channel, args=[local, ton])
568544
local.try_function(check_slashed, args=[local, ton])
569-
#end define
570-
545+
local.try_function(check_ubuntu_version, args=[local])
571546

572547
def CheckTonUpdate(local):
573548
git_path = "/usr/src/ton"

mytonctrl/resources/translate.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@
674674
"ru": "{{red}}Вы были оштрафованы на {0} TON за низкую эффективность в предыдущем раунде.{{endc}}",
675675
"zh_TW": "{{red}}您因上一輪效率低而被罰款 {0} TON。{{endc}}"
676676
},
677+
"ubuntu_version_warning": {
678+
"en": "{{red}}Ubuntu version must be 22.04 or 24.04. Found {0}. {{endc}}",
679+
"ru": "{{red}}Версия Ubuntu должна быть 22.04 или 24.04. Найдена {0}. {{endc}}",
680+
"zh_TW": "{{red}}Ubuntu 版本必须是 22.04 或 24.04。找到 {0}。{{endc}}"
681+
},
677682
"add_custom_overlay_cmd": {
678683
"en": "Add custom overlay",
679684
"ru": "Добавить пользовательский оверлей",

mytonctrl/utils.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,58 @@ def pop_arg_from_args(args: typing.List[str], arg_name: str) -> typing.Optional[
7676

7777
def pop_user_from_args(args: list) -> typing.Optional[str]:
7878
return pop_arg_from_args(args, '-u')
79+
80+
81+
def get_clang_major_version():
82+
try:
83+
process = subprocess.run(["clang", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
84+
text=True, timeout=3)
85+
if process.returncode != 0:
86+
return None
87+
88+
output = process.stdout
89+
90+
lines = output.strip().split('\n')
91+
if not lines:
92+
return None
93+
94+
first_line = lines[0]
95+
if "clang version" not in first_line:
96+
return None
97+
98+
version_part = first_line.split("clang version")[1].strip()
99+
major_version = version_part.split('.')[0]
100+
101+
major_version = ''.join(c for c in major_version if c.isdigit())
102+
103+
if not major_version:
104+
return None
105+
106+
return int(major_version)
107+
except Exception as e:
108+
print(f"Error checking clang version: {type(e)}: {e}")
109+
return None
110+
111+
112+
def get_os_version() -> typing.Tuple[typing.Optional[str], typing.Optional[str]]:
113+
os_release_path = "/etc/os-release"
114+
115+
if not os.path.exists(os_release_path):
116+
return None, None
117+
118+
data: typing.Dict[str, str] = {}
119+
with open(os_release_path) as f:
120+
for line in f:
121+
if "=" not in line:
122+
continue
123+
key, val = line.strip().split("=", 1)
124+
data[key] = val.strip('"')
125+
126+
distro = data.get("ID")
127+
version = (
128+
data.get("VERSION_ID")
129+
or data.get("BUILD_ID")
130+
or data.get("VERSION")
131+
)
132+
133+
return distro, version

tests/conftest.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,16 @@ def execute(self, command: str, no_color: bool = False) -> str:
8484
...
8585

8686

87-
class MyMyPyConsole(MyPyConsole):
87+
class TestMyPyConsole(MyPyConsole):
88+
89+
def run_pre_up(self, no_color: bool = False):
90+
output = io.StringIO()
91+
with redirect_stderr(output), redirect_stdout(output):
92+
self.startFunction()
93+
output = output.getvalue()
94+
if no_color:
95+
output = remove_colors(output)
96+
return output
8897

8998
def execute(self, command: str, no_color: bool = False) -> str:
9099
output = io.StringIO()
@@ -98,15 +107,13 @@ def execute(self, command: str, no_color: bool = False) -> str:
98107

99108

100109
@pytest.fixture()
101-
def cli(local, ton) -> ConsoleProtocol:
102-
console = MyMyPyConsole()
103-
console.start_function = None # todo: do not forget about start function
110+
def cli(local, ton) -> TestMyPyConsole:
111+
console = TestMyPyConsole()
104112
mp = pytest.MonkeyPatch()
105113
mp.setattr(MyTonCore, "using_pool", lambda self: True)
106114
mp.setattr(MyTonCore, "using_nominator_pool", lambda self: True)
107115
mp.setattr(MyTonCore, "using_single_nominator", lambda self: True)
108116
Init(local, ton, console, argv=[])
109117
mp.undo()
110118
console.debug = True
111-
# console.Run()
112119
return console

tests/integration/test_preup.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import builtins
2+
3+
from pytest_mock import MockerFixture
4+
5+
from mytonctrl import mytonctrl
6+
7+
8+
def test_warnings(cli, monkeypatch, mocker: MockerFixture):
9+
monkeypatch.setattr(mytonctrl, 'CheckMytonctrlUpdate', lambda *_: None)
10+
monkeypatch.setattr(mytonctrl, 'check_installer_user', lambda *_: None)
11+
monkeypatch.setattr(mytonctrl, 'check_installer_user', lambda *_: None)
12+
monkeypatch.setattr(mytonctrl, 'check_vport', lambda *_: None)
13+
14+
# test check_ubuntu_version
15+
16+
monkeypatch.setattr(mytonctrl.os.path, 'exists', lambda _: True)
17+
res = '''
18+
PRETTY_NAME="Ubuntu 22.04.4 LTS"
19+
NAME="Ubuntu"
20+
VERSION_ID="22.04"
21+
VERSION="22.04.4 LTS (Jammy Jellyfish)"
22+
VERSION_CODENAME=jammy
23+
ID=ubuntu
24+
'''
25+
mock = mocker.mock_open(read_data=res)
26+
monkeypatch.setattr('builtins.open', mock)
27+
output = cli.run_pre_up()
28+
assert 'Ubuntu' not in output
29+
30+
res = '''
31+
PRETTY_NAME="Ubuntu 20.04.4 LTS"
32+
NAME="Ubuntu"
33+
VERSION_ID="20.04"
34+
VERSION="20.04.4 LTS (Jammy Jellyfish)"
35+
VERSION_CODENAME=jammy
36+
ID=ubuntu
37+
'''
38+
mock = mocker.mock_open(read_data=res)
39+
monkeypatch.setattr('builtins.open', mock)
40+
output = cli.run_pre_up()
41+
assert 'Ubuntu version must be 22.04 or 24.04. Found 20.04.' in output
42+
43+
res = '''
44+
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
45+
NAME="Debian GNU/Linux"
46+
VERSION_ID="12"
47+
VERSION="12 (bookworm)"
48+
VERSION_CODENAME=bookworm
49+
ID=debian
50+
'''
51+
mock = mocker.mock_open(read_data=res)
52+
monkeypatch.setattr('builtins.open', mock)
53+
output = cli.run_pre_up()
54+
assert 'Ubuntu' not in output
55+
56+
# todo: other warnings

0 commit comments

Comments
 (0)