Skip to content

Commit 30af5ce

Browse files
VoltrexKeyvatargos
authored andcommitted
build: use pathlib for paths
Use Python's `pathlib` library for paths and related operations instead of `os.path`. Refs: #47323 (comment) #47323 (comment) PR-URL: #47581 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Christian Clauss <cclauss@me.com>
1 parent 4c26e28 commit 30af5ce

File tree

1 file changed

+54
-56
lines changed

1 file changed

+54
-56
lines changed

configure.py

+54-56
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import shutil
1313
import bz2
1414
import io
15+
from pathlib import Path
1516

1617
from distutils.version import StrictVersion
1718

1819
# If not run from node/, cd to node/.
19-
os.chdir(os.path.dirname(__file__) or '.')
20+
os.chdir(Path(__file__).parent)
2021

2122
original_argv = sys.argv[1:]
2223

@@ -25,11 +26,13 @@
2526
CC = os.environ.get('CC', 'cc' if sys.platform == 'darwin' else 'gcc')
2627
CXX = os.environ.get('CXX', 'c++' if sys.platform == 'darwin' else 'g++')
2728

28-
sys.path.insert(0, os.path.join('tools', 'gyp', 'pylib'))
29+
tools_path = Path('tools')
30+
31+
sys.path.insert(0, str(tools_path / 'gyp' / 'pylib'))
2932
from gyp.common import GetFlavor
3033

3134
# imports in tools/configure.d
32-
sys.path.insert(0, os.path.join('tools', 'configure.d'))
35+
sys.path.insert(0, str(tools_path / 'configure.d'))
3336
import nodedownload
3437

3538
# imports in tools/
@@ -53,8 +56,7 @@
5356
valid_mips_fpu = ('fp32', 'fp64', 'fpxx')
5457
valid_mips_float_abi = ('soft', 'hard')
5558
valid_intl_modes = ('none', 'small-icu', 'full-icu', 'system-icu')
56-
with open('tools/icu/icu_versions.json', encoding='utf-8') as f:
57-
icu_versions = json.load(f)
59+
icu_versions = json.loads((tools_path / 'icu' / 'icu_versions.json').read_text(encoding='utf-8'))
5860

5961
shareable_builtins = {'cjs_module_lexer/lexer': 'deps/cjs-module-lexer/lexer.js',
6062
'cjs_module_lexer/dist/lexer': 'deps/cjs-module-lexer/dist/lexer.js',
@@ -839,7 +841,7 @@
839841
(options, args) = parser.parse_known_args()
840842

841843
# Expand ~ in the install prefix now, it gets written to multiple files.
842-
options.prefix = os.path.expanduser(options.prefix or '')
844+
options.prefix = str(Path(options.prefix or '').expanduser())
843845

844846
# set up auto-download list
845847
auto_downloads = nodedownload.parse(options.download_list)
@@ -1199,7 +1201,7 @@ def configure_zos(o):
11991201
o['variables']['node_static_zoslib'] = b(True)
12001202
if options.static_zoslib_gyp:
12011203
# Apply to all Node.js components for now
1202-
o['variables']['zoslib_include_dir'] = os.path.dirname(options.static_zoslib_gyp) + '/include'
1204+
o['variables']['zoslib_include_dir'] = Path(options.static_zoslib_gyp).parent + '/include'
12031205
o['include_dirs'] += [o['variables']['zoslib_include_dir']]
12041206
else:
12051207
raise Exception('--static-zoslib-gyp=<path to zoslib.gyp file> is required.')
@@ -1603,7 +1605,7 @@ def configure_static(o):
16031605

16041606
def write(filename, data):
16051607
print_verbose(f'creating {filename}')
1606-
with open(filename, 'w+', encoding='utf-8') as f:
1608+
with Path(filename).open(mode='w+', encoding='utf-8') as f:
16071609
f.write(data)
16081610

16091611
do_not_edit = '# Do not edit. Generated by the configure script.\n'
@@ -1619,8 +1621,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16191621
# srcfile uses "slash" as dir separator as its output is consumed by gyp
16201622
srcfile = f'{dir_sub}/{file}'
16211623
if patch_dir:
1622-
patchfile = f'{dir_base}{patch_dir}{file}'
1623-
if os.path.isfile(patchfile):
1624+
patchfile = Path(dir_base, patch_dir, file)
1625+
if patchfile.is_file():
16241626
srcfile = f'{patch_dir}/{file}'
16251627
info(f'Using floating patch "{patchfile}" from "{dir_base}"')
16261628
file_list.append(srcfile)
@@ -1629,9 +1631,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16291631

16301632
def configure_intl(o):
16311633
def icu_download(path):
1632-
depFile = 'tools/icu/current_ver.dep'
1633-
with open(depFile, encoding='utf-8') as f:
1634-
icus = json.load(f)
1634+
depFile = tools_path / 'icu' / 'current_ver.dep'
1635+
icus = json.loads(depFile.read_text(encoding='utf-8'))
16351636
# download ICU, if needed
16361637
if not os.access(options.download_path, os.W_OK):
16371638
error('''Cannot write to desired download path.
@@ -1646,13 +1647,13 @@ def icu_download(path):
16461647
For the entry {url},
16471648
Expected one of these keys: {' '.join(allAlgos)}''')
16481649
local = url.split('/')[-1]
1649-
targetfile = os.path.join(options.download_path, local)
1650-
if not os.path.isfile(targetfile):
1650+
targetfile = Path(options.download_path, local)
1651+
if not targetfile.is_file():
16511652
if attemptdownload:
16521653
nodedownload.retrievefile(url, targetfile)
16531654
else:
16541655
print(f'Re-using existing {targetfile}')
1655-
if os.path.isfile(targetfile):
1656+
if targetfile.is_file():
16561657
print(f'Checking file integrity with {hashAlgo}:\r')
16571658
gotHash = nodedownload.checkHash(targetfile, hashAlgo)
16581659
print(f'{hashAlgo}: {gotHash} {targetfile}')
@@ -1739,14 +1740,15 @@ def icu_download(path):
17391740
icu_full_path = icu_deps_path
17401741

17411742
# icu-tmp is used to download and unpack the ICU tarball.
1742-
icu_tmp_path = os.path.join(icu_parent_path, 'icu-tmp')
1743+
icu_tmp_path = Path(icu_parent_path, 'icu-tmp')
17431744

17441745
# canned ICU. see tools/icu/README.md to update.
17451746
canned_icu_dir = 'deps/icu-small'
17461747

17471748
# use the README to verify what the canned ICU is
1748-
canned_is_full = os.path.isfile(os.path.join(canned_icu_dir, 'README-FULL-ICU.txt'))
1749-
canned_is_small = os.path.isfile(os.path.join(canned_icu_dir, 'README-SMALL-ICU.txt'))
1749+
canned_icu_path = Path(canned_icu_dir)
1750+
canned_is_full = (canned_icu_path / 'README-FULL-ICU.txt').is_file()
1751+
canned_is_small = (canned_icu_path / 'README-SMALL-ICU.txt').is_file()
17501752
if canned_is_small:
17511753
warn(f'Ignoring {canned_icu_dir} - in-repo small icu is no longer supported.')
17521754

@@ -1766,39 +1768,39 @@ def icu_download(path):
17661768
icu_config['variables']['icu_full_canned'] = 1
17671769
# --with-icu-source processing
17681770
# now, check that they didn't pass --with-icu-source=deps/icu
1769-
elif with_icu_source and os.path.abspath(icu_full_path) == os.path.abspath(with_icu_source):
1771+
elif with_icu_source and Path(icu_full_path).resolve() == Path(with_icu_source).resolve():
17701772
warn(f'Ignoring redundant --with-icu-source={with_icu_source}')
17711773
with_icu_source = None
17721774
# if with_icu_source is still set, try to use it.
17731775
if with_icu_source:
1774-
if os.path.isdir(icu_full_path):
1776+
if Path(icu_full_path).is_dir():
17751777
print(f'Deleting old ICU source: {icu_full_path}')
17761778
shutil.rmtree(icu_full_path)
17771779
# now, what path was given?
1778-
if os.path.isdir(with_icu_source):
1780+
if Path(with_icu_source).is_dir():
17791781
# it's a path. Copy it.
17801782
print(f'{with_icu_source} -> {icu_full_path}')
17811783
shutil.copytree(with_icu_source, icu_full_path)
17821784
else:
17831785
# could be file or URL.
17841786
# Set up temporary area
1785-
if os.path.isdir(icu_tmp_path):
1787+
if Path(icu_tmp_path).is_dir():
17861788
shutil.rmtree(icu_tmp_path)
1787-
os.mkdir(icu_tmp_path)
1789+
icu_tmp_path.mkdir()
17881790
icu_tarball = None
1789-
if os.path.isfile(with_icu_source):
1791+
if Path(with_icu_source).is_file():
17901792
# it's a file. Try to unpack it.
17911793
icu_tarball = with_icu_source
17921794
else:
17931795
# Can we download it?
1794-
local = os.path.join(icu_tmp_path, with_icu_source.split('/')[-1]) # local part
1796+
local = icu_tmp_path / with_icu_source.split('/')[-1] # local part
17951797
icu_tarball = nodedownload.retrievefile(with_icu_source, local)
17961798
# continue with "icu_tarball"
17971799
nodedownload.unpack(icu_tarball, icu_tmp_path)
17981800
# Did it unpack correctly? Should contain 'icu'
1799-
tmp_icu = os.path.join(icu_tmp_path, 'icu')
1800-
if os.path.isdir(tmp_icu):
1801-
os.rename(tmp_icu, icu_full_path)
1801+
tmp_icu = icu_tmp_path / 'icu'
1802+
if tmp_icu.is_dir():
1803+
tmp_icu.rename(icu_full_path)
18021804
shutil.rmtree(icu_tmp_path)
18031805
else:
18041806
shutil.rmtree(icu_tmp_path)
@@ -1808,22 +1810,22 @@ def icu_download(path):
18081810
o['variables']['icu_gyp_path'] = 'tools/icu/icu-generic.gyp'
18091811
# ICU source dir relative to tools/icu (for .gyp file)
18101812
o['variables']['icu_path'] = icu_full_path
1811-
if not os.path.isdir(icu_full_path):
1813+
if not Path(icu_full_path).is_dir():
18121814
# can we download (or find) a zipfile?
18131815
localzip = icu_download(icu_full_path)
18141816
if localzip:
18151817
nodedownload.unpack(localzip, icu_parent_path)
18161818
else:
1817-
warn("* ECMA-402 (Intl) support didn't find ICU in {icu_full_path}..")
1818-
if not os.path.isdir(icu_full_path):
1819+
warn(f"* ECMA-402 (Intl) support didn't find ICU in {icu_full_path}..")
1820+
if not Path(icu_full_path).is_dir():
18191821
error(f'''Cannot build Intl without ICU in {icu_full_path}.
18201822
Fix, or disable with "--with-intl=none"''')
18211823
else:
18221824
print_verbose(f'* Using ICU in {icu_full_path}')
18231825
# Now, what version of ICU is it? We just need the "major", such as 54.
18241826
# uvernum.h contains it as a #define.
1825-
uvernum_h = os.path.join(icu_full_path, 'source/common/unicode/uvernum.h')
1826-
if not os.path.isfile(uvernum_h):
1827+
uvernum_h = Path(icu_full_path, 'source', 'common', 'unicode', 'uvernum.h')
1828+
if not uvernum_h.is_file():
18271829
error(f'Could not load {uvernum_h} - is ICU installed?')
18281830
icu_ver_major = None
18291831
matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
@@ -1843,17 +1845,15 @@ def icu_download(path):
18431845
icu_data_file_l = f'icudt{icu_ver_major}l.dat' # LE filename
18441846
icu_data_file = f'icudt{icu_ver_major}{icu_endianness}.dat'
18451847
# relative to configure
1846-
icu_data_path = os.path.join(icu_full_path,
1847-
'source/data/in',
1848-
icu_data_file_l) # LE
1848+
icu_data_path = Path(icu_full_path, 'source', 'data', 'in', icu_data_file_l) # LE
18491849
compressed_data = f'{icu_data_path}.bz2'
1850-
if not os.path.isfile(icu_data_path) and os.path.isfile(compressed_data):
1850+
if not icu_data_path.is_file() and Path(compressed_data).is_file():
18511851
# unpack. deps/icu is a temporary path
1852-
if os.path.isdir(icu_tmp_path):
1852+
if icu_tmp_path.is_dir():
18531853
shutil.rmtree(icu_tmp_path)
1854-
os.mkdir(icu_tmp_path)
1855-
icu_data_path = os.path.join(icu_tmp_path, icu_data_file_l)
1856-
with open(icu_data_path, 'wb') as outf:
1854+
icu_tmp_path.mkdir()
1855+
icu_data_path = icu_tmp_path / icu_data_file_l
1856+
with icu_data_path.open(mode='wb') as outf:
18571857
inf = bz2.BZ2File(compressed_data, 'rb')
18581858
try:
18591859
shutil.copyfileobj(inf, outf)
@@ -1862,20 +1862,18 @@ def icu_download(path):
18621862
# Now, proceed..
18631863

18641864
# relative to dep..
1865-
icu_data_in = os.path.join('..', '..', icu_data_path)
1866-
if not os.path.isfile(icu_data_path) and icu_endianness != 'l':
1865+
icu_data_in = Path('..', '..', icu_data_path)
1866+
if not icu_data_path.is_file() and icu_endianness != 'l':
18671867
# use host endianness
1868-
icu_data_path = os.path.join(icu_full_path,
1869-
'source/data/in',
1870-
icu_data_file) # will be generated
1871-
if not os.path.isfile(icu_data_path):
1868+
icu_data_path = Path(icu_full_path, 'source', 'data', 'in', icu_data_file) # will be generated
1869+
if not icu_data_path.is_file():
18721870
# .. and we're not about to build it from .gyp!
18731871
error(f'''ICU prebuilt data file {icu_data_path} does not exist.
18741872
See the README.md.''')
18751873

18761874
# this is the input '.dat' file to use .. icudt*.dat
18771875
# may be little-endian if from a icu-project.org tarball
1878-
o['variables']['icu_data_in'] = icu_data_in
1876+
o['variables']['icu_data_in'] = str(icu_data_in)
18791877

18801878
# map from variable name to subdirs
18811879
icu_src = {
@@ -1973,16 +1971,16 @@ def make_bin_override():
19731971
os.path.realpath(which_python) == os.path.realpath(sys.executable)):
19741972
return
19751973

1976-
bin_override = os.path.abspath('out/tools/bin')
1974+
bin_override = Path('out', 'tools', 'bin').resolve()
19771975
try:
1978-
os.makedirs(bin_override)
1976+
bin_override.mkdir(parents=True)
19791977
except OSError as e:
19801978
if e.errno != errno.EEXIST:
19811979
raise e
19821980

1983-
python_link = os.path.join(bin_override, 'python')
1981+
python_link = bin_override / 'python'
19841982
try:
1985-
os.unlink(python_link)
1983+
python_link.unlink()
19861984
except OSError as e:
19871985
if e.errno != errno.ENOENT:
19881986
raise e
@@ -1991,7 +1989,7 @@ def make_bin_override():
19911989
# We need to set the environment right now so that when gyp (in run_gyp)
19921990
# shells out, it finds the right python (specifically at
19931991
# https://github.com/nodejs/node/blob/d82e107/deps/v8/gypfiles/toolchain.gypi#L43)
1994-
os.environ['PATH'] = bin_override + ':' + os.environ['PATH']
1992+
os.environ['PATH'] = str(bin_override) + ':' + os.environ['PATH']
19951993

19961994
return bin_override
19971995

@@ -2070,7 +2068,7 @@ def make_bin_override():
20702068

20712069
write('config.status', '#!/bin/sh\nset -x\nexec ./configure ' +
20722070
' '.join([shlex.quote(arg) for arg in original_argv]) + '\n')
2073-
os.chmod('config.status', 0o775)
2071+
Path('config.status').chmod(0o775)
20742072

20752073

20762074
config = {
@@ -2100,7 +2098,7 @@ def make_bin_override():
21002098
# On Windows there's no reason to search for a different python binary.
21012099
bin_override = None if sys.platform == 'win32' else make_bin_override()
21022100
if bin_override:
2103-
config_str = 'export PATH:=' + bin_override + ':$(PATH)\n' + config_str
2101+
config_str = 'export PATH:=' + str(bin_override) + ':$(PATH)\n' + config_str
21042102

21052103
write('config.mk', do_not_edit + config_str)
21062104

0 commit comments

Comments
 (0)