Skip to content

Commit

Permalink
{Misc} Remove various Python 2.7 ImportError (#30451)
Browse files Browse the repository at this point in the history
  • Loading branch information
atombrella authored Dec 4, 2024
1 parent 731bd8d commit f92f723
Show file tree
Hide file tree
Showing 25 changed files with 37 additions and 135 deletions.
14 changes: 1 addition & 13 deletions scripts/curl_install_pypi/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,7 @@
import shutil
import subprocess
import hashlib
try:
# Attempt to load python 3 module
from urllib.request import urlopen
except ImportError:
# Import python 2 version
from urllib2 import urlopen

try:
# Rename raw_input to input to support Python 2
input = raw_input
except NameError:
# Python 3 doesn't have raw_input
pass
from urllib.request import urlopen

AZ_DISPATCH_TEMPLATE = """#!/usr/bin/env bash
{install_dir}/bin/python -m azure.cli "$@"
Expand Down
7 changes: 1 addition & 6 deletions src/azure-cli-core/azure/cli/core/aaz/_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

# pylint: disable=too-few-public-methods, protected-access, too-many-nested-blocks, too-many-branches

from urllib.parse import parse_qs, urljoin, urlparse, quote
import json

from azure.core.exceptions import ClientAuthenticationError, ResourceExistsError, ResourceNotFoundError, \
HttpResponseError
from azure.cli.core.azclierror import InvalidArgumentValueError
from urllib.parse import parse_qs, urljoin, urlparse

from ._arg_browser import AAZArgBrowser
from ._base import AAZUndefined, AAZBaseValue, AAZBaseType, has_value
Expand All @@ -19,11 +19,6 @@
from ._field_value import AAZList, AAZObject, AAZBaseDictValue
from .exceptions import AAZInvalidValueError

try:
from urllib import quote # type: ignore
except ImportError:
from urllib.parse import quote # type: ignore


class AAZOperation:

Expand Down
6 changes: 1 addition & 5 deletions src/azure-cli-core/azure/cli/core/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ def is_valid_ssh_rsa_public_key(openssh_pubkey):
# http://stackoverflow.com/questions/2494450/ssh-rsa-public-key-validation-using-a-regular-expression # pylint: disable=line-too-long
# A "good enough" check is to see if the key starts with the correct header.
import struct
try:
from base64 import decodebytes as base64_decode
except ImportError:
# deprecated and redirected to decodebytes in Python 3
from base64 import decodestring as base64_decode
from base64 import decodebytes as base64_decode

parts = openssh_pubkey.split()
if len(parts) < 2:
Expand Down
7 changes: 3 additions & 4 deletions src/azure-cli-telemetry/azure/cli/telemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ def _start(config_dir, cache_dir):
startupinfo.wShowWindow = subprocess.SW_HIDE
kwargs['startupinfo'] = startupinfo
else:
if sys.version_info >= (3, 3):
kwargs['stdin'] = subprocess.DEVNULL
kwargs['stdout'] = subprocess.DEVNULL
kwargs['stderr'] = subprocess.STDOUT
kwargs['stdin'] = subprocess.DEVNULL
kwargs['stdout'] = subprocess.DEVNULL
kwargs['stderr'] = subprocess.STDOUT

process = subprocess.Popen(**kwargs)
logger.info('Return from creating process %s', process.pid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
import json
import datetime

import urllib.request as http_client_t
from urllib.error import HTTPError

from applicationinsights import TelemetryClient
from applicationinsights.channel import SynchronousSender, SynchronousQueue, TelemetryChannel

try:
# Python 2.x
import urllib2 as http_client_t
from urllib2 import HTTPError
except ImportError:
# Python 3.x
import urllib.request as http_client_t
from urllib.error import HTTPError


class CliTelemetryClient:
def __init__(self, batch=100, sender=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
import os
import unittest

import urllib.request as http_client_t
from urllib.error import HTTPError

from applicationinsights.channel import SynchronousSender

try:
# Python 2.x
import urllib2 as http_client_t
from urllib2 import HTTPError
except ImportError:
# Python 3.x
import urllib.request as http_client_t
from urllib.error import HTTPError

from azure.cli.telemetry.components.telemetry_client import (CliTelemetryClient, _NoRetrySender, http_client_t)
from azure.cli.telemetry.components.telemetry_client import CliTelemetryClient, _NoRetrySender

TEST_RESOURCE_FOLDER = os.path.join(os.path.dirname(__file__), 'resources')

Expand Down
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/acr/_docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
from urllib.parse import urlencode, urlparse, urlunparse
except ImportError:
from urllib import urlencode
from urllib.parse import urlencode, urlparse, urlunparse

import time
from json import loads
Expand Down
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/acr/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

from datetime import datetime, timedelta

try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
from urllib.parse import unquote

from knack.log import get_logger
from azure.cli.core.util import user_confirmation
Expand Down
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/acr/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
from urllib.parse import unquote

from knack.util import CLIError
from knack.log import get_logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
from urllib.parse import urlencode
import json
import unittest
from unittest import mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
from urllib.parse import urlencode
import json
import unittest
from unittest import mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import requests
import urllib3

try:
# Try importing Python 3 urllib.parse
from urllib.parse import quote
except ImportError:
# If urllib.parse was not imported, use Python 2 module urlparse
from urllib import quote # pylint: disable=import-error
from urllib.parse import quote

from knack.util import CLIError
from azure.cli.command_modules.botservice.http_response_validator import HttpResponseValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from urllib.parse import urlsplit

from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.web import WebSiteManagementClient
from azure.mgmt.web.models import HostType

try:
# Try importing Python 3 urllib.parse
from urllib.parse import urlsplit
except ImportError:
# If urllib.parse was not imported, use Python 2 module urlparse
from urlparse import urlsplit # pylint: disable=import-error


class WebAppOperations:
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from hashlib import sha256
from hmac import HMAC
from time import time
try:
from urllib import (urlencode, quote)
except ImportError:
from urllib.parse import (urlencode, quote) # pylint: disable=import-error
from urllib.parse import urlencode, quote
from msrest.authentication import Authentication


Expand Down
6 changes: 1 addition & 5 deletions src/azure-cli/azure/cli/command_modules/lab/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,7 @@ def _is_valid_ssh_rsa_public_key(openssh_pubkey):
# http://stackoverflow.com/questions/2494450/ssh-rsa-public-key-validation-using-a-regular-expression
# A "good enough" check is to see if the key starts with the correct header.
import struct
try:
from base64 import decodebytes as base64_decode
except ImportError:
# deprecated and redirected to decodebytes in Python 3
from base64 import decodestring as base64_decode
from base64 import decodebytes as base64_decode
parts = openssh_pubkey.split()
if len(parts) < 2:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import os
import re
import argparse
from urllib.parse import urlparse, urlsplit

from azure.cli.core.azclierror import ArgumentUsageError

from knack.util import CLIError
try:
from urllib.parse import urlparse, urlsplit
except ImportError:
from urlparse import urlparse, urlsplit # pylint: disable=import-error

MSI_LOCAL_ID = '[system]'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
import os
import time

from urllib.parse import urlparse

from OpenSSL import crypto
from cryptography.hazmat.primitives.serialization import pkcs12
from cryptography.hazmat.primitives import hashes

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error

from azure.cli.core.util import CLIError, get_file_json, b64_to_hex, sdk_no_wait
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.profiles import ResourceType, get_sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


import os
import platform
import subprocess
import datetime
import sys
import zipfile
import stat
from urllib.parse import urlparse
Expand Down Expand Up @@ -224,12 +222,7 @@ def _urlretrieve(url, install_location):
req = urlopen(url)
compressedFile = io.BytesIO(req.read())
if url.endswith('zip'):
if sys.version_info.major >= 3:
zip_file = zipfile.ZipFile(compressedFile)
else:
# If Python version is 2.X, use StringIO instead.
import StringIO # pylint: disable=import-error
zip_file = zipfile.ZipFile(StringIO.StringIO(req.read()))
zip_file = zipfile.ZipFile(compressedFile)
for fileName in zip_file.namelist():
if fileName.endswith('azcopy') or fileName.endswith('azcopy.exe'):
with open(install_location, 'wb') as f:
Expand Down
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/vm/_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

import requests

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
from urllib.parse import urlparse

from knack.util import CLIError
from knack.log import get_logger
Expand Down
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/vm/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

import os

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
from urllib.parse import urlparse

from knack.log import get_logger
from knack.util import CLIError
Expand Down
7 changes: 2 additions & 5 deletions src/azure-cli/azure/cli/command_modules/vm/_vm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import re
import importlib

from azure.cli.core.commands.arm import ArmTemplateBuilder
from urllib.parse import urlparse

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
from azure.cli.core.commands.arm import ArmTemplateBuilder

from knack.log import get_logger
from knack.util import CLIError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
import traceback
from enum import Enum

import requests
from urllib.parse import urlparse

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
import requests

from knack.util import CLIError
from knack.log import get_logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

import os

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
from urllib.parse import urlparse

from knack.log import get_logger
from knack.util import CLIError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

from azure.cli.core.commands.arm import ArmTemplateBuilder

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # pylint: disable=import-error
from urllib.parse import urlparse

from knack.log import get_logger
from knack.util import CLIError
Expand Down
5 changes: 1 addition & 4 deletions tools/automation/utilities/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
import xmlrpclib
except ImportError:
import xmlrpc.client as xmlrpclib # pylint: disable=import-error
import xmlrpc.client as xmlrpclib # pylint: disable=import-error


def is_available_on_pypi(module_name, module_version):
Expand Down

0 comments on commit f92f723

Please sign in to comment.