Skip to content

Commit 495b1fc

Browse files
committed
build: use pathlib for paths
Use Python's `pathlib` library for paths and related operations instead of `os.path`. Refs: #47323 (comment) #47323 (comment)
1 parent 070c9a8 commit 495b1fc

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

configure.py

+51-53
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') 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',
@@ -845,7 +847,7 @@
845847
(options, args) = parser.parse_known_args()
846848

847849
# Expand ~ in the install prefix now, it gets written to multiple files.
848-
options.prefix = os.path.expanduser(options.prefix or '')
850+
options.prefix = str(Path(options.prefix or '').expanduser())
849851

850852
# set up auto-download list
851853
auto_downloads = nodedownload.parse(options.download_list)
@@ -1207,7 +1209,7 @@ def configure_zos(o):
12071209
o['variables']['node_static_zoslib'] = b(True)
12081210
if options.static_zoslib_gyp:
12091211
# Apply to all Node.js components for now
1210-
o['variables']['zoslib_include_dir'] = os.path.dirname(options.static_zoslib_gyp) + '/include'
1212+
o['variables']['zoslib_include_dir'] = Path(options.static_zoslib_gyp).parent + '/include'
12111213
o['include_dirs'] += [o['variables']['zoslib_include_dir']]
12121214
else:
12131215
raise Exception('--static-zoslib-gyp=<path to zoslib.gyp file> is required.')
@@ -1609,7 +1611,7 @@ def configure_static(o):
16091611

16101612
def write(filename, data):
16111613
print_verbose('creating %s' % filename)
1612-
with open(filename, 'w+') as f:
1614+
with Path(filename).open(mode='w+', encoding='utf-8') as f:
16131615
f.write(data)
16141616

16151617
do_not_edit = '# Do not edit. Generated by the configure script.\n'
@@ -1625,8 +1627,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16251627
# srcfile uses "slash" as dir separator as its output is consumed by gyp
16261628
srcfile = '%s/%s' % (dir_sub, file)
16271629
if patch_dir:
1628-
patchfile = '%s/%s/%s' % (dir_base, patch_dir, file)
1629-
if os.path.isfile(patchfile):
1630+
patchfile = Path(dir_base, patch_dir, file)
1631+
if patchfile.is_file():
16301632
srcfile = '%s/%s' % (patch_dir, file)
16311633
info('Using floating patch "%s" from "%s"' % (patchfile, dir_base))
16321634
list.append(srcfile)
@@ -1635,9 +1637,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
16351637

16361638
def configure_intl(o):
16371639
def icu_download(path):
1638-
depFile = 'tools/icu/current_ver.dep'
1639-
with open(depFile) as f:
1640-
icus = json.load(f)
1640+
depFile = tools_path / 'icu' / 'current_ver.dep'
1641+
icus = json.loads(depFile.read_text(encoding='utf-8'))
16411642
# download ICU, if needed
16421643
if not os.access(options.download_path, os.W_OK):
16431644
error('''Cannot write to desired download path.
@@ -1652,13 +1653,13 @@ def icu_download(path):
16521653
For the entry %s,
16531654
Expected one of these keys: %s''' % (depFile, url, ' '.join(allAlgos)))
16541655
local = url.split('/')[-1]
1655-
targetfile = os.path.join(options.download_path, local)
1656-
if not os.path.isfile(targetfile):
1656+
targetfile = Path(options.download_path, local)
1657+
if not targetfile.is_file():
16571658
if attemptdownload:
16581659
nodedownload.retrievefile(url, targetfile)
16591660
else:
16601661
print('Re-using existing %s' % targetfile)
1661-
if os.path.isfile(targetfile):
1662+
if targetfile.is_file():
16621663
print('Checking file integrity with %s:\r' % hashAlgo)
16631664
gotHash = nodedownload.checkHash(targetfile, hashAlgo)
16641665
print('%s: %s %s' % (hashAlgo, gotHash, targetfile))
@@ -1744,14 +1745,15 @@ def icu_download(path):
17441745
icu_full_path = icu_deps_path
17451746

17461747
# icu-tmp is used to download and unpack the ICU tarball.
1747-
icu_tmp_path = os.path.join(icu_parent_path, 'icu-tmp')
1748+
icu_tmp_path = Path(icu_parent_path, 'icu-tmp')
17481749

17491750
# canned ICU. see tools/icu/README.md to update.
17501751
canned_icu_dir = 'deps/icu-small'
17511752

17521753
# use the README to verify what the canned ICU is
1753-
canned_is_full = os.path.isfile(os.path.join(canned_icu_dir, 'README-FULL-ICU.txt'))
1754-
canned_is_small = os.path.isfile(os.path.join(canned_icu_dir, 'README-SMALL-ICU.txt'))
1754+
pcanned_icu_dir = Path(canned_icu_dir)
1755+
canned_is_full = (pcanned_icu_dir / 'README-FULL-ICU.txt').is_file()
1756+
canned_is_small = (pcanned_icu_dir / 'README-SMALL-ICU.txt').is_file()
17551757
if canned_is_small:
17561758
warn('Ignoring %s - in-repo small icu is no longer supported.' % canned_icu_dir)
17571759

@@ -1771,39 +1773,39 @@ def icu_download(path):
17711773
icu_config['variables']['icu_full_canned'] = 1
17721774
# --with-icu-source processing
17731775
# now, check that they didn't pass --with-icu-source=deps/icu
1774-
elif with_icu_source and os.path.abspath(icu_full_path) == os.path.abspath(with_icu_source):
1776+
elif with_icu_source and Path(icu_full_path).resolve() == Path(with_icu_source).resolve():
17751777
warn('Ignoring redundant --with-icu-source=%s' % with_icu_source)
17761778
with_icu_source = None
17771779
# if with_icu_source is still set, try to use it.
17781780
if with_icu_source:
1779-
if os.path.isdir(icu_full_path):
1781+
if Path(icu_full_path).is_dir():
17801782
print('Deleting old ICU source: %s' % icu_full_path)
17811783
shutil.rmtree(icu_full_path)
17821784
# now, what path was given?
1783-
if os.path.isdir(with_icu_source):
1785+
if Path(with_icu_source).is_dir():
17841786
# it's a path. Copy it.
17851787
print('%s -> %s' % (with_icu_source, icu_full_path))
17861788
shutil.copytree(with_icu_source, icu_full_path)
17871789
else:
17881790
# could be file or URL.
17891791
# Set up temporary area
1790-
if os.path.isdir(icu_tmp_path):
1792+
if Path(icu_tmp_path).is_dir():
17911793
shutil.rmtree(icu_tmp_path)
1792-
os.mkdir(icu_tmp_path)
1794+
icu_tmp_path.mkdir()
17931795
icu_tarball = None
1794-
if os.path.isfile(with_icu_source):
1796+
if Path(with_icu_source).is_file():
17951797
# it's a file. Try to unpack it.
17961798
icu_tarball = with_icu_source
17971799
else:
17981800
# Can we download it?
1799-
local = os.path.join(icu_tmp_path, with_icu_source.split('/')[-1]) # local part
1801+
local = icu_tmp_path / with_icu_source.split('/')[-1] # local part
18001802
icu_tarball = nodedownload.retrievefile(with_icu_source, local)
18011803
# continue with "icu_tarball"
18021804
nodedownload.unpack(icu_tarball, icu_tmp_path)
18031805
# Did it unpack correctly? Should contain 'icu'
1804-
tmp_icu = os.path.join(icu_tmp_path, 'icu')
1805-
if os.path.isdir(tmp_icu):
1806-
os.rename(tmp_icu, icu_full_path)
1806+
tmp_icu = icu_tmp_path / 'icu'
1807+
if tmp_icu.is_dir():
1808+
tmp_icu.rename(icu_full_path)
18071809
shutil.rmtree(icu_tmp_path)
18081810
else:
18091811
shutil.rmtree(icu_tmp_path)
@@ -1814,22 +1816,22 @@ def icu_download(path):
18141816
o['variables']['icu_gyp_path'] = 'tools/icu/icu-generic.gyp'
18151817
# ICU source dir relative to tools/icu (for .gyp file)
18161818
o['variables']['icu_path'] = icu_full_path
1817-
if not os.path.isdir(icu_full_path):
1819+
if not Path(icu_full_path).is_dir():
18181820
# can we download (or find) a zipfile?
18191821
localzip = icu_download(icu_full_path)
18201822
if localzip:
18211823
nodedownload.unpack(localzip, icu_parent_path)
18221824
else:
18231825
warn('* ECMA-402 (Intl) support didn\'t find ICU in %s..' % icu_full_path)
1824-
if not os.path.isdir(icu_full_path):
1826+
if not Path(icu_full_path).is_dir():
18251827
error('''Cannot build Intl without ICU in %s.
18261828
Fix, or disable with "--with-intl=none"''' % icu_full_path)
18271829
else:
18281830
print_verbose('* Using ICU in %s' % icu_full_path)
18291831
# Now, what version of ICU is it? We just need the "major", such as 54.
18301832
# uvernum.h contains it as a #define.
1831-
uvernum_h = os.path.join(icu_full_path, 'source/common/unicode/uvernum.h')
1832-
if not os.path.isfile(uvernum_h):
1833+
uvernum_h = Path(icu_full_path, 'source', 'common', 'unicode', 'uvernum.h')
1834+
if not uvernum_h.is_file():
18331835
error('Could not load %s - is ICU installed?' % uvernum_h)
18341836
icu_ver_major = None
18351837
matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
@@ -1850,17 +1852,15 @@ def icu_download(path):
18501852
icu_data_file_l = 'icudt%s%s.dat' % (icu_ver_major, 'l') # LE filename
18511853
icu_data_file = 'icudt%s%s.dat' % (icu_ver_major, icu_endianness)
18521854
# relative to configure
1853-
icu_data_path = os.path.join(icu_full_path,
1854-
'source/data/in',
1855-
icu_data_file_l) # LE
1855+
icu_data_path = Path(icu_full_path, 'source', 'data', 'in', icu_data_file_l) # LE
18561856
compressed_data = '%s.bz2' % (icu_data_path)
1857-
if not os.path.isfile(icu_data_path) and os.path.isfile(compressed_data):
1857+
if not icu_data_path.is_file() and Path(compressed_data).is_file():
18581858
# unpack. deps/icu is a temporary path
1859-
if os.path.isdir(icu_tmp_path):
1859+
if icu_tmp_path.is_dir():
18601860
shutil.rmtree(icu_tmp_path)
1861-
os.mkdir(icu_tmp_path)
1862-
icu_data_path = os.path.join(icu_tmp_path, icu_data_file_l)
1863-
with open(icu_data_path, 'wb') as outf:
1861+
icu_tmp_path.mkdir()
1862+
icu_data_path = icu_tmp_path / icu_data_file_l
1863+
with icu_data_path.open(mode='wb') as outf:
18641864
inf = bz2.BZ2File(compressed_data, 'rb')
18651865
try:
18661866
shutil.copyfileobj(inf, outf)
@@ -1869,20 +1869,18 @@ def icu_download(path):
18691869
# Now, proceed..
18701870

18711871
# relative to dep..
1872-
icu_data_in = os.path.join('..','..', icu_data_path)
1873-
if not os.path.isfile(icu_data_path) and icu_endianness != 'l':
1872+
icu_data_in = Path('..', '..', icu_data_path)
1873+
if not icu_data_path.is_file() and icu_endianness != 'l':
18741874
# use host endianness
1875-
icu_data_path = os.path.join(icu_full_path,
1876-
'source/data/in',
1877-
icu_data_file) # will be generated
1878-
if not os.path.isfile(icu_data_path):
1875+
icu_data_path = Path(icu_full_path, 'source', 'data', 'in', icu_data_file) # will be generated
1876+
if not icu_data_path.is_file():
18791877
# .. and we're not about to build it from .gyp!
18801878
error('''ICU prebuilt data file %s does not exist.
18811879
See the README.md.''' % icu_data_path)
18821880

18831881
# this is the input '.dat' file to use .. icudt*.dat
18841882
# may be little-endian if from a icu-project.org tarball
1885-
o['variables']['icu_data_in'] = icu_data_in
1883+
o['variables']['icu_data_in'] = str(icu_data_in)
18861884

18871885
# map from variable name to subdirs
18881886
icu_src = {
@@ -1979,15 +1977,15 @@ def make_bin_override():
19791977
os.path.realpath(which_python) == os.path.realpath(sys.executable)):
19801978
return
19811979

1982-
bin_override = os.path.abspath('out/tools/bin')
1980+
bin_override = Path('out', 'tools', 'bin').resolve()
19831981
try:
1984-
os.makedirs(bin_override)
1982+
bin_override.mkdir(parents=True)
19851983
except OSError as e:
19861984
if e.errno != errno.EEXIST: raise e
19871985

1988-
python_link = os.path.join(bin_override, 'python')
1986+
python_link = bin_override / 'python'
19891987
try:
1990-
os.unlink(python_link)
1988+
python_link.unlink()
19911989
except OSError as e:
19921990
if e.errno != errno.ENOENT: raise e
19931991
os.symlink(sys.executable, python_link)
@@ -2074,7 +2072,7 @@ def make_bin_override():
20742072

20752073
write('config.status', '#!/bin/sh\nset -x\nexec ./configure ' +
20762074
' '.join([shlex.quote(arg) for arg in original_argv]) + '\n')
2077-
os.chmod('config.status', 0o775)
2075+
Path('config.status').chmod(0o775)
20782076

20792077

20802078
config = {

0 commit comments

Comments
 (0)