Skip to content

Commit 5ca437b

Browse files
VoltrexKeyvaMoLow
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 a9e8a20 commit 5ca437b

1 file changed

Lines changed: 54 additions & 56 deletions

File tree

‎configure.py‎

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
importshutil
1313
importbz2
1414
importio
15+
frompathlibimportPath
1516

1617
fromdistutils.versionimportStrictVersion
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'ifsys.platform=='darwin'else'gcc')
2627
CXX=os.environ.get('CXX', 'c++'ifsys.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
fromgyp.commonimportGetFlavor
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
importnodedownload
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-
withopen('tools/icu/icu_versions.json', encoding='utf-8') asf:
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',
@@ -868,7 +870,7 @@
868870
(options, args) =parser.parse_known_args()
869871

870872
# Expand ~ in the install prefix now, it gets written to multiple files.
871-
options.prefix=os.path.expanduser(options.prefixor'')
873+
options.prefix=str(Path(options.prefixor'').expanduser())
872874

873875
# set up auto-download list
874876
auto_downloads=nodedownload.parse(options.download_list)
@@ -1228,7 +1230,7 @@ def configure_zos(o):
12281230
o['variables']['node_static_zoslib'] =b(True)
12291231
ifoptions.static_zoslib_gyp:
12301232
# Apply to all Node.js components for now
1231-
o['variables']['zoslib_include_dir'] =os.path.dirname(options.static_zoslib_gyp) +'/include'
1233+
o['variables']['zoslib_include_dir'] =Path(options.static_zoslib_gyp).parent+'/include'
12321234
o['include_dirs'] += [o['variables']['zoslib_include_dir']]
12331235
else:
12341236
raiseException('--static-zoslib-gyp=<path to zoslib.gyp file> is required.')
@@ -1656,7 +1658,7 @@ def configure_static(o):
16561658

16571659
defwrite(filename, data):
16581660
print_verbose(f'creating {filename}')
1659-
withopen(filename, 'w+', encoding='utf-8') asf:
1661+
withPath(filename).open(mode='w+', encoding='utf-8') asf:
16601662
f.write(data)
16611663

16621664
do_not_edit='# Do not edit. Generated by the configure script.\n'
@@ -1672,8 +1674,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16721674
# srcfile uses "slash" as dir separator as its output is consumed by gyp
16731675
srcfile=f'{dir_sub}/{file}'
16741676
ifpatch_dir:
1675-
patchfile=f'{dir_base}{patch_dir}{file}'
1676-
ifos.path.isfile(patchfile):
1677+
patchfile=Path(dir_base, patch_dir, file)
1678+
ifpatchfile.is_file():
16771679
srcfile=f'{patch_dir}/{file}'
16781680
info(f'Using floating patch "{patchfile}" from "{dir_base}"')
16791681
file_list.append(srcfile)
@@ -1682,9 +1684,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16821684

16831685
defconfigure_intl(o):
16841686
deficu_download(path):
1685-
depFile='tools/icu/current_ver.dep'
1686-
withopen(depFile, encoding='utf-8') asf:
1687-
icus=json.load(f)
1687+
depFile=tools_path/'icu'/'current_ver.dep'
1688+
icus=json.loads(depFile.read_text(encoding='utf-8'))
16881689
# download ICU, if needed
16891690
ifnotos.access(options.download_path, os.W_OK):
16901691
error('''Cannot write to desired download path.
@@ -1699,13 +1700,13 @@ def icu_download(path):
16991700
For the entry {url},
17001701
Expected one of these keys: {' '.join(allAlgos)}''')
17011702
local=url.split('/')[-1]
1702-
targetfile=os.path.join(options.download_path, local)
1703-
ifnotos.path.isfile(targetfile):
1703+
targetfile=Path(options.download_path, local)
1704+
ifnottargetfile.is_file():
17041705
ifattemptdownload:
17051706
nodedownload.retrievefile(url, targetfile)
17061707
else:
17071708
print(f'Re-using existing {targetfile}')
1708-
ifos.path.isfile(targetfile):
1709+
iftargetfile.is_file():
17091710
print(f'Checking file integrity with {hashAlgo}:\r')
17101711
gotHash=nodedownload.checkHash(targetfile, hashAlgo)
17111712
print(f'{hashAlgo}: {gotHash}{targetfile}')
@@ -1792,14 +1793,15 @@ def icu_download(path):
17921793
icu_full_path=icu_deps_path
17931794

17941795
# icu-tmp is used to download and unpack the ICU tarball.
1795-
icu_tmp_path=os.path.join(icu_parent_path, 'icu-tmp')
1796+
icu_tmp_path=Path(icu_parent_path, 'icu-tmp')
17961797

17971798
# canned ICU. see tools/icu/README.md to update.
17981799
canned_icu_dir='deps/icu-small'
17991800

18001801
# use the README to verify what the canned ICU is
1801-
canned_is_full=os.path.isfile(os.path.join(canned_icu_dir, 'README-FULL-ICU.txt'))
1802-
canned_is_small=os.path.isfile(os.path.join(canned_icu_dir, 'README-SMALL-ICU.txt'))
1802+
canned_icu_path=Path(canned_icu_dir)
1803+
canned_is_full= (canned_icu_path/'README-FULL-ICU.txt').is_file()
1804+
canned_is_small= (canned_icu_path/'README-SMALL-ICU.txt').is_file()
18031805
ifcanned_is_small:
18041806
warn(f'Ignoring {canned_icu_dir} - in-repo small icu is no longer supported.')
18051807

@@ -1819,39 +1821,39 @@ def icu_download(path):
18191821
icu_config['variables']['icu_full_canned'] =1
18201822
# --with-icu-source processing
18211823
# now, check that they didn't pass --with-icu-source=deps/icu
1822-
elifwith_icu_sourceandos.path.abspath(icu_full_path) ==os.path.abspath(with_icu_source):
1824+
elifwith_icu_sourceandPath(icu_full_path).resolve()==Path(with_icu_source).resolve():
18231825
warn(f'Ignoring redundant --with-icu-source={with_icu_source}')
18241826
with_icu_source=None
18251827
# if with_icu_source is still set, try to use it.
18261828
ifwith_icu_source:
1827-
ifos.path.isdir(icu_full_path):
1829+
ifPath(icu_full_path).is_dir():
18281830
print(f'Deleting old ICU source: {icu_full_path}')
18291831
shutil.rmtree(icu_full_path)
18301832
# now, what path was given?
1831-
ifos.path.isdir(with_icu_source):
1833+
ifPath(with_icu_source).is_dir():
18321834
# it's a path. Copy it.
18331835
print(f'{with_icu_source} -> {icu_full_path}')
18341836
shutil.copytree(with_icu_source, icu_full_path)
18351837
else:
18361838
# could be file or URL.
18371839
# Set up temporary area
1838-
ifos.path.isdir(icu_tmp_path):
1840+
ifPath(icu_tmp_path).is_dir():
18391841
shutil.rmtree(icu_tmp_path)
1840-
os.mkdir(icu_tmp_path)
1842+
icu_tmp_path.mkdir()
18411843
icu_tarball=None
1842-
ifos.path.isfile(with_icu_source):
1844+
ifPath(with_icu_source).is_file():
18431845
# it's a file. Try to unpack it.
18441846
icu_tarball=with_icu_source
18451847
else:
18461848
# Can we download it?
1847-
local=os.path.join(icu_tmp_path, with_icu_source.split('/')[-1])# local part
1849+
local=icu_tmp_path/with_icu_source.split('/')[-1] # local part
18481850
icu_tarball=nodedownload.retrievefile(with_icu_source, local)
18491851
# continue with "icu_tarball"
18501852
nodedownload.unpack(icu_tarball, icu_tmp_path)
18511853
# Did it unpack correctly? Should contain 'icu'
1852-
tmp_icu=os.path.join(icu_tmp_path, 'icu')
1853-
ifos.path.isdir(tmp_icu):
1854-
os.rename(tmp_icu, icu_full_path)
1854+
tmp_icu=icu_tmp_path/'icu'
1855+
iftmp_icu.is_dir():
1856+
tmp_icu.rename(icu_full_path)
18551857
shutil.rmtree(icu_tmp_path)
18561858
else:
18571859
shutil.rmtree(icu_tmp_path)
@@ -1861,22 +1863,22 @@ def icu_download(path):
18611863
o['variables']['icu_gyp_path'] ='tools/icu/icu-generic.gyp'
18621864
# ICU source dir relative to tools/icu (for .gyp file)
18631865
o['variables']['icu_path'] =icu_full_path
1864-
ifnotos.path.isdir(icu_full_path):
1866+
ifnotPath(icu_full_path).is_dir():
18651867
# can we download (or find) a zipfile?
18661868
localzip=icu_download(icu_full_path)
18671869
iflocalzip:
18681870
nodedownload.unpack(localzip, icu_parent_path)
18691871
else:
1870-
warn("* ECMA-402 (Intl) support didn't find ICU in {icu_full_path}..")
1871-
ifnotos.path.isdir(icu_full_path):
1872+
warn(f"* ECMA-402 (Intl) support didn't find ICU in {icu_full_path}..")
1873+
ifnotPath(icu_full_path).is_dir():
18721874
error(f'''Cannot build Intl without ICU in {icu_full_path}.
18731875
Fix, or disable with "--with-intl=none"''')
18741876
else:
18751877
print_verbose(f'* Using ICU in {icu_full_path}')
18761878
# Now, what version of ICU is it? We just need the "major", such as 54.
18771879
# uvernum.h contains it as a #define.
1878-
uvernum_h=os.path.join(icu_full_path, 'source/common/unicode/uvernum.h')
1879-
ifnotos.path.isfile(uvernum_h):
1880+
uvernum_h=Path(icu_full_path, 'source', 'common', 'unicode', 'uvernum.h')
1881+
ifnotuvernum_h.is_file():
18801882
error(f'Could not load {uvernum_h} - is ICU installed?')
18811883
icu_ver_major=None
18821884
matchVerExp=r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
@@ -1896,17 +1898,15 @@ def icu_download(path):
18961898
icu_data_file_l=f'icudt{icu_ver_major}l.dat'# LE filename
18971899
icu_data_file=f'icudt{icu_ver_major}{icu_endianness}.dat'
18981900
# relative to configure
1899-
icu_data_path=os.path.join(icu_full_path,
1900-
'source/data/in',
1901-
icu_data_file_l) # LE
1901+
icu_data_path=Path(icu_full_path, 'source', 'data', 'in', icu_data_file_l) # LE
19021902
compressed_data=f'{icu_data_path}.bz2'
1903-
ifnotos.path.isfile(icu_data_path) andos.path.isfile(compressed_data):
1903+
ifnoticu_data_path.is_file() andPath(compressed_data).is_file():
19041904
# unpack. deps/icu is a temporary path
1905-
ifos.path.isdir(icu_tmp_path):
1905+
ificu_tmp_path.is_dir():
19061906
shutil.rmtree(icu_tmp_path)
1907-
os.mkdir(icu_tmp_path)
1908-
icu_data_path=os.path.join(icu_tmp_path, icu_data_file_l)
1909-
withopen(icu_data_path, 'wb') asoutf:
1907+
icu_tmp_path.mkdir()
1908+
icu_data_path=icu_tmp_path/icu_data_file_l
1909+
withicu_data_path.open(mode='wb') asoutf:
19101910
inf=bz2.BZ2File(compressed_data, 'rb')
19111911
try:
19121912
shutil.copyfileobj(inf, outf)
@@ -1915,20 +1915,18 @@ def icu_download(path):
19151915
# Now, proceed..
19161916

19171917
# relative to dep..
1918-
icu_data_in=os.path.join('..', '..', icu_data_path)
1919-
ifnotos.path.isfile(icu_data_path) andicu_endianness!='l':
1918+
icu_data_in=Path('..', '..', icu_data_path)
1919+
ifnoticu_data_path.is_file() andicu_endianness!='l':
19201920
# use host endianness
1921-
icu_data_path=os.path.join(icu_full_path,
1922-
'source/data/in',
1923-
icu_data_file) # will be generated
1924-
ifnotos.path.isfile(icu_data_path):
1921+
icu_data_path=Path(icu_full_path, 'source', 'data', 'in', icu_data_file) # will be generated
1922+
ifnoticu_data_path.is_file():
19251923
# .. and we're not about to build it from .gyp!
19261924
error(f'''ICU prebuilt data file {icu_data_path} does not exist.
19271925
See the README.md.''')
19281926

19291927
# this is the input '.dat' file to use .. icudt*.dat
19301928
# may be little-endian if from a icu-project.org tarball
1931-
o['variables']['icu_data_in'] =icu_data_in
1929+
o['variables']['icu_data_in'] =str(icu_data_in)
19321930

19331931
# map from variable name to subdirs
19341932
icu_src= {
@@ -2026,16 +2024,16 @@ def make_bin_override():
20262024
os.path.realpath(which_python) ==os.path.realpath(sys.executable)):
20272025
return
20282026

2029-
bin_override=os.path.abspath('out/tools/bin')
2027+
bin_override=Path('out', 'tools', 'bin').resolve()
20302028
try:
2031-
os.makedirs(bin_override)
2029+
bin_override.mkdir(parents=True)
20322030
exceptOSErrorase:
20332031
ife.errno!=errno.EEXIST:
20342032
raisee
20352033

2036-
python_link=os.path.join(bin_override, 'python')
2034+
python_link=bin_override/'python'
20372035
try:
2038-
os.unlink(python_link)
2036+
python_link.unlink()
20392037
exceptOSErrorase:
20402038
ife.errno!=errno.ENOENT:
20412039
raisee
@@ -2044,7 +2042,7 @@ def make_bin_override():
20442042
# We need to set the environment right now so that when gyp (in run_gyp)
20452043
# shells out, it finds the right python (specifically at
20462044
# https://github.com/nodejs/node/blob/d82e107/deps/v8/gypfiles/toolchain.gypi#L43)
2047-
os.environ['PATH'] =bin_override+':'+os.environ['PATH']
2045+
os.environ['PATH'] =str(bin_override)+':'+os.environ['PATH']
20482046

20492047
returnbin_override
20502048

@@ -2123,7 +2121,7 @@ def make_bin_override():
21232121

21242122
write('config.status', '#!/bin/sh\nset -x\nexec ./configure '+
21252123
' '.join([shlex.quote(arg) forarginoriginal_argv]) +'\n')
2126-
os.chmod('config.status', 0o775)
2124+
Path('config.status').chmod(0o775)
21272125

21282126

21292127
config= {
@@ -2153,7 +2151,7 @@ def make_bin_override():
21532151
# On Windows there's no reason to search for a different python binary.
21542152
bin_override=Noneifsys.platform=='win32'elsemake_bin_override()
21552153
ifbin_override:
2156-
config_str='export PATH:='+bin_override+':$(PATH)\n'+config_str
2154+
config_str='export PATH:='+str(bin_override)+':$(PATH)\n'+config_str
21572155

21582156
write('config.mk', do_not_edit+config_str)
21592157

0 commit comments

Comments
 (0)