Skip to content

Commit

Permalink
Merge pull request #2903 from johannaengland/drop-python-37-code-changes
Browse files Browse the repository at this point in the history
Remove imports/adjustments to make NAV run on Python<3.9/Django<3.2
  • Loading branch information
lunkwill42 authored Aug 23, 2024
2 parents d3381e9 + 17a5009 commit d532a75
Show file tree
Hide file tree
Showing 22 changed files with 37 additions and 130 deletions.
1 change: 1 addition & 0 deletions changelog.d/+drop-py37-code-changes.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change Python code due to dropping Python 3.7
31 changes: 0 additions & 31 deletions python/nav/adapters.py

This file was deleted.

2 changes: 1 addition & 1 deletion python/nav/alertengine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import gc
import logging
from datetime import datetime, timedelta
from functools import lru_cache

from django.db import transaction, reset_queries

from nav.compatibility import lru_cache
from nav.models.profiles import (
Account,
AccountAlertQueue,
Expand Down
10 changes: 3 additions & 7 deletions python/nav/buildconf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"""NAV build configuration variables."""

# pylint: disable=invalid-name
from importlib import metadata
import os
import sysconfig

try:
from importlib import metadata as _impmeta
except ImportError:
import importlib_metadata as _impmeta


datadir = os.path.join(sysconfig.get_config_var('datarootdir'), 'nav')
localstatedir = os.path.join(datadir, 'var')
Expand All @@ -18,8 +14,8 @@


try:
VERSION = _impmeta.version("nav")
except _impmeta.PackageNotFoundError:
VERSION = metadata.version("nav")
except metadata.PackageNotFoundError:
# If we're not installed, try to get the current version from Git tags
import setuptools_scm

Expand Down
9 changes: 0 additions & 9 deletions python/nav/compatibility.py

This file was deleted.

5 changes: 1 addition & 4 deletions python/nav/eventengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
#
"""NAV eventengine"""

try:
from subprocess32 import STDOUT, check_output, TimeoutExpired, CalledProcessError
except ImportError:
from subprocess import STDOUT, check_output, TimeoutExpired, CalledProcessError
from subprocess import STDOUT, check_output, TimeoutExpired, CalledProcessError


def get_eventengine_output(timeout=10):
Expand Down
5 changes: 1 addition & 4 deletions python/nav/eventengine/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
"""Alert stream export functionality"""
import logging
import json
import subprocess

from django.core.serializers.json import DjangoJSONEncoder

try:
import subprocess32 as subprocess
except ImportError:
import subprocess

from nav.web.api.v1.alert_serializers import AlertQueueSerializer

Expand Down
1 change: 0 additions & 1 deletion python/nav/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def _get_logging_conf():
"""
filename = os.environ.get(LOGGING_CONF_VAR, LOGGING_CONF_FILE_DEFAULT)
config = configparser.ConfigParser()
# Warning about `bytes` on py 3.5, fixed in py 3.7. Do not change
read = config.read(filename)
if filename not in read and LOGGING_CONF_VAR in os.environ:
_logger.error(
Expand Down
2 changes: 1 addition & 1 deletion python/nav/metrics/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#
"""Functions for reverse-mapping metric names to NAV objects"""

from functools import lru_cache
import re

from nav.compatibility import lru_cache
from nav.models.manage import Netbox, Interface, Prefix, Sensor


Expand Down
2 changes: 1 addition & 1 deletion python/nav/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

from datetime import datetime

from django.contrib.postgres.fields import HStoreField
from django.db import models
from django.urls import reverse

from nav.adapters import HStoreField
from nav.models.fields import VarcharField
from nav.models.profiles import Account

Expand Down
2 changes: 1 addition & 1 deletion python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@

import IPy
from django.conf import settings
from django.contrib.postgres.fields import HStoreField
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import JSONField, Q
from django.db.models.expressions import RawSQL
from django.urls import reverse

from nav import util
from nav.adapters import HStoreField
from nav.bitvector import BitVector
from nav.metrics.data import get_netboxes_availability
from nav.metrics.graphs import get_simple_graph_url, Graph
Expand Down
6 changes: 3 additions & 3 deletions python/nav/models/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import re
import json

from django.views.decorators.debug import sensitive_variables
from django.contrib.postgres.fields import HStoreField
from django.db import models, transaction
from django.urls import reverse
from django.forms.models import model_to_dict
from django.urls import reverse
from django.views.decorators.debug import sensitive_variables

from nav.adapters import HStoreField
import nav.buildconf
import nav.pwhash
from nav.config import getconfig as get_alertengine_config
Expand Down
15 changes: 1 addition & 14 deletions python/nav/statemon/abstractchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,11 @@ def run(self):
service = "%s:%s" % (self.sysname, self.get_type())
_logger.info("%-20s -> %s", service, info)

if status == event.Event.UP:
# Dirty hack to check if we timed out...
# this is needed as ssl-socket calls may hang
# in python < 2.3
if self.response_time > 2 * self.timeout:
_logger.info(
"Adjusting status due to high responsetime (%s, " "%s)",
service,
self.response_time,
)
status = event.Event.DOWN
self.response_time = 2 * self.timeout

if status != self.status and (self.runcount < int(self._conf.get('retry', 3))):
delay = int(self._conf.get('retry delay', 5))
self.runcount += 1
_logger.info(
"%-20s -> State changed. New check in %i sec. (%s, " "%s)",
"%-20s -> State changed. New check in %i sec. (%s, %s)",
service,
delay,
status,
Expand Down
10 changes: 3 additions & 7 deletions python/nav/statemon/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
"""
from __future__ import absolute_import

import threading

try:
import queue
except ImportError:
import Queue as queue
import time
import atexit
from collections import defaultdict
import logging
import queue
import time
import threading

import psycopg2
from psycopg2.errorcodes import IN_FAILED_SQL_TRANSACTION
Expand Down
6 changes: 1 addition & 5 deletions python/nav/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
import uuid
import hashlib
from functools import wraps
from importlib.resources import as_file, files as resource_files
from itertools import chain, tee, groupby, islice
from operator import itemgetter
from secrets import token_hex

import IPy

try:
from importlib.resources import as_file, files as resource_files
except ImportError: # Python 3.7!
from importlib_resources import as_file, files as resource_files


def gradient(start, stop, steps):
"""Create and return a sequence of steps representing an integer
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/ipdevinfo/host_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#
"""Provides a function for getting host information"""

from functools import lru_cache

import IPy
from nav import asyncdns
from nav.compatibility import lru_cache

from nav.util import is_valid_ip

Expand Down
6 changes: 1 addition & 5 deletions python/nav/web/sortedstats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

import logging
from datetime import datetime

try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo
from zoneinfo import ZoneInfo

from django.shortcuts import render
from django.core.cache import caches
Expand Down
23 changes: 10 additions & 13 deletions tests/integration/auditlog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@ def test_str(self):
l.delete()

def test_add_log_entry_bad_template(self):
LogEntry.add_log_entry(
self.justification, u'bad template test', u'this is a {bad} template'
)
l = LogEntry.objects.filter(verb='bad template test').get()
self.assertEqual(l.summary, u'Error creating summary - see error log')
l.delete()

# # When on python3:
# with self.assertLogs(level='ERROR') as log:
# # run body
# self.assertEqual(len(log.output), 1)
# self.assertEqual(len(log.records), 1)
# self.assertIn('KeyError when creating summary:', log.output[0])
with self.assertLogs(level='ERROR') as log:
LogEntry.add_log_entry(
self.justification, u'bad template test', u'this is a {bad} template'
)
l = LogEntry.objects.filter(verb='bad template test').get()
self.assertEqual(l.summary, u'Error creating summary - see error log')
l.delete()
self.assertEqual(len(log.output), 1)
self.assertEqual(len(log.records), 1)
self.assertIn('KeyError when creating summary:', log.output[0])

def test_add_log_entry_actor_only(self):
LogEntry.add_log_entry(
Expand Down
8 changes: 2 additions & 6 deletions tests/integration/pping_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""
various pping integration tests
"""

import os
from pathlib import Path
import getpass
from shutil import which

try:
from subprocess32 import STDOUT, check_output, TimeoutExpired, CalledProcessError
except ImportError:
from subprocess import STDOUT, check_output, TimeoutExpired, CalledProcessError
from subprocess import STDOUT, check_output, CalledProcessError

import pytest

Expand Down
8 changes: 2 additions & 6 deletions tests/integration/smsd_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""
smsd integration tests
"""

import os
import os.path

try:
from subprocess32 import STDOUT, check_output, TimeoutExpired, CalledProcessError
except ImportError:
from subprocess import STDOUT, check_output, TimeoutExpired, CalledProcessError
from subprocess import STDOUT, check_output, TimeoutExpired, CalledProcessError

import pytest
from mock import Mock, patch

from nav.config import find_config_file, find_config_dir

Expand Down
7 changes: 2 additions & 5 deletions tests/integration/web/crawler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
URLs that report a Content-Type of text/html.
"""

from __future__ import print_function

from collections import namedtuple
from http.client import BadStatusLine
from lxml.html import fromstring
import os

try:
from http.client import BadStatusLine
except ImportError:
from httplib import BadStatusLine

import pytest

from tidylib import tidy_document
Expand Down
6 changes: 1 addition & 5 deletions tests/unittests/general/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
#

from __future__ import unicode_literals
from io import StringIO
from os import makedirs, remove, rmdir, listdir
from os.path import join

import pytest

try:
from io import StringIO
except ImportError:
from StringIO import StringIO

from nav import config


Expand Down

0 comments on commit d532a75

Please sign in to comment.