Skip to content

Commit

Permalink
Merge pull request #101 from walles/johan/pylint
Browse files Browse the repository at this point in the history
Add pylint to tox
  • Loading branch information
walles authored May 28, 2022
2 parents 0d4cee0 + 2e51270 commit 61e4700
Show file tree
Hide file tree
Showing 42 changed files with 419 additions and 382 deletions.
22 changes: 22 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[MESSAGES CONTROL]
disable=
consider-using-f-string, # We should, in a separate change
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,
14 changes: 6 additions & 8 deletions devbin/benchmark_ipcmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
"""

import os
import sys
import time

MYDIR = os.path.dirname(os.path.abspath(__file__))
from typing import MutableMapping

import sys

MYDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(MYDIR, ".."))

import time

from tests import testutils
from px import px_file

from typing import MutableMapping


# For how long should we do the benchmarking run (in seconds)
DURATION_S = 30
Expand All @@ -49,7 +47,7 @@ def get_timings(file, pid):
"""
t0 = time.time()
files = None
with open(file, "r") as lsof_output:
with open(file, "r", encoding="utf-8") as lsof_output:
files = px_file.lsof_to_files(lsof_output.read())
t1 = time.time()
dt_load = t1 - t0
Expand All @@ -73,7 +71,7 @@ def print_statistics(name, values):
def main(lsof_file):
print("Finding most popular PID...")
files = None
with open(lsof_file, "r") as lsof_output:
with open(lsof_file, "r", encoding="utf-8") as lsof_output:
files = px_file.lsof_to_files(lsof_output.read())
pid = get_most_common_pid(files)
print("Most popular PID: {}".format(pid))
Expand Down
10 changes: 4 additions & 6 deletions devbin/benchmark_proc_get_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
"""

import os

MYDIR = os.path.dirname(os.path.abspath(__file__))

import sys
import time

sys.path.insert(0, os.path.join(MYDIR, ".."))

import time
MYDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(MYDIR, ".."))

from px import px_process

Expand All @@ -23,7 +21,7 @@

def main():
t0 = time.time()
for iteration in range(LAPS):
for _ in range(LAPS):
px_process.get_all()
t1 = time.time()
dt_seconds = t1 - t0
Expand Down
3 changes: 1 addition & 2 deletions devbin/stop-using-kill-9.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import signal


def receiveSignal(signalNumber, frame):
def receiveSignal(signalNumber, _):
print("Received signal " + str(signalNumber))
return


signal.signal(signal.SIGTERM, receiveSignal)
Expand Down
21 changes: 13 additions & 8 deletions px/px.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
from . import px_terminal
from . import px_processinfo

import sys

from typing import Optional, List


Expand Down Expand Up @@ -94,7 +92,7 @@ def main():

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

Expand Down Expand Up @@ -128,7 +126,14 @@ def handleLogMessages(messages: Optional[str]) -> None:
sys.stderr.write(ERROR_REPORTING_HEADER)
sys.stderr.write("\n")

from . import version
# If this fails, run "tox.sh" once and the "version.py" file will be created
# for you.
#
# 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

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

Expand Down Expand Up @@ -158,7 +163,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
from . import version # pylint: disable=import-outside-toplevel

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

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

px_top.top(search=search)
return
Expand All @@ -224,7 +229,7 @@ def _main(argv: List[str]) -> None:
processes = px_process.get_all()
process = px_processinfo.find_process_by_pid(pid, processes)
if not process:
exit("No such PID: {}\n".format(pid))
sys.exit("No such PID: {}\n".format(pid))

px_pager.page_process_info(process, processes)
return
Expand All @@ -236,7 +241,7 @@ def _main(argv: List[str]) -> None:

columns: Optional[int] = None
try:
rows, columns = px_terminal.get_window_size()
_, columns = px_terminal.get_window_size()
except px_terminal.TerminalError:
columns = None

Expand Down
17 changes: 9 additions & 8 deletions px/px_commandline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Extract information from command lines"""

import re
import sys
import os.path

from typing import List
Expand Down Expand Up @@ -260,7 +259,7 @@ def get_java_command(commandline: str) -> str:
# to just returning the command name
return java
return os.path.basename(component)
elif state == "scanning":
if state == "scanning":
if component.startswith("-X"):
continue
if component.startswith("-D"):
Expand Down Expand Up @@ -302,7 +301,7 @@ def get_java_command(commandline: str) -> str:
continue
if component == "-noverify":
continue
if component == "-cp" or component == "-classpath":
if component in ("-cp", "-classpath"):
state = "skip next"
continue
if component == "-jar":
Expand All @@ -312,20 +311,22 @@ def get_java_command(commandline: str) -> str:
# Unsupported switch, give up
return java
return prettify_fully_qualified_java_class(component)
else:
raise ValueError(
"Unhandled state <{}> at <{}> for: {}".format(state, component, array)
)

raise ValueError(
"Unhandled state <{}> at <{}> for: {}".format(state, component, array)
)

# We got to the end without being able to come up with a better name, give up
return java


def get_generic_script_command(
commandline: str, ignore_switches: List[str] = []
commandline: str, ignore_switches: Optional[List[str]] = None
) -> str:
array = to_array(commandline)

if ignore_switches is None:
ignore_switches = []
while len(array) > 1 and array[1].split("=")[0] in ignore_switches:
del array[1]

Expand Down
2 changes: 1 addition & 1 deletion px/px_cpuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_core_count_from_proc_cpuinfo(
core_ids = set()
max_processor_no = 0
try:
with open(proc_cpuinfo) as f:
with open(proc_cpuinfo, encoding="utf-8") as f:
for line in f:
if line.startswith(PROCESSOR_NO_PREFIX):
processor_no = int(line[len(PROCESSOR_NO_PREFIX) :])
Expand Down
4 changes: 1 addition & 3 deletions px/px_cwdfriends.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from . import px_process
from . import px_file
from typing import List
Expand All @@ -14,7 +12,7 @@ def _strip_leading_dash(process: px_process.PxProcess) -> str:
return key


class PxCwdFriends(object):
class PxCwdFriends:
def __init__(
self,
process: px_process.PxProcess,
Expand Down
14 changes: 6 additions & 8 deletions px/px_exec_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import subprocess

import sys

from typing import List
from typing import Dict

Expand All @@ -17,12 +15,12 @@


def run(command: List[str], check_exitcode: bool = False) -> str:
run = subprocess.Popen(
with subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV
)
stdout = run.communicate()[0].decode("utf-8")
) as execution:
stdout = execution.communicate()[0].decode("utf-8")

if check_exitcode and run.returncode != 0:
raise subprocess.CalledProcessError(run.returncode, command)
if check_exitcode and execution.returncode != 0:
raise subprocess.CalledProcessError(execution.returncode, command)

return stdout
return stdout
10 changes: 7 additions & 3 deletions px/px_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import Optional


class PxFile(object):
class PxFile:
def __init__(self, pid: int, filetype: str) -> None:
self.fd: Optional[int] = None
self.pid = pid
Expand Down Expand Up @@ -42,7 +42,7 @@ def __str__(self):
name = self.name
listen_suffix = ""
if self.type in ["IPv4", "IPv6"]:
local, remote_endpoint = self.get_endpoints()
_, remote_endpoint = self.get_endpoints()
if not remote_endpoint:
listen_suffix = " (LISTEN)"

Expand Down Expand Up @@ -179,8 +179,12 @@ def resolve_endpoint(endpoint: str) -> str:

try:
host = socket.gethostbyaddr(address)[0]
except Exception:
except Exception: # pylint: disable=broad-except
# Lookup failed for whatever reason, give up
#
# Catching "Exception" because I am (on 2022may27) unable to figure out
# from these docs what exceptions can be thrown by that method, if any:
# https://docs.python.org/3.10/library/socket.html#socket.gethostbyaddr
return endpoint

if host == "localhost.localdomain":
Expand Down
4 changes: 2 additions & 2 deletions px/px_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def install(src, dest):
"""
try:
_install(src, dest)
except Exception as e:
except Exception as e: # pylint: disable=broad-except
sys.stderr.write("Installing {} failed, please retry with sudo\n".format(dest))
sys.stderr.write("Error was: {}\n".format(str(e)))
exit(1)
sys.exit(1)
print("Created: {}".format(dest))


Expand Down
Loading

0 comments on commit 61e4700

Please sign in to comment.