From ace339eabf0d76f9aeb840005a80113f365ef133 Mon Sep 17 00:00:00 2001 From: Nikolas Nyby Date: Tue, 23 Jul 2019 21:43:16 -0400 Subject: [PATCH] Introduce flake8 checking - #58 This introduces flake8 and fixes a bunch of flake8 errors. flake8 is run with: ./venv/bin/flake8 plinth if you're using a python3 venv. We can eventually further integrate this with gitlab ci. https://salsa.debian.org/freedombox-team/plinth/issues/58 Reviewed-by: Sunil Mohan Adapa --- debian/control | 1 + plinth/__main__.py | 2 +- plinth/axes_app_config.py | 1 + plinth/module_loader.py | 4 ++-- plinth/modules/backups/decorators.py | 2 +- plinth/modules/backups/forms.py | 9 ++++----- plinth/modules/backups/tests/test_api.py | 2 -- plinth/modules/backups/tests/test_ssh_remotes.py | 6 +----- plinth/modules/coquelicot/views.py | 4 ++-- plinth/modules/dynamicdns/__init__.py | 2 +- plinth/modules/networks/urls.py | 3 ++- plinth/modules/searx/__init__.py | 1 - plinth/setup.py | 3 ++- plinth/tests/test_context_processors.py | 1 + plinth/tests/test_network.py | 3 +-- setup.py | 7 ++++++- 16 files changed, 26 insertions(+), 25 deletions(-) diff --git a/debian/control b/debian/control index aab367b04..b8a83a7d1 100644 --- a/debian/control +++ b/debian/control @@ -29,6 +29,7 @@ Build-Depends: python3-django-axes (>= 3.0.3), python3-django-captcha, python3-django-stronghold (>= 0.3.0), + python3-flake8, python3-gi, python3-paramiko, python3-psutil, diff --git a/plinth/__main__.py b/plinth/__main__.py index ee99b2c66..d0a15b461 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -66,7 +66,7 @@ def run_setup_and_exit(module_list, allow_install=True): error_code = 0 try: setup.run_setup_on_modules(module_list, allow_install) - except Exception as exception: + except Exception: error_code = 1 sys.exit(error_code) diff --git a/plinth/axes_app_config.py b/plinth/axes_app_config.py index cca9097fa..2d8a2b553 100644 --- a/plinth/axes_app_config.py +++ b/plinth/axes_app_config.py @@ -26,4 +26,5 @@ class AppConfig(apps.AppConfig): def ready(self): # Signals must be loaded for axes to get the login_failed signals + # flake8: noqa from axes import signals # isort:skip pylint: disable=unused-import diff --git a/plinth/module_loader.py b/plinth/module_loader.py index ff4b95c30..877b4573b 100644 --- a/plinth/module_loader.py +++ b/plinth/module_loader.py @@ -22,7 +22,6 @@ import collections import django import importlib import logging -import os import pathlib import re @@ -157,7 +156,8 @@ def get_modules_to_load(): # './setup.py install' has not been executed yet. Pickup files to load # from local module directories. directory = pathlib.Path(__file__).parent - files = list(directory.glob('modules/*/data/etc/plinth/modules-enabled/*')) + files = list(directory.glob( + 'modules/*/data/etc/plinth/modules-enabled/*')) # Omit hidden files files = [ diff --git a/plinth/modules/backups/decorators.py b/plinth/modules/backups/decorators.py index 4ac2dcd45..b8cd92270 100644 --- a/plinth/modules/backups/decorators.py +++ b/plinth/modules/backups/decorators.py @@ -28,7 +28,7 @@ def delete_tmp_backup_file(function): def wrap(request, *args, **kwargs): path = request.session.get(SESSION_PATH_VARIABLE, None) - if path: + if path: if os.path.isfile(path): os.remove(path) del request.session[SESSION_PATH_VARIABLE] diff --git a/plinth/modules/backups/forms.py b/plinth/modules/backups/forms.py index b331f0334..431082589 100644 --- a/plinth/modules/backups/forms.py +++ b/plinth/modules/backups/forms.py @@ -19,10 +19,8 @@ Forms for backups module. """ import logging -import os import re import subprocess -import tempfile from django import forms from django.core.exceptions import ValidationError @@ -204,9 +202,10 @@ class VerifySshHostkeyForm(forms.Form): stderr=subprocess.DEVNULL) keys = keyscan.stdout.decode().splitlines() # Generate user-friendly fingerprints of public keys - keygen = subprocess.run(['ssh-keygen', '-l', '-f', '-'], - input=keyscan.stdout, - stdout=subprocess.PIPE) + keygen = subprocess.run( + ['ssh-keygen', '-l', '-f', '-'], + input=keyscan.stdout, + stdout=subprocess.PIPE) fingerprints = keygen.stdout.decode().splitlines() return zip(keys, fingerprints) diff --git a/plinth/modules/backups/tests/test_api.py b/plinth/modules/backups/tests/test_api.py index 222d1c714..e760b3248 100644 --- a/plinth/modules/backups/tests/test_api.py +++ b/plinth/modules/backups/tests/test_api.py @@ -23,8 +23,6 @@ from unittest.mock import MagicMock, call, patch import pytest from django.core.files.uploadedfile import SimpleUploadedFile -from plinth import cfg, module_loader - from .. import ROOT_REPOSITORY, api, forms # pylint: disable=protected-access diff --git a/plinth/modules/backups/tests/test_ssh_remotes.py b/plinth/modules/backups/tests/test_ssh_remotes.py index ceb76b0a9..417909f2d 100644 --- a/plinth/modules/backups/tests/test_ssh_remotes.py +++ b/plinth/modules/backups/tests/test_ssh_remotes.py @@ -21,18 +21,14 @@ Tests for SSH remotes for backups. import datetime import os import pwd -import shutil import subprocess -import tempfile import pytest from django.forms import ValidationError -from django.urls import reverse -from plinth.modules.backups import network_storage from plinth.utils import generate_password, random_string -from .. import forms, views +from .. import forms pytestmark = [ pytest.mark.usefixtures('needs_root', 'load_cfg', 'has_ssh_key'), diff --git a/plinth/modules/coquelicot/views.py b/plinth/modules/coquelicot/views.py index 4eaa91e6b..6a59a4c56 100644 --- a/plinth/modules/coquelicot/views.py +++ b/plinth/modules/coquelicot/views.py @@ -57,7 +57,7 @@ class CoquelicotAppView(views.AppView): 'coquelicot', ['set-upload-password'], input=form_data['upload_password'].encode()) messages.success(self.request, _('Upload password updated')) - except ActionError as e: + except ActionError: messages.error(self.request, _('Failed to update upload password')) @@ -68,7 +68,7 @@ class CoquelicotAppView(views.AppView): 'coquelicot', ['set-max-file-size', str(max_file_size)]) messages.success(self.request, _('Maximum file size updated')) - except ActionError as e: + except ActionError: messages.error(self.request, _('Failed to update maximum file size')) diff --git a/plinth/modules/dynamicdns/__init__.py b/plinth/modules/dynamicdns/__init__.py index 56b4dcc87..92baee282 100644 --- a/plinth/modules/dynamicdns/__init__.py +++ b/plinth/modules/dynamicdns/__init__.py @@ -97,7 +97,7 @@ def setup(helper, old_version=None): def get_enabled_services(domain_name): """Get enabled services for the domain name.""" - if domain_name != None and domain_name != '': + if domain_name is not None and domain_name != '': try: domainname_services = firewall.get_enabled_services( zone='external') diff --git a/plinth/modules/networks/urls.py b/plinth/modules/networks/urls.py index 2cc514b76..6e93f68f1 100644 --- a/plinth/modules/networks/urls.py +++ b/plinth/modules/networks/urls.py @@ -38,7 +38,8 @@ urlpatterns = [ url(r'^sys/networks/add/ethernet/$', views.add_ethernet, name='add_ethernet'), url(r'^sys/networks/add/pppoe/$', views.add_pppoe, name='add_pppoe'), - url(r'^sys/networks/add/wifi/(?:(?P[^/]+)/(?P[^/]+)/)?$', + url(r'^sys/networks/add/wifi/(?:(?P[^/]+)/' + r'(?P[^/]+)/)?$', views.add_wifi, name='add_wifi'), url(r'^sys/networks/(?P[\w.@+-]+)/delete/$', views.delete, name='delete'), diff --git a/plinth/modules/searx/__init__.py b/plinth/modules/searx/__init__.py index 3fd554568..89a80bf1c 100644 --- a/plinth/modules/searx/__init__.py +++ b/plinth/modules/searx/__init__.py @@ -25,7 +25,6 @@ from django.utils.translation import ugettext_lazy as _ from plinth import action_utils, actions from plinth import app as app_module from plinth import frontpage, menu -from plinth.daemon import Daemon from plinth.modules.apache.components import Uwsgi, Webserver from plinth.modules.firewall.components import Firewall from plinth.modules.users import register_group diff --git a/plinth/setup.py b/plinth/setup.py index 28adc51de..76beaa3c6 100644 --- a/plinth/setup.py +++ b/plinth/setup.py @@ -423,7 +423,8 @@ class ForceUpgrader(): """Raised when upgrade fails but can be tried again immediately.""" class PermanentFailure(Exception): - """Raised when upgrade fails and there is nothing more we wish to do.""" + """Raised when upgrade fails and there is nothing more we wish to do. + """ def __init__(self): """Initialize the force upgrader.""" diff --git a/plinth/tests/test_context_processors.py b/plinth/tests/test_context_processors.py index dfc7bd712..146f61ec4 100644 --- a/plinth/tests/test_context_processors.py +++ b/plinth/tests/test_context_processors.py @@ -28,6 +28,7 @@ from plinth import cfg from plinth import context_processors as cp from plinth import menu as menu_module + @pytest.fixture(name='menu', autouse=True) def fixture_menu(): """Initialized menu module.""" diff --git a/plinth/tests/test_network.py b/plinth/tests/test_network.py index 72be1fab7..ba772eb04 100644 --- a/plinth/tests/test_network.py +++ b/plinth/tests/test_network.py @@ -83,6 +83,7 @@ pppoe_settings = { }, } + @pytest.fixture(name='network') def fixture_network(needs_root): """Return the network module. Load it conservatively.""" @@ -116,7 +117,6 @@ def fixture_pppoe_uuid(network): yield from _connection(network, pppoe_settings) - @pytest.mark.usefixtures('ethernet_uuid', 'wifi_uuid', 'pppoe_uuid') def test_get_connection_list(network): """Check that we can get a list of available connections.""" @@ -127,7 +127,6 @@ def test_get_connection_list(network): assert 'plinth_test_pppoe' in [x['name'] for x in connections] - def test_get_connection(network, ethernet_uuid, wifi_uuid): """Check that we can get a connection by name.""" connection = network.get_connection(ethernet_uuid) diff --git a/setup.py b/setup.py index 97066fbd4..01ce6e397 100755 --- a/setup.py +++ b/setup.py @@ -268,7 +268,12 @@ setuptools.setup( 'requests', 'ruamel.yaml', ], - tests_require=['pytest', 'pytest-cov', 'pytest-django'], + tests_require=[ + 'pytest', + 'pytest-cov', + 'pytest-django', + 'flake8', + ], package_data={ '': ['templates/*', 'static/*', 'locale/*/LC_MESSAGES/*.[pm]o'] },