Skip to content

Commit

Permalink
Merge branch 'johan/ruff' into python
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 30, 2023
2 parents 2259495 + d94b9d1 commit bf6caf8
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 85 deletions.
23 changes: 3 additions & 20 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
# Disable all categories since we're running ruff instead, but VSCode insists on
# invoking pylint all the time
[MESSAGES CONTROL]
disable=
consider-using-max-builtin,
disallowed-name,
fixme,
global-statement,
invalid-name,
line-too-long,
missing-class-docstring,
missing-function-docstring,
missing-module-docstring,
protected-access, # Used a lot in tests, maybe make this tests-specific?
too-few-public-methods,
too-many-arguments,
too-many-branches,
too-many-instance-attributes,
too-many-locals,
too-many-return-statements,
too-many-statements,
wrong-import-order,
wrong-import-position,
disable=E,W,R,C,F
4 changes: 2 additions & 2 deletions devbin/benchmark_ipcmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
MYDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(MYDIR, ".."))

from tests import testutils
from px import px_file
from tests import testutils # noqa: E402
from px import px_file # noqa: E402


# For how long should we do the benchmarking run (in seconds)
Expand Down
2 changes: 1 addition & 1 deletion devbin/benchmark_proc_get_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
MYDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(MYDIR, ".."))

from px import px_process
from px import px_process # noqa: E402

LAPS = 20

Expand Down
10 changes: 5 additions & 5 deletions px/px.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def main():

try:
_main(argv)
except Exception: # pylint: disable=broad-except
except Exception:
LOG = logging.getLogger(__name__)
LOG.exception("Uncaught Exception")

Expand Down Expand Up @@ -139,7 +139,7 @@ def handleLogMessages(messages: Optional[str]) -> None:
# even if we don't use it. And this will make test avoidance fail to avoid
# px.py tests every time you make a new commit (because committing recreates
# version.py).
from . import version # pylint: disable=import-outside-toplevel
from . import version

sys.stderr.write("px version: " + version.VERSION + "\n")

Expand Down Expand Up @@ -168,7 +168,7 @@ def _main(argv: List[str]) -> None:
# NOTE: If we "import version" at the top of this file, we will depend on it even if
# we don't use it. And this will make test avoidance fail to avoid px.py tests every
# time you make a new commit (because committing recreates version.py).
from . import version # pylint: disable=import-outside-toplevel
from . import version

print(version.VERSION)
return
Expand Down Expand Up @@ -236,14 +236,14 @@ def _main(argv: List[str]) -> None:

if top:
# Pulling px_top in on demand like this improves test result caching
from . import px_top # pylint: disable=import-outside-toplevel
from . import px_top

px_top.top(search=search)
return

if tree:
# Pulling px_tree in on demand like this improves test result caching
from . import px_tree # pylint: disable=import-outside-toplevel
from . import px_tree

px_tree.tree(search=search)
return
Expand Down
1 change: 0 additions & 1 deletion px/px_cpuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def get_core_count_from_sysctl() -> Optional[Tuple[int, int]]:
def parse_sysctl_output(
sysctl_lines: List[str],
) -> Tuple[Optional[int], Optional[int]]:

# Note the ending spaces, they must be there for number extraction to work!
PHYSICAL_PREFIX = "hw.physicalcpu: "
LOGICAL_PREFIX = "hw.logicalcpu: "
Expand Down
1 change: 0 additions & 1 deletion px/px_cwdfriends.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(
all_processes: List[px_process.PxProcess],
all_files: List[px_file.PxFile],
) -> None:

pid_to_process: Dict[int, px_process.PxProcess] = {}
for p in all_processes:
pid_to_process[p.pid] = p
Expand Down
2 changes: 1 addition & 1 deletion px/px_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def resolve_endpoint(endpoint: str) -> str:

try:
host = socket.gethostbyaddr(address)[0]
except Exception: # pylint: disable=broad-except
except Exception:
# Lookup failed for whatever reason, give up
#
# Catching "Exception" because I am (on 2022may27) unable to figure out
Expand Down
2 changes: 1 addition & 1 deletion px/px_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def install(src, dest):
"""
try:
_install(src, dest)
except Exception as e: # pylint: disable=broad-except
except Exception as e:
sys.stderr.write(f"Installing {dest} failed, please retry with sudo\n")
sys.stderr.write(f"Error was: {str(e)}\n")
sys.exit(1)
Expand Down
3 changes: 0 additions & 3 deletions px/px_ioload.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ def parse_proc_diskstats(proc_diskstats_contents: str) -> List[Sample]:

class SubsystemStat:
def __init__(self, throughput: float, high_watermark: float) -> None:

if throughput > high_watermark:
raise ValueError(
f"High watermark {high_watermark} lower than throughput {throughput}"
Expand Down Expand Up @@ -247,7 +246,6 @@ def update_baseline_from_system(self, system_state: SystemState) -> None:
del self.baseline[remove_me]

def update(self) -> None:

self.previous_system_state = self.most_recent_system_state
self.most_recent_system_state = SystemState()
self.update_baseline_from_system(self.most_recent_system_state)
Expand Down Expand Up @@ -342,7 +340,6 @@ def get_load_string(self) -> str:
math.trunc(bottleneck[1]), math.trunc(bottleneck[2])
)

# pylint: disable=consider-using-f-string
return "[{} / {}] {}".format(
px_terminal.bold(current_throughput + "/s"),
max_throughput + "/s",
Expand Down
4 changes: 1 addition & 3 deletions px/px_ipc_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def __str__(self):


class IpcMap:
# pylint: disable=attribute-defined-outside-init
"""
This is a map of process->[channels], where "process" is a process we have
IPC communication open with, and a channel is a socket or a pipe that we
Expand All @@ -59,7 +58,6 @@ def __init__(
processes: Iterable[px_process.PxProcess],
is_root: bool,
) -> None:

# On Linux, lsof reports the same open file once per thread of a
# process. Putting the files in a set gives us each file only once.
files = set(files)
Expand Down Expand Up @@ -130,7 +128,7 @@ def _create_fds(self, is_root: bool) -> Dict[int, str]:
fds[network_connection.fd] = str(network_connection)

# Traverse our IPC structure and update FDs as required
for target in self.keys(): # pylint: disable=consider-using-dict-items
for target in self.keys():
for link in self[target]:
if link.fd is None:
# No FD, never mind
Expand Down
3 changes: 0 additions & 3 deletions px/px_launchcounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def _merge_tuple_lists(


def _callchain(process: px_process.PxProcess) -> Tuple[str, ...]:

reverse_callchain: List[str] = []

current: Optional[px_process.PxProcess] = process
Expand All @@ -125,7 +124,6 @@ def _register_launches(self, new_processes: List[px_process.PxProcess]) -> None:
self._hierarchies[callchain] = 1

def update(self, procs_snapshot: List[px_process.PxProcess]) -> None:

if self._last_processlist is None:
self._last_processlist = procs_snapshot
return
Expand Down Expand Up @@ -155,7 +153,6 @@ def _coalesce_launchers(self) -> List[List[Tuple[str, int]]]:
return coalesced

def get_screen_lines(self) -> List[str]:

launchers_list = self._coalesce_launchers()
launchers_list = sort_launchers_list(launchers_list)

Expand Down
4 changes: 2 additions & 2 deletions px/px_loginhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get_users_at(
from_timestamp = _to_timestamp(from_s, now)
if timestamp < from_timestamp:
continue
except Exception: # pylint: disable=broad-except
except Exception:
LOG.error("Problematic1 last line: <%s>", line)
continue

Expand All @@ -120,7 +120,7 @@ def get_users_at(
to_timestamp = from_timestamp + duration_delta
if timestamp > to_timestamp:
continue
except Exception: # pylint: disable=broad-except
except Exception:
LOG.error("Problematic2 last line: <%s>", line)

users.add(username)
Expand Down
3 changes: 0 additions & 3 deletions px/px_meminfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@


def get_meminfo() -> str:

total_ram_bytes, wanted_ram_bytes = _get_ram_numbers()
percentage = (100.0 * wanted_ram_bytes) / total_ram_bytes

Expand Down Expand Up @@ -82,7 +81,6 @@ def _update_from_meminfo(base: Optional[int], line: str, name: str) -> Optional[
def _get_ram_numbers_from_proc(
proc_meminfo: str = "/proc/meminfo",
) -> Optional[Tuple[int, int]]:

total_kb: Optional[int] = None
available_kb: Optional[int] = None
free_kb: Optional[int] = None
Expand Down Expand Up @@ -188,7 +186,6 @@ def _update_if_prefix(base: Optional[int], line: str, prefix: str) -> Optional[i
def _get_ram_numbers_from_vm_stat_output(
vm_stat_lines: List[str],
) -> Optional[Tuple[int, int]]:

# List based on https://apple.stackexchange.com/a/196925/182882
page_size_bytes = None
pages_free = None
Expand Down
5 changes: 2 additions & 3 deletions px/px_pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _pump_info_to_fd(with_fileno, process, processes):
LOG.warning(
"Unexpected OSError pumping process info into pager", exc_info=True
)
except Exception: # pylint: disable=broad-except
except Exception:
# Logging exceptions on warning level will make them visible to somebody
# who changes the LOGLEVEL in px.py, but not to ordinary users.
#
Expand Down Expand Up @@ -117,7 +117,6 @@ def launch_pager():
def page_process_info(
process: px_process.PxProcess, processes: List[px_process.PxProcess]
) -> None:

pager = launch_pager()
pager_stdin = pager.stdin
assert pager_stdin is not None
Expand All @@ -130,7 +129,7 @@ def page_process_info(
# Terminating ptop while this is running is fine. This is deprecated since
# Python 3.10, but we want to support older Pythons as well so let's keep it
# this way for now.
info_thread.setDaemon(True) # pylint: disable=deprecated-method
info_thread.setDaemon(True)

info_thread.start()

Expand Down
2 changes: 0 additions & 2 deletions px/px_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def _parse_time(time_s: str) -> datetime.datetime:


class PxProcess:
# pylint: disable=attribute-defined-outside-init
def __init__(
self,
cmdline: str,
Expand Down Expand Up @@ -254,7 +253,6 @@ def __init__(self):
self.memory_percent: Optional[float] = None

def __repr__(self):
# pylint: disable=consider-using-f-string
return (
"start_time_string=%r pid=%r ppid=%r user=%r cpu%%=%r cputime=%r mem%%=%r cmd=<%r>"
% (
Expand Down
4 changes: 0 additions & 4 deletions px/px_processinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def print_users_when_process_started(fd: int, process: px_process.PxProcess) ->


def to_ipc_lines(ipc_map: px_ipc_map.IpcMap) -> Iterable[str]:

return_me = []
for target in sorted(ipc_map.keys(), key=operator.attrgetter("name", "pid")):
channels = ipc_map[target]
Expand Down Expand Up @@ -234,7 +233,6 @@ def print_cwd_friends(fd, process, all_processes, all_files):
def print_fds(
fd: int, process: px_process.PxProcess, processes: Iterable[px_process.PxProcess]
) -> None:

# It's true, I measured it myself /[email protected]
println(
fd,
Expand Down Expand Up @@ -299,7 +297,6 @@ def print_fds(


def print_start_time(fd: int, process: px_process.PxProcess) -> None:
# pylint: disable=consider-using-f-string
println(
fd,
"{} {} was started by {}, at {}.".format(
Expand All @@ -312,7 +309,6 @@ def print_start_time(fd: int, process: px_process.PxProcess) -> None:

if process.cpu_time_seconds and process.age_seconds:
cpu_percent = 100.0 * process.cpu_time_seconds / process.age_seconds
# pylint: disable=consider-using-f-string
println(
fd,
"{} has been its average CPU usage since then, or {}/{}".format(
Expand Down
4 changes: 1 addition & 3 deletions px/px_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ def get_command(**kwargs):


def _top(search: str = "") -> None:

global search_string
search_string = search

Expand Down Expand Up @@ -554,7 +553,6 @@ def _top(search: str = "") -> None:


def top(search: str = "") -> None:

if not sys.stdout.isatty():
sys.stderr.write(
'Top mode only works on TTYs, try running just "px" instead.\n'
Expand All @@ -564,7 +562,7 @@ def top(search: str = "") -> None:
with px_terminal.fullscreen_display():
try:
_top(search=search)
except Exception: # pylint: disable=broad-except
except Exception:
LOG.exception("Running ptop failed")

# Make sure we actually end up on a new line
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"ptop = px.px:main",
"pxtree = px.px:main",
],
}
},
# Note that we're by design *not* installing man pages here.
# Using "data_files=" only puts the man pages in the egg file,
# and installing that egg doesn't put them on the destination
Expand Down
5 changes: 1 addition & 4 deletions tests/px_exec_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ def test_exec_true_with_check():
def test_exec_false_with_check():
try:
px_exec_util.run(["false"], check_exitcode=True)
assert (
False # pylint: disable=condition-evals-to-constant
and "We should never get here"
)
assert False and "We should never get here"
except subprocess.CalledProcessError:
# This is the exception we want, done!
pass
2 changes: 0 additions & 2 deletions tests/px_loginhistory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

# These warnings conflict with how pytest fixtures work:
# https://docs.pytest.org/en/6.2.x/fixture.html
#
# pylint: disable=redefined-outer-name,unused-argument


@pytest.fixture
Expand Down
Loading

0 comments on commit bf6caf8

Please sign in to comment.