summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MANIFEST.in1
-rw-r--r--Makefile5
-rwxr-xr-xpsutil/tests/test_linux.py6
-rwxr-xr-xscripts/internal/fix_flake8.py185
4 files changed, 6 insertions, 191 deletions
diff --git a/MANIFEST.in b/MANIFEST.in
index 13acb0fd..1816eb5f 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -137,7 +137,6 @@ include scripts/internal/clinter.py
include scripts/internal/convert_readme.py
include scripts/internal/download_wheels_appveyor.py
include scripts/internal/download_wheels_github.py
-include scripts/internal/fix_flake8.py
include scripts/internal/generate_manifest.py
include scripts/internal/git_pre_commit.py
include scripts/internal/print_access_denied.py
diff --git a/Makefile b/Makefile
index 0ab1ea06..16c7bb26 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ TSCRIPT = psutil/tests/runner.py
# Internal.
DEPS = \
argparse \
+ autopep8 \
check-manifest \
concurrencytest \
coverage \
@@ -206,8 +207,8 @@ lint: ## Run all linters
# Fixers
# ===================================================================
-fix-flake8: ## Attempt to automatically fix some Python flake8 issues.
- @git ls-files | grep \\.py$ | xargs $(PYTHON) -m flake8 --exit-zero | $(PYTHON) scripts/internal/fix_flake8.py
+fix-flake8: ## Run autopep8, fix some Python flake8 / pep8 issues.
+ @git ls-files | grep \\.py$ | xargs $(PYTHON) -m autopep8 --in-place --jobs 0 --global-config=.flake8
fix-imports: ## Fix imports with isort.
@git ls-files '*.py' | xargs $(PYTHON) -m isort --settings=.isort.cfg
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index a0c69c78..0d42d051 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -119,8 +119,8 @@ def get_ipv6_addresses(ifname):
fields = line.split()
if fields[-1] == ifname:
all_fields.append(fields)
-
- if len(all_fields) ==0:
+
+ if len(all_fields) == 0:
raise ValueError("could not find interface %r" % ifname)
for i in range(0, len(all_fields)):
@@ -956,7 +956,7 @@ class TestSystemNetIfAddrs(PsutilTestCase):
# of the network interface.
address = addr.address.split('%')[0]
self.assertIn(address, get_ipv6_addresses(name))
-
+
# XXX - not reliable when having virtual NICs installed by Docker.
# @unittest.skipIf(not which('ip'), "'ip' utility not available")
# def test_net_if_names(self):
diff --git a/scripts/internal/fix_flake8.py b/scripts/internal/fix_flake8.py
deleted file mode 100755
index 14fbb4d2..00000000
--- a/scripts/internal/fix_flake8.py
+++ /dev/null
@@ -1,185 +0,0 @@
-#!/usr/bin/env python3
-
-# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-"""
-Fix some flake8 errors by rewriting files for which flake8 emitted
-an error/warning. Usage (from the root dir):
-
- $ python3 -m flake8 --exit-zero | python3 scripts/fix_flake8.py
-"""
-
-import shutil
-import sys
-import tempfile
-from collections import defaultdict
-from collections import namedtuple
-from pprint import pprint as pp # NOQA
-
-
-ntentry = namedtuple('ntentry', 'msg, issue, lineno, pos, descr')
-
-
-# =====================================================================
-# utils
-# =====================================================================
-
-
-def enter_pdb():
- from pdb import set_trace as st # trick GIT commit hook
- sys.stdin = open('/dev/tty')
- st()
-
-
-def read_lines(fname):
- with open(fname, 'rt') as f:
- return f.readlines()
-
-
-def read_line(fname, lineno):
- with open(fname, 'rt') as f:
- for i, line in enumerate(f, 1):
- if i == lineno:
- return line
- raise ValueError("lineno too big")
-
-
-def write_file(fname, newlines):
- with tempfile.NamedTemporaryFile('wt', delete=False) as f:
- for line in newlines:
- f.write(line)
- shutil.move(f.name, fname)
-
-
-# =====================================================================
-# handlers
-# =====================================================================
-
-
-def handle_f401(fname, lineno):
- """ This is 'module imported but not used'
- Able to handle this case:
- import os
-
- ...but not this:
- from os import listdir
- """
- line = read_line(fname, lineno).strip()
- if line.startswith('import '):
- return True
- else:
- mods = line.partition(' import ')[2] # everything after import
- return ',' not in mods
-
-
-# =====================================================================
-# converters
-# =====================================================================
-
-
-def remove_lines(fname, entries):
- """Check if we should remove lines, then do it.
- Return the numner of lines removed.
- """
- to_remove = []
- for entry in entries:
- msg, issue, lineno, pos, descr = entry
- # 'module imported but not used'
- if issue == 'F401' and handle_f401(fname, lineno):
- to_remove.append(lineno)
- # 'blank line(s) at end of file'
- elif issue == 'W391':
- lines = read_lines(fname)
- i = len(lines) - 1
- while lines[i] == '\n':
- to_remove.append(i + 1)
- i -= 1
- # 'too many blank lines'
- elif issue == 'E303':
- howmany = descr.replace('(', '').replace(')', '')
- howmany = int(howmany[-1])
- for x in range(lineno - howmany, lineno):
- to_remove.append(x)
-
- if to_remove:
- newlines = []
- for i, line in enumerate(read_lines(fname), 1):
- if i not in to_remove:
- newlines.append(line)
- print("removing line(s) from %s" % fname)
- write_file(fname, newlines)
-
- return len(to_remove)
-
-
-def add_lines(fname, entries):
- """Check if we should remove lines, then do it.
- Return the numner of lines removed.
- """
- EXPECTED_BLANK_LINES = (
- 'E302', # 'expected 2 blank limes, found 1'
- 'E305') # ìexpected 2 blank lines after class or fun definition'
- to_add = {}
- for entry in entries:
- msg, issue, lineno, pos, descr = entry
- if issue in EXPECTED_BLANK_LINES:
- howmany = 2 if descr.endswith('0') else 1
- to_add[lineno] = howmany
-
- if to_add:
- newlines = []
- for i, line in enumerate(read_lines(fname), 1):
- if i in to_add:
- newlines.append('\n' * to_add[i])
- newlines.append(line)
- print("adding line(s) to %s" % fname)
- write_file(fname, newlines)
-
- return len(to_add)
-
-
-# =====================================================================
-# main
-# =====================================================================
-
-
-def build_table():
- table = defaultdict(list)
- for line in sys.stdin:
- line = line.strip()
- if not line:
- break
- fields = line.split(':')
- fname, lineno, pos = fields[:3]
- issue = fields[3].split()[0]
- descr = fields[3].strip().partition(' ')[2]
- lineno, pos = int(lineno), int(pos)
- table[fname].append(ntentry(line, issue, lineno, pos, descr))
- return table
-
-
-def main():
- table = build_table()
-
- # remove lines (unused imports)
- removed = 0
- for fname, entries in table.items():
- removed += remove_lines(fname, entries)
- if removed:
- print("%s lines were removed from some file(s); please re-run" %
- removed)
- return
-
- # add lines (missing \n between functions/classes)
- added = 0
- for fname, entries in table.items():
- added += add_lines(fname, entries)
- if added:
- print("%s lines were added from some file(s); please re-run" %
- added)
- return
-
-
-main()