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' ,
868
870
(options , args ) = parser .parse_known_args ()
869
871
870
872
# Expand ~ in the install prefix now, it gets written to multiple files.
871
- options .prefix = os . path . expanduser ( options .prefix or '' )
873
+ options .prefix = str ( Path ( options .prefix or '' ). expanduser () )
872
874
873
875
# set up auto-download list
874
876
auto_downloads = nodedownload .parse (options .download_list )
@@ -1228,7 +1230,7 @@ def configure_zos(o):
1228
1230
o ['variables' ]['node_static_zoslib' ] = b (True )
1229
1231
if options .static_zoslib_gyp :
1230
1232
# 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'
1232
1234
o ['include_dirs' ] += [o ['variables' ]['zoslib_include_dir' ]]
1233
1235
else :
1234
1236
raise Exception ('--static-zoslib-gyp=<path to zoslib.gyp file> is required.' )
@@ -1656,7 +1658,7 @@ def configure_static(o):
1656
1658
1657
1659
def write (filename , data ):
1658
1660
print_verbose (f'creating { filename } ' )
1659
- with open (filename , 'w+' , encoding = 'utf-8' ) as f :
1661
+ with Path (filename ). open ( mode = 'w+' , encoding = 'utf-8' ) as f :
1660
1662
f .write (data )
1661
1663
1662
1664
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):
1672
1674
# srcfile uses "slash" as dir separator as its output is consumed by gyp
1673
1675
srcfile = f'{ dir_sub } /{ file } '
1674
1676
if patch_dir :
1675
- patchfile = f' { dir_base } { patch_dir } { file } '
1676
- if os . path . isfile ( patchfile ):
1677
+ patchfile = Path ( dir_base , patch_dir , file )
1678
+ if patchfile . is_file ( ):
1677
1679
srcfile = f'{ patch_dir } /{ file } '
1678
1680
info (f'Using floating patch "{ patchfile } " from "{ dir_base } "' )
1679
1681
file_list .append (srcfile )
@@ -1682,9 +1684,8 @@ def glob_to_var(dir_base, dir_sub, patch_dir):
1682
1684
1683
1685
def configure_intl (o ):
1684
1686
def icu_download (path ):
1685
- depFile = 'tools/icu/current_ver.dep'
1686
- with open (depFile , encoding = 'utf-8' ) as f :
1687
- icus = json .load (f )
1687
+ depFile = tools_path / 'icu' / 'current_ver.dep'
1688
+ icus = json .loads (depFile .read_text (encoding = 'utf-8' ))
1688
1689
# download ICU, if needed
1689
1690
if not os .access (options .download_path , os .W_OK ):
1690
1691
error ('''Cannot write to desired download path.
@@ -1699,13 +1700,13 @@ def icu_download(path):
1699
1700
For the entry { url } ,
1700
1701
Expected one of these keys: { ' ' .join (allAlgos )} ''' )
1701
1702
local = url .split ('/' )[- 1 ]
1702
- targetfile = os . path . join (options .download_path , local )
1703
- if not os . path . isfile ( targetfile ):
1703
+ targetfile = Path (options .download_path , local )
1704
+ if not targetfile . is_file ( ):
1704
1705
if attemptdownload :
1705
1706
nodedownload .retrievefile (url , targetfile )
1706
1707
else :
1707
1708
print (f'Re-using existing { targetfile } ' )
1708
- if os . path . isfile ( targetfile ):
1709
+ if targetfile . is_file ( ):
1709
1710
print (f'Checking file integrity with { hashAlgo } :\r ' )
1710
1711
gotHash = nodedownload .checkHash (targetfile , hashAlgo )
1711
1712
print (f'{ hashAlgo } : { gotHash } { targetfile } ' )
@@ -1792,14 +1793,15 @@ def icu_download(path):
1792
1793
icu_full_path = icu_deps_path
1793
1794
1794
1795
# 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' )
1796
1797
1797
1798
# canned ICU. see tools/icu/README.md to update.
1798
1799
canned_icu_dir = 'deps/icu-small'
1799
1800
1800
1801
# 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 ()
1803
1805
if canned_is_small :
1804
1806
warn (f'Ignoring { canned_icu_dir } - in-repo small icu is no longer supported.' )
1805
1807
@@ -1819,39 +1821,39 @@ def icu_download(path):
1819
1821
icu_config ['variables' ]['icu_full_canned' ] = 1
1820
1822
# --with-icu-source processing
1821
1823
# now, check that they didn't pass --with-icu-source=deps/icu
1822
- elif with_icu_source and os . path . abspath (icu_full_path ) == os . path . abspath (with_icu_source ):
1824
+ elif with_icu_source and Path (icu_full_path ). resolve () == Path (with_icu_source ). resolve ( ):
1823
1825
warn (f'Ignoring redundant --with-icu-source={ with_icu_source } ' )
1824
1826
with_icu_source = None
1825
1827
# if with_icu_source is still set, try to use it.
1826
1828
if with_icu_source :
1827
- if os . path . isdir (icu_full_path ):
1829
+ if Path (icu_full_path ). is_dir ( ):
1828
1830
print (f'Deleting old ICU source: { icu_full_path } ' )
1829
1831
shutil .rmtree (icu_full_path )
1830
1832
# now, what path was given?
1831
- if os . path . isdir (with_icu_source ):
1833
+ if Path (with_icu_source ). is_dir ( ):
1832
1834
# it's a path. Copy it.
1833
1835
print (f'{ with_icu_source } -> { icu_full_path } ' )
1834
1836
shutil .copytree (with_icu_source , icu_full_path )
1835
1837
else :
1836
1838
# could be file or URL.
1837
1839
# Set up temporary area
1838
- if os . path . isdir (icu_tmp_path ):
1840
+ if Path (icu_tmp_path ). is_dir ( ):
1839
1841
shutil .rmtree (icu_tmp_path )
1840
- os .mkdir (icu_tmp_path )
1842
+ icu_tmp_path .mkdir ()
1841
1843
icu_tarball = None
1842
- if os . path . isfile (with_icu_source ):
1844
+ if Path (with_icu_source ). is_file ( ):
1843
1845
# it's a file. Try to unpack it.
1844
1846
icu_tarball = with_icu_source
1845
1847
else :
1846
1848
# 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
1848
1850
icu_tarball = nodedownload .retrievefile (with_icu_source , local )
1849
1851
# continue with "icu_tarball"
1850
1852
nodedownload .unpack (icu_tarball , icu_tmp_path )
1851
1853
# Did it unpack correctly? Should contain 'icu'
1852
- tmp_icu = os . path . join ( icu_tmp_path , 'icu' )
1853
- if os . path . isdir ( tmp_icu ):
1854
- os .rename (tmp_icu , icu_full_path )
1854
+ tmp_icu = icu_tmp_path / 'icu'
1855
+ if tmp_icu . is_dir ( ):
1856
+ tmp_icu .rename (icu_full_path )
1855
1857
shutil .rmtree (icu_tmp_path )
1856
1858
else :
1857
1859
shutil .rmtree (icu_tmp_path )
@@ -1861,22 +1863,22 @@ def icu_download(path):
1861
1863
o ['variables' ]['icu_gyp_path' ] = 'tools/icu/icu-generic.gyp'
1862
1864
# ICU source dir relative to tools/icu (for .gyp file)
1863
1865
o ['variables' ]['icu_path' ] = icu_full_path
1864
- if not os . path . isdir (icu_full_path ):
1866
+ if not Path (icu_full_path ). is_dir ( ):
1865
1867
# can we download (or find) a zipfile?
1866
1868
localzip = icu_download (icu_full_path )
1867
1869
if localzip :
1868
1870
nodedownload .unpack (localzip , icu_parent_path )
1869
1871
else :
1870
- warn ("* ECMA-402 (Intl) support didn't find ICU in {icu_full_path}.." )
1871
- if not os . path . isdir (icu_full_path ):
1872
+ warn (f "* ECMA-402 (Intl) support didn't find ICU in { icu_full_path } .." )
1873
+ if not Path (icu_full_path ). is_dir ( ):
1872
1874
error (f'''Cannot build Intl without ICU in { icu_full_path } .
1873
1875
Fix, or disable with "--with-intl=none"''' )
1874
1876
else :
1875
1877
print_verbose (f'* Using ICU in { icu_full_path } ' )
1876
1878
# Now, what version of ICU is it? We just need the "major", such as 54.
1877
1879
# uvernum.h contains it as a #define.
1878
- uvernum_h = os . path . join (icu_full_path , 'source/ common/ unicode/ uvernum.h' )
1879
- if not os . path . isfile ( uvernum_h ):
1880
+ uvernum_h = Path (icu_full_path , 'source' , ' common' , ' unicode' , ' uvernum.h' )
1881
+ if not uvernum_h . is_file ( ):
1880
1882
error (f'Could not load { uvernum_h } - is ICU installed?' )
1881
1883
icu_ver_major = None
1882
1884
matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
@@ -1896,17 +1898,15 @@ def icu_download(path):
1896
1898
icu_data_file_l = f'icudt{ icu_ver_major } l.dat' # LE filename
1897
1899
icu_data_file = f'icudt{ icu_ver_major } { icu_endianness } .dat'
1898
1900
# 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
1902
1902
compressed_data = f'{ icu_data_path } .bz2'
1903
- if not os . path . isfile ( icu_data_path ) and os . path . isfile (compressed_data ):
1903
+ if not icu_data_path . is_file ( ) and Path (compressed_data ). is_file ( ):
1904
1904
# unpack. deps/icu is a temporary path
1905
- if os . path . isdir ( icu_tmp_path ):
1905
+ if icu_tmp_path . is_dir ( ):
1906
1906
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
- with open (icu_data_path , 'wb' ) as outf :
1907
+ icu_tmp_path .mkdir ()
1908
+ icu_data_path = icu_tmp_path / icu_data_file_l
1909
+ with icu_data_path . open (mode = 'wb' ) as outf :
1910
1910
inf = bz2 .BZ2File (compressed_data , 'rb' )
1911
1911
try :
1912
1912
shutil .copyfileobj (inf , outf )
@@ -1915,20 +1915,18 @@ def icu_download(path):
1915
1915
# Now, proceed..
1916
1916
1917
1917
# relative to dep..
1918
- icu_data_in = os . path . join ('..' , '..' , icu_data_path )
1919
- if not os . path . isfile ( icu_data_path ) and icu_endianness != 'l' :
1918
+ icu_data_in = Path ('..' , '..' , icu_data_path )
1919
+ if not icu_data_path . is_file ( ) and icu_endianness != 'l' :
1920
1920
# 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
- if not os .path .isfile (icu_data_path ):
1921
+ icu_data_path = Path (icu_full_path , 'source' , 'data' , 'in' , icu_data_file ) # will be generated
1922
+ if not icu_data_path .is_file ():
1925
1923
# .. and we're not about to build it from .gyp!
1926
1924
error (f'''ICU prebuilt data file { icu_data_path } does not exist.
1927
1925
See the README.md.''' )
1928
1926
1929
1927
# this is the input '.dat' file to use .. icudt*.dat
1930
1928
# 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 )
1932
1930
1933
1931
# map from variable name to subdirs
1934
1932
icu_src = {
@@ -2026,16 +2024,16 @@ def make_bin_override():
2026
2024
os .path .realpath (which_python ) == os .path .realpath (sys .executable )):
2027
2025
return
2028
2026
2029
- bin_override = os . path . abspath ('out/ tools/ bin' )
2027
+ bin_override = Path ('out' , ' tools' , ' bin'). resolve ( )
2030
2028
try :
2031
- os . makedirs ( bin_override )
2029
+ bin_override . mkdir ( parents = True )
2032
2030
except OSError as e :
2033
2031
if e .errno != errno .EEXIST :
2034
2032
raise e
2035
2033
2036
- python_link = os . path . join ( bin_override , 'python' )
2034
+ python_link = bin_override / 'python'
2037
2035
try :
2038
- os .unlink (python_link )
2036
+ python_link .unlink ()
2039
2037
except OSError as e :
2040
2038
if e .errno != errno .ENOENT :
2041
2039
raise e
@@ -2044,7 +2042,7 @@ def make_bin_override():
2044
2042
# We need to set the environment right now so that when gyp (in run_gyp)
2045
2043
# shells out, it finds the right python (specifically at
2046
2044
# 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' ]
2048
2046
2049
2047
return bin_override
2050
2048
@@ -2123,7 +2121,7 @@ def make_bin_override():
2123
2121
2124
2122
write ('config.status' , '#!/bin/sh\n set -x\n exec ./configure ' +
2125
2123
' ' .join ([shlex .quote (arg ) for arg in original_argv ]) + '\n ' )
2126
- os . chmod ('config.status' , 0o775 )
2124
+ Path ('config.status' ). chmod ( 0o775 )
2127
2125
2128
2126
2129
2127
config = {
@@ -2153,7 +2151,7 @@ def make_bin_override():
2153
2151
# On Windows there's no reason to search for a different python binary.
2154
2152
bin_override = None if sys .platform == 'win32' else make_bin_override ()
2155
2153
if bin_override :
2156
- config_str = 'export PATH:=' + bin_override + ':$(PATH)\n ' + config_str
2154
+ config_str = 'export PATH:=' + str ( bin_override ) + ':$(PATH)\n ' + config_str
2157
2155
2158
2156
write ('config.mk' , do_not_edit + config_str )
2159
2157
0 commit comments