Skip to content

Commit

Permalink
Remove support for Python 2.
Browse files Browse the repository at this point in the history
Switch to pyproject.toml based build system.
  • Loading branch information
kevinsteves committed Apr 20, 2022
1 parent 662230c commit 5f81bf1
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 310 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include MANIFEST.in HISTORY.rst LICENSE.txt AUTHORS.rst
include HISTORY.rst
include doc/Makefile doc/*.rst doc/*.html
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ on GitHub and as a
`package <https://pypi.org/project/pan-python/>`_
on PyPi.

Python versions 2.7 and 3.x are supported with a single code base.
There are no external modules required to use ``pan-python``.
3 changes: 1 addition & 2 deletions bin/panafapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

#
# Copyright (c) 2015 Palo Alto Networks, Inc. <[email protected]>
Expand All @@ -16,7 +16,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
import datetime
import getopt
import json
Expand Down
3 changes: 1 addition & 2 deletions bin/panconf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

#
# Copyright (c) 2012-2014 Kevin Steves <[email protected]>
Expand All @@ -16,7 +16,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
import sys
import os
import signal
Expand Down
3 changes: 1 addition & 2 deletions bin/panlicapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

#
# Copyright (c) 2017 Palo Alto Networks, Inc. <[email protected]>
Expand All @@ -16,7 +16,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
import getopt
import json
import logging
Expand Down
53 changes: 21 additions & 32 deletions bin/panwfapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

#
# Copyright (c) 2013-2017 Kevin Steves <[email protected]>
Expand All @@ -16,7 +16,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
from datetime import date, timedelta
import sys
import os
Expand All @@ -26,10 +25,7 @@
import pprint
import logging
import ssl
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
from urllib.parse import urlparse

libpath = os.path.dirname(os.path.abspath(__file__))
sys.path[:0] = [os.path.join(libpath, os.pardir, 'lib')]
Expand Down Expand Up @@ -474,33 +470,26 @@ def parse_opts():


def create_ssl_context(cafile, capath, ssl_option):
# PEP 0476
if (sys.version_info.major == 2 and sys.hexversion >= 0x02070900 or
sys.version_info.major == 3 and sys.hexversion >= 0x03040300):
if cafile or capath:
try:
ssl_context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH,
cafile=cafile,
capath=capath)
except Exception as e:
print('cafile or capath invalid: %s' % e, file=sys.stderr)
sys.exit(1)
elif ssl_option:
if ssl_option == 'cacloud':
ssl_context = pan.wfapi.cloud_ssl_context()
elif ssl_option == 'noverify':
ssl_context = ssl._create_unverified_context()
elif ssl_option == 'default':
ssl_context = None

return ssl_context

print('Warning: Python %d.%d.%d: cafile, capath and ssl ignored' %
(sys.version_info.major, sys.version_info.minor,
sys.version_info.micro), file=sys.stderr)
if cafile or capath:
try:
ssl_context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH,
cafile=cafile,
capath=capath)
except Exception as e:
print('cafile or capath invalid: %s' % e, file=sys.stderr)
sys.exit(1)
elif ssl_option:
if ssl_option == 'cacloud':
ssl_context = pan.wfapi.cloud_ssl_context()
elif ssl_option == 'noverify':
ssl_context = ssl._create_unverified_context()
elif ssl_option == 'default':
ssl_context = None
else:
assert False, 'cafile or capath or ssl_option'

return None
return ssl_context


def print_status(wfapi, action, exception_msg=None):
Expand Down
58 changes: 19 additions & 39 deletions bin/panxapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

#
# Copyright (c) 2013-2015 Kevin Steves <[email protected]>
Expand All @@ -16,7 +16,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
from datetime import datetime
import sys
import os
Expand Down Expand Up @@ -610,28 +609,18 @@ def parse_opts():


def create_ssl_context(cafile, capath):
if (sys.version_info.major == 2 and sys.hexversion >= 0x02070900 or
sys.version_info.major == 3 and sys.hexversion >= 0x03020000):
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
context.verify_mode = ssl.CERT_REQUIRED
# added 3.4
if hasattr(context, 'check_hostname'):
context.check_hostname = True
try:
context.load_verify_locations(cafile=cafile, capath=capath)
except Exception as e:
print('cafile or capath invalid: %s' % e, file=sys.stderr)
sys.exit(1)

return context

print('Warning: Python %d.%d: cafile and capath ignored' %
(sys.version_info.major, sys.version_info.minor),
file=sys.stderr)
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
try:
context.load_verify_locations(cafile=cafile, capath=capath)
except Exception as e:
print('cafile or capath invalid: %s' % e, file=sys.stderr)
sys.exit(1)

return None
return context


def get_vsys(s):
Expand Down Expand Up @@ -828,23 +817,14 @@ def set_encoding():
#
encoding = 'utf-8'

if hasattr(sys.stdin, 'detach'):
# >= 3.1
import io

for s in ('stdin', 'stdout', 'stderr'):
line_buffering = getattr(sys, s).line_buffering
# print(s, line_buffering, file=sys.stderr)
setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(),
encoding=encoding,
line_buffering=line_buffering))

else:
import codecs
import io

sys.stdin = codecs.getreader(encoding)(sys.stdin)
sys.stdout = codecs.getwriter(encoding)(sys.stdout)
sys.stderr = codecs.getwriter(encoding)(sys.stderr)
for s in ('stdin', 'stdout', 'stderr'):
line_buffering = getattr(sys, s).line_buffering
# print(s, line_buffering, file=sys.stderr)
setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(),
encoding=encoding,
line_buffering=line_buffering))


def usage():
Expand Down
3 changes: 0 additions & 3 deletions doc/pan.wfapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ class pan.wfapi.PanWFapi()

The default is *None*.

SSL contexts are supported starting in Python versions 2.7.9
and 3.2.

exception pan.wfapi.PanWFapiError
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 1 addition & 6 deletions doc/pan.xapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,8 @@ class pan.xapi.PanXapi()
This can be used to specify the ``cafile``, ``capath`` and other SSL
configuration options.

SSL contexts are supported starting in Python versions 2.7.9
and 3.2.

Starting with Python versions 2.7.9 and 3.4.3 SSL server certificate
verification is performed by default as described in PEP 476.
Because many PAN-OS systems use a self-signed certificate, pan.xapi
will disable the default starting with these versions.
will disable the default server certificate verification.
**ssl_context** can be used to enable verification.

exception pan.xapi.PanXapiError
Expand Down
3 changes: 0 additions & 3 deletions doc/panwfapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ DESCRIPTION

This is the default.

SSL server certificate verification is only performed in Python
version 2.7.9 and 3.4.3 and greater.

``--ssl`` is ignored if ``--cafile`` or ``--capath`` are specified.

``--cafile`` *path*
Expand Down
6 changes: 2 additions & 4 deletions doc/panxapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,13 @@ DESCRIPTION
Specify the ``cafile`` value for HTTPS requests. ``cafile`` is a
file containing CA certificates to be used for SSL server
certificate verification. By default the SSL server certificate is
not verified. ``--cafile`` is supported starting in Python versions
2.7.9 and 3.2.
not verified.

``--capath`` *path*
Specify the ``capath`` value for HTTPS requests. ``capath`` is a
directory of hashed certificate files to be used for SSL server
certificate verification. By default the SSL server certificate is
not verified. ``--cafile`` is supported starting in Python versions
2.7.9 and 3.2.
not verified.

``--version``
Display version.
Expand Down
1 change: 0 additions & 1 deletion lib/pan/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
import sys
import logging

Expand Down
43 changes: 8 additions & 35 deletions lib/pan/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,10 @@
import requests
_using_requests = True
except ImportError:
try:
# 3.2
from urllib.request import Request, \
build_opener, HTTPErrorProcessor, HTTPSHandler
from urllib.error import URLError
from urllib.parse import urlencode
except ImportError:
# 2.7
from urllib2 import Request, URLError, \
build_opener, HTTPErrorProcessor, HTTPSHandler
from urllib import urlencode


def _isunicode(s):
try:
if isinstance(s, unicode):
return True
return False
except NameError:
if isinstance(s, str):
return True
return False
from urllib.request import Request, \
build_opener, HTTPErrorProcessor, HTTPSHandler
from urllib.error import URLError
from urllib.parse import urlencode


class PanHttpError(Exception):
Expand Down Expand Up @@ -119,8 +101,8 @@ def _http_request_urllib(self, url, headers, data, params):
x = set(k.lower() for k in headers)
if not 'content-type' in x:
kwargs['data'] = urlencode(data)
# data must by type 'bytes' for 3.x
if _isunicode(kwargs['data']):
# data must be type 'bytes'
if isinstance(kwargs['data'], str):
kwargs['data'] = kwargs['data'].encode()

request = Request(**kwargs)
Expand All @@ -131,11 +113,7 @@ def _http_request_urllib(self, url, headers, data, params):
if self.timeout is not None:
kwargs['timeout'] = self.timeout

if not self.verify_cert and \
(sys.version_info.major == 2 and
sys.hexversion >= 0x02070900 or
sys.version_info.major == 3 and
sys.hexversion >= 0x03040300):
if not self.verify_cert:
context = ssl._create_unverified_context()
kwargs['context'] = context

Expand All @@ -147,12 +125,7 @@ def _http_request_urllib(self, url, headers, data, params):
raise PanHttpError(str(e))

self.code = response.getcode()
if hasattr(response, 'reason'):
# 3.2
self.reason = response.reason
elif hasattr(response, 'msg'):
# 2.7
self.reason = response.msg
self.reason = response.reason

try:
self.headers = email.message_from_string(str(response.info()))
Expand Down
2 changes: 1 addition & 1 deletion lib/pan/licapi/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _api_request(self, url, headers, data=None, params=None):

try:
self.http.http_request(url=url,
headers=self.headers,
headers=headers,
data=data,
params=params)
except pan.http.PanHttpError as e:
Expand Down
1 change: 0 additions & 1 deletion lib/pan/rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function
import sys
import os
import re
Expand Down
Loading

0 comments on commit 5f81bf1

Please sign in to comment.