Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SG-38307 Remove Python 2 compatibility #1005

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions python/tank/authentication/app_session_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
# not expressly granted therein are reserved by Shotgun Software Inc.

import json
import http.client
import os
import platform
import random
import sys
import time
import urllib

import tank
from tank_vendor import six
from tank_vendor.six.moves import http_client, urllib

from . import errors
from .. import platform as sgtk_platform
Expand Down Expand Up @@ -62,9 +62,10 @@ def format(self):

def process(
sg_url,
browser_open_callback,
*,
http_proxy=None,
product=None,
browser_open_callback,
keep_waiting_callback=lambda: True,
):
sg_url = connection.sanitize_url(sg_url)
Expand Down Expand Up @@ -103,7 +104,7 @@ def process(

request = urllib.request.Request(
urllib.parse.urljoin(sg_url, "/internal_api/app_session_request"),
# method="POST", # see below
method="POST",
data=urllib.parse.urlencode(
{
"appName": product,
Expand All @@ -115,9 +116,6 @@ def process(
},
)

# Hook for Python 2
request.get_method = lambda: "POST"

response = http_request(url_opener, request)
logger.debug(
"Initial network request returned HTTP code {code}".format(
Expand All @@ -134,7 +132,7 @@ def process(
)

elif response_code_major == 4:
if response.code == http_client.FORBIDDEN and hasattr(response, "json"):
if response.code == http.client.FORBIDDEN and hasattr(response, "json"):
logger.debug(
"HTTP response Forbidden: {data}".format(data=response.json),
exc_info=getattr(response, "exception", None),
Expand All @@ -151,7 +149,7 @@ def process(
parent_exception=response.exception,
)

elif response.code != http_client.OK:
elif response.code != http.client.OK:
raise AuthenticationError(
"Unexpected response from the Flow Production Tracking site",
payload=getattr(response, "json", response),
Expand Down Expand Up @@ -201,6 +199,10 @@ def process(
"/internal_api/app_session_request/{session_id}".format(
session_id=session_id,
),
method="PUT",
headers={
"User-Agent": user_agent,
},
)

approved = False
Expand Down Expand Up @@ -264,7 +266,7 @@ def process(
exc_info=getattr(response, "exception", None),
)

if response.code == http_client.NOT_FOUND and hasattr(response, "json"):
if response.code == http.client.NOT_FOUND and hasattr(response, "json"):
raise AuthenticationError(
"The request has been rejected or has expired."
)
Expand All @@ -274,7 +276,7 @@ def process(
parent_exception=getattr(response, "exception", None),
)

elif response.code != http_client.OK:
elif response.code != http.client.OK:
logger.debug("Request denied: http code is: {code}".format(
code=response.code,
))
Expand Down Expand Up @@ -355,14 +357,6 @@ def get_product_name():
return PRODUCT_DEFAULT


def _get_content_type(headers):
if six.PY2:
value = headers.get("content-type", "text/plain")
return value.split(";", 1)[0].lower()
else:
return headers.get_content_type()


def http_request(opener, req, max_attempts=4):
attempt = 0
backoff = 0.75 # Seconds to wait before retry, times the attempt number
Expand Down Expand Up @@ -422,7 +416,7 @@ def http_request(opener, req, max_attempts=4):
parent_exception=exc,
)

if _get_content_type(response.headers) == "application/json":
if response.headers.get_content_type() == "application/json":
try:
response.json = json.load(response)
except json.JSONDecodeError as exc:
Expand Down
2 changes: 1 addition & 1 deletion python/tank/authentication/console_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _authenticate_app_session_launcher(self, hostname, login, http_proxy):
print()
session_info = app_session_launcher.process(
hostname,
webbrowser.open, # browser_open_callback
browser_open_callback=webbrowser.open,
http_proxy=http_proxy,
)

Expand Down
2 changes: 1 addition & 1 deletion python/tank/authentication/login_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ def run(self):
try:
self.session_info = app_session_launcher.process(
self._sg_url,
lambda u: QtGui.QDesktopServices.openUrl(u), # browser_open_callback
browser_open_callback=lambda u: QtGui.QDesktopServices.openUrl(u),
http_proxy=self._http_proxy,
product=self._product,
keep_waiting_callback=self.should_continue,
Expand Down
49 changes: 27 additions & 22 deletions tests/authentication_tests/test_app_session_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ def test_process_parameters(self):
with self.assertRaises(AssertionError):
app_session_launcher.process(
"https://test.shotgunstudio.com",
None, # browser_open_callback
)

with self.assertRaises(AssertionError):
app_session_launcher.process(
"https://test.shotgunstudio.com",
"Test", # browser_open_callback
browser_open_callback=None,
)

with self.assertRaises(AssertionError):
app_session_launcher.process(
"https://test.shotgunstudio.com",
lambda: True, # browser_open_callback
browser_open_callback="Test",
)

with self.assertRaises(AssertionError):
app_session_launcher.process(
"https://test.shotgunstudio.com",
browser_open_callback=lambda: True,
keep_waiting_callback=None,
)

Expand Down Expand Up @@ -165,7 +170,7 @@ def url_opener(url):
self.assertEqual(
app_session_launcher.process(
self.api_url,
url_opener, # browser_open_callback
browser_open_callback=url_opener,
http_proxy="{fqdn}:{port}".format( # For code coverage
fqdn=self.httpd.server_address[0],
port=self.httpd.server_address[1],
Expand All @@ -188,7 +193,7 @@ def test_not_reachable(self, *mocks):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertIsInstance(
Expand Down Expand Up @@ -251,7 +256,7 @@ def api_put_handler(request):
self.assertEqual(
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
),
(self.api_url, "john", "to123", None),
)
Expand All @@ -262,7 +267,7 @@ def test_post_request(self, *mocks):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -276,7 +281,7 @@ def test_post_request(self, *mocks):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -292,7 +297,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertIsInstance(
Expand All @@ -311,7 +316,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -325,7 +330,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -340,7 +345,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -356,7 +361,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -372,7 +377,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -388,7 +393,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -406,7 +411,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -426,7 +431,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
)

self.assertEqual(
Expand All @@ -445,7 +450,7 @@ def test_browser_error(self):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: False, # browser_open_callback
browser_open_callback=lambda url: False,
)

self.assertEqual(
Expand All @@ -470,7 +475,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
product="app_2a37c59",
keep_waiting_callback=lambda: False,
)
Expand Down Expand Up @@ -506,7 +511,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
url_opener, # browser_open_callback
browser_open_callback=url_opener,
)

self.assertIsInstance(
Expand All @@ -531,7 +536,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
keep_waiting_callback=lambda: False, # Avoid 5 minute timeout
)

Expand All @@ -544,7 +549,7 @@ def api_handler1(request):
with self.assertRaises(app_session_launcher.AuthenticationError) as cm:
app_session_launcher.process(
self.api_url,
lambda url: True, # browser_open_callback
browser_open_callback=lambda url: True,
keep_waiting_callback=lambda: False, # Avoid 5 minute timeout
)

Expand Down
Loading