12
12
import shutil
13
13
import bz2
14
14
import io
15
+ from pathlib import Path
15
16
16
17
from distutils .version import StrictVersion
17
18
18
19
# If not run from node/, cd to node/.
19
- os .chdir (os . path . dirname (__file__ ) or '.' )
20
+ os .chdir (Path (__file__ ). parent )
20
21
21
22
original_argv = sys .argv [1 :]
22
23
25
26
CC = os .environ .get ('CC' , 'cc' if sys .platform == 'darwin' else 'gcc' )
26
27
CXX = os .environ .get ('CXX' , 'c++' if sys .platform == 'darwin' else 'g++' )
27
28
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' ))
29
32
from gyp .common import GetFlavor
30
33
31
34
# 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' ))
33
36
import nodedownload
34
37
35
38
# imports in tools/
53
56
valid_mips_fpu = ('fp32' , 'fp64' , 'fpxx' )
54
57
valid_mips_float_abi = ('soft' , 'hard' )
55
58
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' ))
58
60
59
61
shareable_builtins = {'cjs_module_lexer/lexer' : 'deps/cjs-module-lexer/lexer.js' ,
60
62
'cjs_module_lexer/dist/lexer' : 'deps/cjs-module-lexer/dist/lexer.js' ,
839
841
(options , args ) = parser .parse_known_args ()
840
842
841
843
# 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 () )
843
845
844
846
# set up auto-download list
845
847
auto_downloads = nodedownload .parse (options .download_list )
@@ -1199,7 +1201,7 @@ def configure_zos(o):
1199
1201
o ['variables' ]['node_static_zoslib' ] = b (True )
1200
1202
if options .static_zoslib_gyp :
1201
1203
# 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'
1203
1205
o ['include_dirs' ] += [o ['variables' ]['zoslib_include_dir' ]]
1204
1206
else :
1205
1207
raise Exception ('--static-zoslib-gyp=<path to zoslib.gyp file> is required.' )
@@ -1603,7 +1605,7 @@ def configure_static(o):
1603
1605
1604
1606
def write (filename , data ):
1605
1607
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 :
1607
1609
f .write (data )
1608
1610
1609
1611
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):
1619
1621
# srcfile uses "slash" as dir separator as its output is consumed by gyp
1620
1622
srcfile = f'{ dir_sub } /{ file } '
1621
1623
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 ( ):
1624
1626
srcfile = f'{ patch_dir } /{ file } '
1625
1627
info (f'Using floating patch "{ patchfile } " from "{ dir_base } "' )
1626
1628
file_list .append (srcfile )
@@ -1629,9 +1631,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
1629
1631
1630
1632
def configure_intl (o ):
1631
1633
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' ))
1635
1636
# download ICU, if needed
1636
1637
if not os .access (options .download_path , os .W_OK ):
1637
1638
error ('''Cannot write to desired download path.
@@ -1646,13 +1647,13 @@ def icu_download(path):
1646
1647
For the entry { url } ,
1647
1648
Expected one of these keys: { ' ' .join (allAlgos )} ''' )
1648
1649
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 ( ):
1651
1652
if attemptdownload :
1652
1653
nodedownload .retrievefile (url , targetfile )
1653
1654
else :
1654
1655
print (f'Re-using existing { targetfile } ' )
1655
- if os . path . isfile ( targetfile ):
1656
+ if targetfile . is_file ( ):
1656
1657
print (f'Checking file integrity with { hashAlgo } :\r ' )
1657
1658
gotHash = nodedownload .checkHash (targetfile , hashAlgo )
1658
1659
print (f'{ hashAlgo } : { gotHash } { targetfile } ' )
@@ -1739,14 +1740,15 @@ def icu_download(path):
1739
1740
icu_full_path = icu_deps_path
1740
1741
1741
1742
# 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' )
1743
1744
1744
1745
# canned ICU. see tools/icu/README.md to update.
1745
1746
canned_icu_dir = 'deps/icu-small'
1746
1747
1747
1748
# 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 ()
1750
1752
if canned_is_small :
1751
1753
warn (f'Ignoring { canned_icu_dir } - in-repo small icu is no longer supported.' )
1752
1754
@@ -1766,39 +1768,39 @@ def icu_download(path):
1766
1768
icu_config ['variables' ]['icu_full_canned' ] = 1
1767
1769
# --with-icu-source processing
1768
1770
# 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 ( ):
1770
1772
warn (f'Ignoring redundant --with-icu-source={ with_icu_source } ' )
1771
1773
with_icu_source = None
1772
1774
# if with_icu_source is still set, try to use it.
1773
1775
if with_icu_source :
1774
- if os . path . isdir (icu_full_path ):
1776
+ if Path (icu_full_path ). is_dir ( ):
1775
1777
print (f'Deleting old ICU source: { icu_full_path } ' )
1776
1778
shutil .rmtree (icu_full_path )
1777
1779
# now, what path was given?
1778
- if os . path . isdir (with_icu_source ):
1780
+ if Path (with_icu_source ). is_dir ( ):
1779
1781
# it's a path. Copy it.
1780
1782
print (f'{ with_icu_source } -> { icu_full_path } ' )
1781
1783
shutil .copytree (with_icu_source , icu_full_path )
1782
1784
else :
1783
1785
# could be file or URL.
1784
1786
# Set up temporary area
1785
- if os . path . isdir (icu_tmp_path ):
1787
+ if Path (icu_tmp_path ). is_dir ( ):
1786
1788
shutil .rmtree (icu_tmp_path )
1787
- os .mkdir (icu_tmp_path )
1789
+ icu_tmp_path .mkdir ()
1788
1790
icu_tarball = None
1789
- if os . path . isfile (with_icu_source ):
1791
+ if Path (with_icu_source ). is_file ( ):
1790
1792
# it's a file. Try to unpack it.
1791
1793
icu_tarball = with_icu_source
1792
1794
else :
1793
1795
# 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
1795
1797
icu_tarball = nodedownload .retrievefile (with_icu_source , local )
1796
1798
# continue with "icu_tarball"
1797
1799
nodedownload .unpack (icu_tarball , icu_tmp_path )
1798
1800
# 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 )
1802
1804
shutil .rmtree (icu_tmp_path )
1803
1805
else :
1804
1806
shutil .rmtree (icu_tmp_path )
@@ -1808,22 +1810,22 @@ def icu_download(path):
1808
1810
o ['variables' ]['icu_gyp_path' ] = 'tools/icu/icu-generic.gyp'
1809
1811
# ICU source dir relative to tools/icu (for .gyp file)
1810
1812
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 ( ):
1812
1814
# can we download (or find) a zipfile?
1813
1815
localzip = icu_download (icu_full_path )
1814
1816
if localzip :
1815
1817
nodedownload .unpack (localzip , icu_parent_path )
1816
1818
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 ( ):
1819
1821
error (f'''Cannot build Intl without ICU in { icu_full_path } .
1820
1822
Fix, or disable with "--with-intl=none"''' )
1821
1823
else :
1822
1824
print_verbose (f'* Using ICU in { icu_full_path } ' )
1823
1825
# Now, what version of ICU is it? We just need the "major", such as 54.
1824
1826
# 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 ( ):
1827
1829
error (f'Could not load { uvernum_h } - is ICU installed?' )
1828
1830
icu_ver_major = None
1829
1831
matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
@@ -1843,17 +1845,15 @@ def icu_download(path):
1843
1845
icu_data_file_l = f'icudt{ icu_ver_major } l.dat' # LE filename
1844
1846
icu_data_file = f'icudt{ icu_ver_major } { icu_endianness } .dat'
1845
1847
# 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
1849
1849
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 ( ):
1851
1851
# unpack. deps/icu is a temporary path
1852
- if os . path . isdir ( icu_tmp_path ):
1852
+ if icu_tmp_path . is_dir ( ):
1853
1853
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 :
1857
1857
inf = bz2 .BZ2File (compressed_data , 'rb' )
1858
1858
try :
1859
1859
shutil .copyfileobj (inf , outf )
@@ -1862,20 +1862,18 @@ def icu_download(path):
1862
1862
# Now, proceed..
1863
1863
1864
1864
# 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' :
1867
1867
# 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 ():
1872
1870
# .. and we're not about to build it from .gyp!
1873
1871
error (f'''ICU prebuilt data file { icu_data_path } does not exist.
1874
1872
See the README.md.''' )
1875
1873
1876
1874
# this is the input '.dat' file to use .. icudt*.dat
1877
1875
# 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 )
1879
1877
1880
1878
# map from variable name to subdirs
1881
1879
icu_src = {
@@ -1973,16 +1971,16 @@ def make_bin_override():
1973
1971
os .path .realpath (which_python ) == os .path .realpath (sys .executable )):
1974
1972
return
1975
1973
1976
- bin_override = os . path . abspath ('out/ tools/ bin' )
1974
+ bin_override = Path ('out' , ' tools' , ' bin'). resolve ( )
1977
1975
try :
1978
- os . makedirs ( bin_override )
1976
+ bin_override . mkdir ( parents = True )
1979
1977
except OSError as e :
1980
1978
if e .errno != errno .EEXIST :
1981
1979
raise e
1982
1980
1983
- python_link = os . path . join ( bin_override , 'python' )
1981
+ python_link = bin_override / 'python'
1984
1982
try :
1985
- os .unlink (python_link )
1983
+ python_link .unlink ()
1986
1984
except OSError as e :
1987
1985
if e .errno != errno .ENOENT :
1988
1986
raise e
@@ -1991,7 +1989,7 @@ def make_bin_override():
1991
1989
# We need to set the environment right now so that when gyp (in run_gyp)
1992
1990
# shells out, it finds the right python (specifically at
1993
1991
# 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' ]
1995
1993
1996
1994
return bin_override
1997
1995
@@ -2070,7 +2068,7 @@ def make_bin_override():
2070
2068
2071
2069
write ('config.status' , '#!/bin/sh\n set -x\n exec ./configure ' +
2072
2070
' ' .join ([shlex .quote (arg ) for arg in original_argv ]) + '\n ' )
2073
- os . chmod ('config.status' , 0o775 )
2071
+ Path ('config.status' ). chmod ( 0o775 )
2074
2072
2075
2073
2076
2074
config = {
@@ -2100,7 +2098,7 @@ def make_bin_override():
2100
2098
# On Windows there's no reason to search for a different python binary.
2101
2099
bin_override = None if sys .platform == 'win32' else make_bin_override ()
2102
2100
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
2104
2102
2105
2103
write ('config.mk' , do_not_edit + config_str )
2106
2104
0 commit comments