Skip to content

Commit

Permalink
Closes #2000: Remove support for Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Aug 21, 2018
1 parent e1e41a7 commit cd1a559
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 59 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ addons:
postgresql: "9.4"
language: python
python:
- "2.7"
- "3.5"
install:
- pip install -r requirements.txt
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
2 changes: 1 addition & 1 deletion base_requirements.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/additional-features/netbox-shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<model>) for more info.
```

Expand Down
7 changes: 0 additions & 7 deletions docs/installation/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 6 additions & 17 deletions netbox/extras/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import importlib
import inspect
import pkgutil
import sys

from django.conf import settings
from django.utils import timezone
Expand All @@ -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:
Expand Down
15 changes: 7 additions & 8 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
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:
raise ImproperlyConfigured(
"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__)))
Expand Down
6 changes: 1 addition & 5 deletions netbox/utilities/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ def process_exception(self, request, exception):
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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 0 additions & 16 deletions upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd1a559

Please sign in to comment.