From ac546a97110362367b0b9a78c98aacc3f587f5ea Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 14 Aug 2018 11:47:54 -0400 Subject: [PATCH] Closes #2000: Remove support for Python 2 --- .travis.yml | 1 - README.md | 2 -- base_requirements.txt | 2 +- docs/additional-features/netbox-shell.md | 2 +- docs/installation/upgrading.md | 7 ------- netbox/extras/reports.py | 23 ++++++----------------- netbox/netbox/settings.py | 15 +++++++-------- netbox/utilities/middleware.py | 6 +----- requirements.txt | 2 +- upgrade.sh | 16 ---------------- 10 files changed, 17 insertions(+), 59 deletions(-) diff --git a/.travis.yml b/.travis.yml index 33abc8425..13c6d406b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ addons: postgresql: "9.4" language: python python: - - "2.7" - "3.5" install: - pip install -r requirements.txt diff --git a/README.md b/README.md index 28673bf36..9955316b9 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,6 @@ or join us in the #netbox Slack channel on [NetworkToCode](https://networktocode ### Build Status -NetBox is built against both Python 2.7 and 3.5. Python 3.5 or higher is strongly recommended. - | | status | |-------------|------------| | **master** | [![Build Status](https://travis-ci.org/digitalocean/netbox.svg?branch=master)](https://travis-ci.org/digitalocean/netbox) | diff --git a/base_requirements.txt b/base_requirements.txt index 6012ffa6c..5f3e0eb80 100644 --- a/base_requirements.txt +++ b/base_requirements.txt @@ -1,5 +1,5 @@ # django-filter-1.1.0 breaks with Django-2.1 -Django>=1.11,<2.1 +Django>=2.0,<2.1 django-cors-headers django-debug-toolbar # django-filter-2.0.0 drops Python 2 support (blocked by #2000) diff --git a/docs/additional-features/netbox-shell.md b/docs/additional-features/netbox-shell.md index 5afd7876d..2ebea5ce5 100644 --- a/docs/additional-features/netbox-shell.md +++ b/docs/additional-features/netbox-shell.md @@ -9,7 +9,7 @@ This will launch a customized version of [the built-in Django shell](https://doc ``` $ ./manage.py nbshell ### NetBox interactive shell (jstretch-laptop) -### Python 2.7.6 | Django 1.11.3 | NetBox 2.1.0-dev +### Python 3.5.2 | Django 2.0.8 | NetBox 2.4.3 ### lsmodels() will show available models. Use help() for more info. ``` diff --git a/docs/installation/upgrading.md b/docs/installation/upgrading.md index bca60ca89..6dc8a3c7a 100644 --- a/docs/installation/upgrading.md +++ b/docs/installation/upgrading.md @@ -64,13 +64,6 @@ Once the new code is in place, run the upgrade script (which may need to be run # ./upgrade.sh ``` -!!! warning - The upgrade script will prefer Python3 and pip3 if both executables are available. To force it to use Python2 and pip, use the `-2` argument as below. Note that Python 2 will no longer be supported in NetBox v2.5. - -```no-highlight -# ./upgrade.sh -2 -``` - This script: * Installs or upgrades any new required Python packages diff --git a/netbox/extras/reports.py b/netbox/extras/reports.py index 52883063c..f6b5d7570 100644 --- a/netbox/extras/reports.py +++ b/netbox/extras/reports.py @@ -4,7 +4,6 @@ from collections import OrderedDict import importlib import inspect import pkgutil -import sys from django.conf import settings from django.utils import timezone @@ -26,22 +25,12 @@ def get_report(module_name, report_name): """ file_path = '{}/{}.py'.format(settings.REPORTS_ROOT, module_name) - # Python 3.5+ - if sys.version_info >= (3, 5): - spec = importlib.util.spec_from_file_location(module_name, file_path) - module = importlib.util.module_from_spec(spec) - try: - spec.loader.exec_module(module) - except FileNotFoundError: - return None - - # Python 2.7 - else: - import imp - try: - module = imp.load_source(module_name, file_path) - except IOError: - return None + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + try: + spec.loader.exec_module(module) + except FileNotFoundError: + return None report = getattr(module, report_name, None) if report is None: diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index f6d1b26fd..116192a02 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -7,6 +7,13 @@ import warnings from django.contrib.messages import constants as messages from django.core.exceptions import ImproperlyConfigured +# Check for Python 3.5+ +if sys.version_info < (3, 5): + raise RuntimeError( + "NetBox requires Python 3.5 or higher (current: Python {})".format(sys.version.split()[0]) + ) + +# Check for configuration file try: from netbox import configuration except ImportError: @@ -14,14 +21,6 @@ except ImportError: "Configuration file is not present. Please define netbox/netbox/configuration.py per the documentation." ) -# Raise a deprecation warning for Python 2.x -if sys.version_info[0] < 3: - warnings.warn( - "Support for Python 2 will be removed in NetBox v2.5. Please consider migration to Python 3 at your earliest " - "opportunity. Guidance is available in the documentation at http://netbox.readthedocs.io/.", - DeprecationWarning - ) - VERSION = '2.4.4-dev' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) diff --git a/netbox/utilities/middleware.py b/netbox/utilities/middleware.py index dafafde24..207968690 100644 --- a/netbox/utilities/middleware.py +++ b/netbox/utilities/middleware.py @@ -72,11 +72,7 @@ class ExceptionHandlingMiddleware(object): custom_template = 'exceptions/programming_error.html' elif isinstance(exception, ImportError): custom_template = 'exceptions/import_error.html' - elif ( - sys.version_info[0] >= 3 and isinstance(exception, PermissionError) - ) or ( - isinstance(exception, OSError) and exception.errno == 13 - ): + elif isinstance(exception, PermissionError): custom_template = 'exceptions/permission_error.html' # Return a custom error message, or fall back to Django's default 500 error handling diff --git a/requirements.txt b/requirements.txt index b3bee6b6d..8fc83c4d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -Django>=1.11,<2.1 +Django>=2.0,<2.1 django-cors-headers==2.4.0 django-debug-toolbar==1.9.1 django-filter==1.1.0 diff --git a/upgrade.sh b/upgrade.sh index a1930eb3d..3a9f4a864 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -5,24 +5,8 @@ # Once the script completes, remember to restart the WSGI service (e.g. # gunicorn or uWSGI). -# Determine which version of Python/pip to use. Default to v3 (if available) -# but allow the user to force v2. PYTHON="python3" PIP="pip3" -type $PYTHON >/dev/null 2>&1 && type $PIP >/dev/null 2>&1 || PYTHON="python" PIP="pip" -while getopts ":2" opt; do - case $opt in - 2) - PYTHON="python" - PIP="pip" - echo "Forcing Python/pip v2" - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - exit - ;; - esac -done # Optionally use sudo if not already root, and always prompt for password # before running the command