Skip to content

Commit 3b4c272

Browse files
committed
add ruff linting to mytonctrl/
1 parent b9b91ab commit 3b4c272

File tree

6 files changed

+36
-157
lines changed

6 files changed

+36
-157
lines changed

.github/workflows/tests.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Python tests
1+
name: Lint + Tests
22

33
on:
44
push:
@@ -10,25 +10,33 @@ on:
1010

1111
jobs:
1212
test:
13-
1413
runs-on: ubuntu-latest
1514

15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
19+
1620
steps:
1721
- uses: actions/checkout@v5
1822
with:
1923
submodules: true
2024

21-
- name: Set up Python
25+
- name: Set up Python ${{ matrix.python-version }}
2226
uses: actions/setup-python@v5
2327
with:
24-
python-version: '3.13'
28+
python-version: ${{ matrix.python-version }}
2529

2630
- name: Install dependencies
2731
run: |
2832
python -m pip install --upgrade pip
2933
pip install -r requirements.txt
34+
pip install ruff pytest pytest-mock
35+
36+
- name: Run Ruff
37+
run: |
38+
ruff check mytonctrl # wip adding ruff linting across the whole project
3039
3140
- name: Run pytest
3241
run: |
33-
pip install pytest pytest-mock
3442
pytest

mytonctrl/mytonctrl.py

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf_8 -*-
31
import base64
42
import pathlib
53
import subprocess
64
import json
75
import psutil
86
import inspect
97
import socket
8+
import sys
9+
import getopt
10+
import os
1011

1112
from functools import partial
1213

@@ -23,21 +24,18 @@
2324
get_load_avg,
2425
run_as_root,
2526
time2human,
26-
timeago,
27-
timestamp2datetime,
2827
get_timestamp,
2928
print_table,
3029
color_print,
3130
color_text,
3231
bcolors,
3332
Dict,
34-
MyPyClass, ip2int
33+
MyPyClass
3534
)
3635

3736
from mypyconsole.mypyconsole import MyPyConsole
3837
from mytoncore.mytoncore import MyTonCore
3938
from mytoncore.functions import (
40-
Slashing,
4139
GetMemoryInfo,
4240
GetSwapInfo,
4341
GetBinGitHash,
@@ -47,9 +45,6 @@
4745
from mytonctrl.migrate import run_migrations
4846
from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt, \
4947
pop_user_from_args, pop_arg_from_args
50-
51-
import sys, getopt, os
52-
5348
from mytoninstaller.archive_blocks import download_blocks
5449
from mytoninstaller.utils import get_ton_storage_port
5550

@@ -89,12 +84,6 @@ def inject_globals(func):
8984
console.AddItem("rollback", inject_globals(rollback_to_mtc1), local.translate("rollback_cmd"))
9085
console.AddItem("download_archive_blocks", inject_globals(download_archive_blocks), local.translate("download_archive_blocks_cmd"))
9186

92-
#console.AddItem("xrestart", inject_globals(Xrestart), local.translate("xrestart_cmd"))
93-
#console.AddItem("xlist", inject_globals(Xlist), local.translate("xlist_cmd"))
94-
#console.AddItem("gpk", inject_globals(GetPubKey), local.translate("gpk_cmd"))
95-
#console.AddItem("ssoc", inject_globals(SignShardOverlayCert), local.translate("ssoc_cmd"))
96-
#console.AddItem("isoc", inject_globals(ImportShardOverlayCert), local.translate("isoc_cmd"))
97-
9887
from modules.backups import BackupModule
9988
module = BackupModule(ton, local)
10089
module.add_console_commands(console)
@@ -180,12 +169,10 @@ def inject_globals(func):
180169
print ("Wallets path " + wallets + " is not a directory")
181170
sys.exit()
182171
ton.walletsDir = wallets
183-
#end for
184172

185173
local.db.config.logLevel = "debug" if console.debug else "info"
186174
local.db.config.isLocaldbSaving = False
187175
local.run()
188-
#end define
189176

190177

191178
def about(local, ton, args):
@@ -265,7 +252,7 @@ def GetAuthorRepoBranchFromArgs(args):
265252
def check_vport(local, ton):
266253
try:
267254
vconfig = ton.GetValidatorConfig()
268-
except:
255+
except Exception:
269256
local.add_log("GetValidatorConfig error", "error")
270257
return
271258
addr = vconfig.addrs.pop()
@@ -524,7 +511,7 @@ def check_validator_balance(local, ton):
524511
validator_wallet = ton.GetValidatorWallet()
525512
validator_account = local.try_function(ton.GetAccount, args=[validator_wallet.addrB64])
526513
if validator_account is None:
527-
local.add_log(f"Failed to check validator wallet balance", "warning")
514+
local.add_log("Failed to check validator wallet balance", "warning")
528515
return
529516
if validator_account.balance < 100:
530517
print_warning(local, "validator_balance_warning")
@@ -559,7 +546,8 @@ def check_adnl(local, ton):
559546
config = ton.GetValidatorConfig()
560547
if config.fullnodeslaves:
561548
return
562-
except: ...
549+
except Exception:
550+
pass
563551
from modules.utilities import UtilitiesModule
564552
utils_module = UtilitiesModule(ton, local)
565553
ok, error = utils_module.check_adnl_connection()
@@ -633,12 +621,12 @@ def PrintStatus(local, ton, args):
633621
disks_load_avg = ton.GetStatistics("disksLoadAvg", statistics)
634622
disks_load_percent_avg = ton.GetStatistics("disksLoadPercentAvg", statistics)
635623

636-
all_status = validator_status.is_working == True and validator_status.out_of_sync < 20
624+
all_status = validator_status.is_working and validator_status.out_of_sync < 20
637625

638626
try:
639627
vconfig = ton.GetValidatorConfig()
640628
fullnode_adnl = base64.b64decode(vconfig.fullnode).hex().upper()
641-
except:
629+
except Exception:
642630
fullnode_adnl = 'n/a'
643631

644632
if all_status:
@@ -782,11 +770,7 @@ def PrintLocalStatus(local, ton, adnlAddr, validatorIndex, validatorEfficiency,
782770
# Disks status
783771
disksLoad_data = list()
784772
for key, item in disksLoadAvg.items():
785-
diskLoad1_text = bcolors.green_text(item[0]) # TODO: this variables is unused. Why?
786-
diskLoad5_text = bcolors.green_text(item[1]) # TODO: this variables is unused. Why?
787773
diskLoad15_text = bcolors.green_text(item[2])
788-
diskLoadPercent1_text = GetColorInt(disksLoadPercentAvg[key][0], 80, logic="less", ending="%") # TODO: this variables is unused. Why?
789-
diskLoadPercent5_text = GetColorInt(disksLoadPercentAvg[key][1], 80, logic="less", ending="%") # TODO: this variables is unused. Why?
790774
diskLoadPercent15_text = GetColorInt(disksLoadPercentAvg[key][2], 80, logic="less", ending="%")
791775
buff = "{}, {}"
792776
buff = "{}{}:[{}{}{}]{}".format(bcolors.cyan, key, bcolors.default, buff, bcolors.cyan, bcolors.endc)
@@ -941,8 +925,8 @@ def PrintLocalStatus(local, ton, adnlAddr, validatorIndex, validatorEfficiency,
941925
print()
942926
#end define
943927

944-
def GetColorStatus(input):
945-
if input == True:
928+
def GetColorStatus(status: bool):
929+
if status:
946930
result = bcolors.green_text("working")
947931
else:
948932
result = bcolors.red_text("not working")
@@ -1030,7 +1014,7 @@ def GetColorTime(datetime, timestamp):
10301014
def GetSettings(ton, args):
10311015
try:
10321016
name = args[0]
1033-
except:
1017+
except IndexError:
10341018
color_print("{red}Bad args. Usage:{endc} get <settings-name>")
10351019
return
10361020
result = ton.GetSettings(name)
@@ -1041,7 +1025,7 @@ def SetSettings(local, ton, args):
10411025
try:
10421026
name = args[0]
10431027
value = args[1]
1044-
except:
1028+
except IndexError:
10451029
color_print("{red}Bad args. Usage:{endc} set <settings-name> <settings-value>")
10461030
return
10471031
if name == 'usePool' or name == 'useController':
@@ -1070,7 +1054,7 @@ def SetSettings(local, ton, args):
10701054
def enable_mode(local, ton, args):
10711055
try:
10721056
name = args[0]
1073-
except:
1057+
except IndexError:
10741058
color_print("{red}Bad args. Usage:{endc} enable_mode <mode_name>")
10751059
return
10761060
ton.enable_mode(name)
@@ -1081,7 +1065,7 @@ def enable_mode(local, ton, args):
10811065
def disable_mode(local, ton, args):
10821066
try:
10831067
name = args[0]
1084-
except:
1068+
except IndexError:
10851069
color_print("{red}Bad args. Usage:{endc} disable_mode <mode_name>")
10861070
return
10871071
ton.disable_mode(name)
@@ -1090,46 +1074,6 @@ def disable_mode(local, ton, args):
10901074
#end define
10911075

10921076

1093-
def Xrestart(inputArgs):
1094-
if len(inputArgs) < 2:
1095-
color_print("{red}Bad args. Usage:{endc} xrestart <timestamp> <args>")
1096-
return
1097-
with get_package_resource_path('mytonctrl', 'scripts/xrestart.py') as xrestart_script_path:
1098-
args = ["python3", xrestart_script_path] # TODO: Fix path
1099-
args += inputArgs
1100-
exitCode = run_as_root(args)
1101-
if exitCode == 0:
1102-
text = "Xrestart - {green}OK{endc}"
1103-
else:
1104-
text = "Xrestart - {red}Error{endc}"
1105-
color_print(text)
1106-
#end define
1107-
1108-
def Xlist(args):
1109-
color_print("Xlist - {green}OK{endc}")
1110-
#end define
1111-
1112-
def GetPubKey(ton, args):
1113-
adnlAddr = ton.GetAdnlAddr()
1114-
pubkey = ton.GetPubKey(adnlAddr)
1115-
print("pubkey:", pubkey)
1116-
#end define
1117-
1118-
def SignShardOverlayCert(ton, args):
1119-
try:
1120-
adnl = args[0]
1121-
pubkey = args[0]
1122-
except:
1123-
color_print("{red}Bad args. Usage:{endc} ssoc <pubkey>")
1124-
return
1125-
ton.SignShardOverlayCert(adnl, pubkey)
1126-
#end define
1127-
1128-
def ImportShardOverlayCert(ton, args):
1129-
ton.ImportShardOverlayCert()
1130-
#end define
1131-
1132-
11331077
def download_archive_blocks(local, args: list):
11341078
if len(args) < 2:
11351079
color_print("{red}Bad args. Usage:{endc} download_archive_blocks [ton_storage_api_port] <download_path> <from_block_seqno> [to_block_seqno] [--only-master]")
@@ -1145,22 +1089,22 @@ def download_archive_blocks(local, args: list):
11451089
to_block = args[2] if len(args) >= 3 else None
11461090
try:
11471091
from_block, to_block = int(from_block), int(to_block) if to_block else None
1148-
except:
1092+
except ValueError:
11491093
color_print("{red}Bad args. from_block and to_block must be integers.{endc}")
11501094
return
11511095

11521096
if api_port is None:
11531097
api_port = get_ton_storage_port(local)
11541098
if api_port is None:
1155-
raise Exception(f'Failed to get Ton Storage API port and port was not provided')
1099+
raise Exception('Failed to get Ton Storage API port and port was not provided')
11561100

11571101
# check ton storage is alive
11581102
local_ts_url = f"http://127.0.0.1:{api_port}"
11591103

11601104
try:
11611105
requests.get(local_ts_url + '/api/v1/list', timeout=3)
1162-
except:
1163-
color_print(f"{{red}}Error: cannot connect to ton-storage at 127.0.0.1:{api_port}. "
1106+
except Exception as e:
1107+
color_print(f"{{red}}Error: cannot connect to ton-storage at 127.0.0.1:{api_port}: {type(e)}: {e}. "
11641108
f"Make sure `ton_storage` daemon is running or install it via `installer enable TS`.{{endc}}")
11651109
return
11661110

mytonctrl/progressbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def snake_process(self):
5757
indent_len = 0
5858
cycles = int(self.timeout / self.sleep_time)
5959
for cycle in range(cycles):
60-
if self.thread.is_alive() == False:
60+
if not self.thread.is_alive():
6161
break
6262
sleep(self.sleep_time)
6363
if indent_len == self.toolbar_width:

mytonctrl/scripts/etabar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
process = bar.run(run, args, stdin=PIPE, stdout=PIPE, stderr=PIPE, timeout=timeout)
1515
exit_code = -1
1616
output = "process is None"
17-
if process != None:
17+
if process is not None:
1818
exit_code = process.returncode
1919
stdout = process.stdout.decode("utf-8")
2020
stderr = process.stderr.decode("utf-8")

mytonctrl/scripts/xrestart.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

mytonctrl/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def timestamp2utcdatetime(timestamp, format="%d.%m.%Y %H:%M:%S"):
1717
def GetItemFromList(data, index):
1818
try:
1919
return data[index]
20-
except:
20+
except IndexError:
2121
pass
2222

2323

0 commit comments

Comments
 (0)