From 0aabd63461433b590f709544b41792d4e2f23c11 Mon Sep 17 00:00:00 2001 From: Viktor Samun Date: Wed, 30 Jun 2021 22:17:51 +0000 Subject: [PATCH] Fix python2/3 compatibility in DownloadUsingHttp function Change-Id: I59ed965d066b7875c1739408054498b328652be5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2988315 Commit-Queue: Dirk Pranke Reviewed-by: Dirk Pranke --- win_toolchain/get_toolchain_if_necessary.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/win_toolchain/get_toolchain_if_necessary.py b/win_toolchain/get_toolchain_if_necessary.py index b6c479ed0..328f79ba6 100755 --- a/win_toolchain/get_toolchain_if_necessary.py +++ b/win_toolchain/get_toolchain_if_necessary.py @@ -21,6 +21,7 @@ won't be properly maintained. See http://crbug.com/323300. from __future__ import print_function import argparse +from contextlib import closing import hashlib import filecmp import json @@ -31,6 +32,13 @@ import subprocess import sys import tempfile import time +if sys.version_info[0] < 3: + from urllib2 import urlopen, URLError + from urlparse import urljoin +else: + from urllib.request import urlopen + from urllib.parse import urljoin + from urllib.error import URLError import zipfile # Environment variable that, if set, specifies the default Visual Studio @@ -310,19 +318,16 @@ def DelayBeforeRemoving(target_dir): def DownloadUsingHttp(filename): """Downloads the given file from a url defined in DEPOT_TOOLS_WIN_TOOLCHAIN_BASE_URL environment variable.""" - import urlparse - import urllib2 - from contextlib import closing temp_dir = tempfile.mkdtemp() assert os.path.basename(filename) == filename target_path = os.path.join(temp_dir, filename) base_url = ToolchainBaseURL() - src_url = urlparse.urljoin(base_url, filename) + src_url = urljoin(base_url, filename) try: - with closing(urllib2.urlopen(src_url)) as fsrc, \ + with closing(urlopen(src_url)) as fsrc, \ open(target_path, 'wb') as fdst: shutil.copyfileobj(fsrc, fdst) - except urllib2.URLError as e: + except URLError as e: RmDir(temp_dir) sys.exit('Failed to retrieve file: %s' % e) return temp_dir, target_path