Skip to content

Commit

Permalink
Merge pull request #11994 from thesujai/plugin-flake8-print
Browse files Browse the repository at this point in the history
Introduce flake8-print as pre-commit hook with migration of print to logger
  • Loading branch information
rtibbles authored Mar 17, 2024
2 parents 91d93e6 + 128d72a commit ff2357b
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ repos:
hooks:
- id: trailing-whitespace
- id: flake8
additional_dependencies: [
'flake8-print==5.0.0'
]
- id: check-yaml
- id: check-added-large-files
- id: debug-statements
Expand Down
12 changes: 7 additions & 5 deletions build_tools/customize_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
For more detail see the documentation in __init__.py
"""
import logging
import os
import sys
import tempfile
Expand All @@ -13,6 +14,7 @@

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))

logger = logging.getLogger(__name__)
plugins_cache = {}


Expand All @@ -21,7 +23,7 @@ def load_plugins_from_file(file_path):
if file_path not in plugins_cache:
# We have been passed a URL, not a local file path
if file_path.startswith("http"):
print(
logger.info(
"Downloading plugins manifest from {file_path}".format(
file_path=file_path
)
Expand Down Expand Up @@ -53,7 +55,7 @@ def set_default_settings_module():
default_settings_path = os.environ["DEFAULT_SETTINGS_MODULE"]
with open(os.path.join(build_config_path, "default_settings.py"), "w") as f:
# Just write out settings_path = '<settings_path>'
print(
logger.info(
"Setting default settings module to {path}".format(
path=default_settings_path
)
Expand All @@ -69,10 +71,10 @@ def set_run_time_plugins():
runtime_plugins = load_plugins_from_file(os.environ["RUN_TIME_PLUGINS"])
with open(os.path.join(build_config_path, "default_plugins.py"), "w") as f:
# Just write out 'plugins = [...]' <-- list of plugins
print("Setting run time plugins to:")
logger.info("Setting run time plugins to:")
for runtime_plugin in runtime_plugins:
print(runtime_plugin)
print("### End run time plugins ###")
logger.info(runtime_plugin)
logger.info("### End run time plugins ###")
f.write(run_time_plugin_template.format(plugins=runtime_plugins.__str__()))


Expand Down
5 changes: 4 additions & 1 deletion build_tools/customize_docker_envlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
For more detail see the documentation in __init__.py
"""
import logging
import os

BUILD_ENV_PREFIX = "KOLIBRI_BUILD_"
ENVLIST_FILE = os.path.abspath(
os.path.join(os.path.dirname(os.path.dirname(__file__)), "docker", "env.list")
)

logger = logging.getLogger(__name__)


def add_env_var_to_docker():
envs = os.environ
Expand All @@ -23,7 +26,7 @@ def add_env_var_to_docker():
if env.startswith(BUILD_ENV_PREFIX):
key = env.replace(BUILD_ENV_PREFIX, "")
f.write("\n{key}={value}".format(key=key, value=envs[env]))
print(
logger.info(
"Writing value of environment variable {} to Docker env.list\n".format(
key
)
Expand Down
5 changes: 4 additions & 1 deletion build_tools/customize_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
For more detail see the documentation in __init__.py
"""
import logging
import os
import tempfile

import requests

logger = logging.getLogger(__name__)


def add_requirements_to_base():
if "EXTRA_REQUIREMENTS" in os.environ and os.environ["EXTRA_REQUIREMENTS"]:
file_path = os.environ["EXTRA_REQUIREMENTS"]
# We have been passed a URL, not a local file path
if file_path.startswith("http"):
print(
logger.info(
"Downloading extra requirements from {file_path}".format(
file_path=file_path
)
Expand Down
11 changes: 7 additions & 4 deletions build_tools/install_cexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
created under the directory where the script runs to store the cache data.
"""
import argparse
import logging
import os
import shutil
import subprocess
Expand All @@ -41,6 +42,8 @@
PYPI_DOWNLOAD = "https://pypi.python.org/simple/"
PIWHEEL_DOWNLOAD = "https://www.piwheels.org/simple/"

logger = logging.getLogger(__name__)


def get_path_with_arch(platform, path, abi, implementation, python_version):
"""
Expand Down Expand Up @@ -135,7 +138,7 @@ def install_package(package_name, package_version, index_url, info, cache_path):
platform, version_path, abi, implementation, python_version
)

print("Installing package {}...".format(filename))
logger.info("Installing package {}...".format(filename))
# Install the package using pip with cache_path as the cache directory
install_return = run_pip_install(
package_path,
Expand Down Expand Up @@ -218,13 +221,13 @@ def parse_pypi_and_piwheels(name, pk_version, cache_path, session):
r = session.get(url)
r.raise_for_status()
except Exception as e:
print("Error retrieving {}: {}".format(url, e))
logger.info("Error retrieving {}: {}".format(url, e))
else:
if r.status_code == 200:
# Got a valid response
break

print(
logger.info(
"Unexpected response from {}: {} {}".format(
url, r.status_code, r.reason
)
Expand Down Expand Up @@ -253,7 +256,7 @@ def check_cache_path_writable(cache_path):
return cache_path
except (OSError, IOError):
new_path = os.path.realpath("cext_cache")
print(
logger.info(
"The cache directory {old_path} is not writable. Changing to directory {new_path}.".format(
old_path=cache_path, new_path=new_path
)
Expand Down
7 changes: 5 additions & 2 deletions build_tools/preseed_home.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import os
import shutil
import tempfile

temphome = tempfile.mkdtemp()
os.environ["KOLIBRI_HOME"] = temphome

logger = logging.getLogger(__name__)

from kolibri.main import initialize # noqa E402
from kolibri.deployment.default.sqlite_db_names import ( # noqa E402
ADDITIONAL_SQLITE_DATABASES,
Expand All @@ -16,7 +19,7 @@
shutil.rmtree(move_to, ignore_errors=True)
os.mkdir(move_to)

print("Generating preseeded home data in {}".format(temphome))
logger.info("Generating preseeded home data in {}".format(temphome))

initialize()
call_command(
Expand All @@ -27,6 +30,6 @@
if db_config["ENGINE"] == "django.db.backends.sqlite3":
shutil.move(os.path.join(temphome, db_config["NAME"]), move_to)

print("Moved all preseeded home data to {}".format(move_to))
logger.info("Moved all preseeded home data to {}".format(move_to))

shutil.rmtree(temphome)
6 changes: 4 additions & 2 deletions build_tools/read_whl_version.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import logging
import sys

from pkginfo import Wheel

logger = logging.getLogger(__name__)

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: read_whl_version.py <whl_file>")
logger.error("Usage: read_whl_version.py <whl_file>")
sys.exit(1)

whl_file = sys.argv[1]
whl = Wheel(whl_file)
print(whl.version)
logger.info(whl.version)
sys.exit(0)
2 changes: 1 addition & 1 deletion integration_testing/scripts/run_kolibri_app_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def RUN(self):
start_url = "http://127.0.0.1:{port}".format(
port=self.port
) + interface.get_initialize_url(auth_token="1234")
print("Kolibri running at: {start_url}".format(start_url=start_url))
logging.info("Kolibri running at: {start_url}".format(start_url=start_url))


logging.info("Initializing Kolibri and running any upgrade routines")
Expand Down
7 changes: 5 additions & 2 deletions kolibri/core/analytics/management/commands/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import sys

from django.conf import settings
Expand All @@ -17,6 +18,8 @@
from kolibri.utils.system import get_free_space
from kolibri.utils.time_utils import local_now

logger = logging.getLogger(__name__)


def format_line(parameter, value, indented=False):
if indented:
Expand Down Expand Up @@ -75,7 +78,7 @@ class Command(BaseCommand):

def handle(self, *args, **options):
if not SUPPORTED_OS:
print("This OS is not yet supported")
logger.error("This OS is not yet supported")
sys.exit(1)

try:
Expand Down Expand Up @@ -146,7 +149,7 @@ def handle(self, *args, **options):
self.messages.append(format_line("Server timezone", settings.TIME_ZONE))

self.messages.append("")
print("\n".join(self.messages))
logger.info("\n".join(self.messages))

def add_header(self, header):
self.messages.append("")
Expand Down
13 changes: 8 additions & 5 deletions kolibri/core/analytics/management/commands/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import logging
import os.path
import sys
import time
Expand All @@ -16,6 +17,8 @@
from kolibri.utils.server import PROFILE_LOCK
from kolibri.utils.system import pid_exists

logger = logging.getLogger(__name__)


def remove_lock():
# Kolibri command PID file exists but no command is running, it's corrupted
Expand Down Expand Up @@ -54,11 +57,11 @@ def add_arguments(self, parser):

def check_start_conditions(self):
if not SUPPORTED_OS:
print("This OS is not yet supported")
logger.error("This OS is not yet supported")
sys.exit(1)

if not conf.OPTIONS["Server"]["PROFILE"]:
print(
logger.error(
"Kolibri has not enabled profiling of its requests."
"To enable it, edit the Kolibri options.ini file and "
"add `PROFILE = true` in the [Server] section"
Expand All @@ -73,7 +76,7 @@ def check_start_conditions(self):
remove_lock()
if command_pid:
if pid_exists(command_pid):
print("Profile command is already running")
logger.error("Profile command is already running")
sys.exit(1)
else:
remove_lock()
Expand All @@ -89,7 +92,7 @@ def handle(self, *args, **options):
f.write("%d" % this_pid)
f.write("\n{}".format(file_timestamp))
except (IOError, OSError):
print(
logger.error(
"Impossible to create profile lock file. Kolibri won't profile its requests"
)
samples = 1
Expand All @@ -102,7 +105,7 @@ def handle(self, *args, **options):
try:
os.mkdir(performance_dir)
except OSError:
print("Not enough permissions to write performance logs")
logger.error("Not enough permissions to write performance logs")
sys.exit(1)
with open(self.performance_file, mode="w") as profile_file:
profile_writer = csv.writer(
Expand Down
9 changes: 7 additions & 2 deletions kolibri/core/analytics/measurements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from collections import namedtuple
from datetime import timedelta

Expand All @@ -16,6 +17,8 @@
from kolibri.utils.server import NotRunning
from kolibri.utils.server import PID_FILE

logger = logging.getLogger(__name__)

try:
import kolibri.utils.pskolibri as psutil
except NotImplementedError:
Expand Down Expand Up @@ -53,7 +56,9 @@ def get_db_info():
).count()
)
except OperationalError:
print("Database unavailable, impossible to retrieve users and sessions info")
logger.info(
"Database unavailable, impossible to retrieve users and sessions info"
)

return (active_sessions, active_users, active_users_minute)

Expand Down Expand Up @@ -94,7 +99,7 @@ def get_channels_usage_info():
)
)
except OperationalError:
print("Database unavailable, impossible to retrieve channels usage info")
logger.info("Database unavailable, impossible to retrieve channels usage info")
return channels_info


Expand Down
8 changes: 6 additions & 2 deletions kolibri/core/auth/management/commands/deleteuser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging

from django.core.management.base import BaseCommand
from django.core.management.base import CommandError

from kolibri.core.auth.management.utils import confirm_or_exit
from kolibri.core.auth.models import FacilityUser

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "To allow administrators to comply with GDPR requests, this command initiates the deletion process for a user."
Expand Down Expand Up @@ -51,10 +55,10 @@ def handle(self, *args, **options):
"ARE YOU SURE? If you do this, there is no way to recover the user data on this device."
)

print(
logger.info(
"Proceeding with user deletion. Deleting all data for user <{}>".format(
options["username"]
)
)
user.delete(hard_delete=True)
print("Deletion complete. All data for this user has been deleted.")
logger.info("Deletion complete. All data for this user has been deleted.")
4 changes: 2 additions & 2 deletions kolibri/core/auth/management/commands/deprovision.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def handle_async(self, *args, **options):
"ARE YOU SURE? If you do this, there is no way to recover the user data on this device."
)

print("Proceeding with deprovisioning. Deleting all user data.")
logger.info("Proceeding with deprovisioning. Deleting all user data.")
self.deprovision()
print("Deprovisioning complete. All user data has been deleted.")
logger.info("Deprovisioning complete. All user data has been deleted.")
Loading

0 comments on commit ff2357b

Please sign in to comment.