tools: make nodedownload module compatible with Python 3.14

FancyURLopener and URLopener have been deprecated since
Python 3.3 and they are removed completely from 3.14.

Fixes: https://github.com/nodejs/node/issues/58740
PR-URL: https://github.com/nodejs/node/pull/58752
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Lumír 'Frenzy' Balhar 2025-06-22 07:49:14 +02:00 committed by GitHub
parent eafbe277b0
commit dfcb824ae3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,10 +7,7 @@ import sys
import zipfile
import tarfile
import contextlib
try:
from urllib.request import FancyURLopener, URLopener
except ImportError:
from urllib import FancyURLopener, URLopener
from urllib.request import build_opener, install_opener, urlretrieve
def formatSize(amt):
"""Format a size as a string in MB"""
@ -21,11 +18,6 @@ def spin(c):
spin = ".:|'"
return (spin[c % len(spin)])
class ConfigOpener(FancyURLopener):
"""fancy opener used by retrievefile. Set a UA"""
# append to existing version (UA)
version = '%s node.js/configure' % URLopener.version
def reporthook(count, size, total):
"""internal hook used by retrievefile"""
sys.stdout.write(' Fetch: %c %sMB total, %sMB downloaded \r' %
@ -38,7 +30,10 @@ def retrievefile(url, targetfile):
try:
sys.stdout.write(' <%s>\nConnecting...\r' % url)
sys.stdout.flush()
ConfigOpener().retrieve(url, targetfile, reporthook=reporthook)
opener = build_opener()
opener.addheaders = [('User-agent', f'Python-urllib/{sys.version_info.major}.{sys.version_info.minor} node.js/configure')]
install_opener(opener)
urlretrieve(url, targetfile, reporthook=reporthook)
print('') # clear the line
return targetfile
except IOError as err: