Skip to content

Commit 32fba7b

Browse files
authored
Use constants for fixed paths. NFC (#1140)
1 parent 87e02fc commit 32fba7b

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

emsdk.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,13 @@ def to_unix_path(p):
191191
return p.replace('\\', '/')
192192

193193

194-
def emsdk_path():
195-
return to_unix_path(os.path.dirname(os.path.realpath(__file__)))
196-
194+
EMSDK_PATH = to_unix_path(os.path.dirname(os.path.realpath(__file__)))
197195

198196
EMSDK_SET_ENV = ""
199197
if POWERSHELL:
200-
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.ps1')
198+
EMSDK_SET_ENV = os.path.join(EMSDK_PATH, 'emsdk_set_env.ps1')
201199
else:
202-
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.bat')
200+
EMSDK_SET_ENV = os.path.join(EMSDK_PATH, 'emsdk_set_env.bat')
203201

204202

205203
# Parses https://github.com/emscripten-core/emscripten/tree/d6aced8 to a pair (https://github.com/emscripten-core/emscripten, d6aced8)
@@ -486,7 +484,7 @@ def sdk_path(path):
486484
if os.path.isabs(path):
487485
return path
488486

489-
return to_unix_path(os.path.join(emsdk_path(), path))
487+
return to_unix_path(os.path.join(EMSDK_PATH, path))
490488

491489

492490
# Removes a single file, suppressing exceptions on failure.
@@ -1615,7 +1613,7 @@ def to_native_path(p):
16151613
# Finds and returns a list of the directories that need to be added to PATH for
16161614
# the given set of tools.
16171615
def get_required_path(active_tools):
1618-
path_add = [to_native_path(emsdk_path())]
1616+
path_add = [to_native_path(EMSDK_PATH)]
16191617
for tool in active_tools:
16201618
if hasattr(tool, 'activated_path'):
16211619
path = to_native_path(tool.expand_vars(tool.activated_path))
@@ -1625,11 +1623,8 @@ def get_required_path(active_tools):
16251623

16261624
# Returns the absolute path to the file '.emscripten' for the current user on
16271625
# this system.
1628-
def dot_emscripten_path():
1629-
return os.path.join(emsdk_path(), ".emscripten")
1630-
1631-
1632-
dot_emscripten = {}
1626+
EM_CONFIG_PATH = os.path.join(EMSDK_PATH, ".emscripten")
1627+
EM_CONFIG_DICT = {}
16331628

16341629

16351630
def parse_key_value(line):
@@ -1644,23 +1639,23 @@ def parse_key_value(line):
16441639
return (key, '')
16451640

16461641

1647-
def load_dot_emscripten():
1648-
dot_emscripten.clear()
1642+
def load_em_config():
1643+
EM_CONFIG_DICT.clear()
16491644
lines = []
16501645
try:
1651-
lines = open(dot_emscripten_path(), "r").read().split('\n')
1646+
lines = open(EM_CONFIG_PATH, "r").read().split('\n')
16521647
except:
16531648
pass
16541649
for line in lines:
16551650
try:
16561651
key, value = parse_key_value(line)
16571652
if value != '':
1658-
dot_emscripten[key] = value
1653+
EM_CONFIG_DICT[key] = value
16591654
except:
16601655
pass
16611656

16621657

1663-
def generate_dot_emscripten(active_tools):
1658+
def generate_em_config(active_tools):
16641659
cfg = 'import os\n'
16651660
cfg += "emsdk_path = os.path.dirname(os.getenv('EM_CONFIG')).replace('\\\\', '/')\n"
16661661

@@ -1685,17 +1680,17 @@ def generate_dot_emscripten(active_tools):
16851680
JS_ENGINES = [NODE_JS]
16861681
'''
16871682

1688-
cfg = cfg.replace("'" + emsdk_path(), "emsdk_path + '")
1683+
cfg = cfg.replace("'" + EMSDK_PATH, "emsdk_path + '")
16891684

1690-
if os.path.exists(dot_emscripten_path()):
1691-
backup_path = dot_emscripten_path() + ".old"
1692-
move_with_overwrite(dot_emscripten_path(), backup_path)
1685+
if os.path.exists(EM_CONFIG_PATH):
1686+
backup_path = EM_CONFIG_PATH + ".old"
1687+
move_with_overwrite(EM_CONFIG_PATH, backup_path)
16931688

1694-
with open(dot_emscripten_path(), "w") as text_file:
1689+
with open(EM_CONFIG_PATH, "w") as text_file:
16951690
text_file.write(cfg)
16961691

16971692
# Clear old emscripten content.
1698-
rmfile(os.path.join(emsdk_path(), ".emscripten_sanity"))
1693+
rmfile(os.path.join(EMSDK_PATH, ".emscripten_sanity"))
16991694

17001695
path_add = get_required_path(active_tools)
17011696
if not WINDOWS:
@@ -1927,16 +1922,16 @@ def is_active(self):
19271922
return len(deps) > 0
19281923

19291924
for key, value in activated_cfg.items():
1930-
if key not in dot_emscripten:
1925+
if key not in EM_CONFIG_DICT:
19311926
debug_print(str(self) + ' is not active, because key="' + key + '" does not exist in .emscripten')
19321927
return False
19331928

19341929
# all paths are stored dynamically relative to the emsdk root, so
19351930
# normalize those first.
1936-
dot_emscripten_key = dot_emscripten[key].replace("emsdk_path + '", "'" + emsdk_path())
1937-
dot_emscripten_key = dot_emscripten_key.strip("'")
1938-
if dot_emscripten_key != value:
1939-
debug_print(str(self) + ' is not active, because key="' + key + '" has value "' + dot_emscripten_key + '" but should have value "' + value + '"')
1931+
config_value = EM_CONFIG_DICT[key].replace("emsdk_path + '", "'" + EMSDK_PATH)
1932+
config_value = config_value.strip("'")
1933+
if config_value != value:
1934+
debug_print(str(self) + ' is not active, because key="' + key + '" has value "' + config_value + '" but should have value "' + value + '"')
19401935
return False
19411936
return True
19421937

@@ -2013,7 +2008,7 @@ def install_sdk(self):
20132008
if getattr(self, 'custom_install_script', None) == 'emscripten_npm_install':
20142009
# upstream tools have hardcoded paths that are not stored in emsdk_manifest.json registry
20152010
install_path = 'upstream' if 'releases-upstream' in self.version else 'fastcomp'
2016-
emscripten_dir = os.path.join(emsdk_path(), install_path, 'emscripten')
2011+
emscripten_dir = os.path.join(EMSDK_PATH, install_path, 'emscripten')
20172012
# Older versions of the sdk did not include the node_modules directory
20182013
# and require `npm ci` to be run post-install
20192014
if not os.path.exists(os.path.join(emscripten_dir, 'node_modules')):
@@ -2278,14 +2273,14 @@ def python_2_3_sorted(arr, cmp):
22782273

22792274

22802275
def is_emsdk_sourced_from_github():
2281-
return os.path.exists(os.path.join(emsdk_path(), '.git'))
2276+
return os.path.exists(os.path.join(EMSDK_PATH, '.git'))
22822277

22832278

22842279
def update_emsdk():
22852280
if is_emsdk_sourced_from_github():
22862281
errlog('You seem to have bootstrapped Emscripten SDK by cloning from GitHub. In this case, use "git pull" instead of "emsdk update" to update emsdk. (Not doing that automatically in case you have local changes)')
22872282
sys.exit(1)
2288-
if not download_and_unzip(emsdk_zip_download_url, emsdk_path(), clobber=False):
2283+
if not download_and_unzip(emsdk_zip_download_url, EMSDK_PATH, clobber=False):
22892284
sys.exit(1)
22902285

22912286

@@ -2547,7 +2542,7 @@ def process_tool_list(tools_to_activate):
25472542

25482543

25492544
def write_set_env_script(env_string):
2550-
assert(CMD or POWERSHELL)
2545+
assert CMD or POWERSHELL
25512546
open(EMSDK_SET_ENV, 'w').write(env_string)
25522547

25532548

@@ -2562,7 +2557,7 @@ def set_active_tools(tools_to_activate, permanently_activate, system):
25622557
print('Setting the following tools as active:\n ' + '\n '.join(map(lambda x: str(x), tools)))
25632558
print('')
25642559

2565-
generate_dot_emscripten(tools_to_activate)
2560+
generate_em_config(tools_to_activate)
25662561

25672562
# Construct a .bat or .ps1 script that will be invoked to set env. vars and PATH
25682563
# We only do this on cmd or powershell since emsdk.bat/ps1 is able to modify the
@@ -2630,12 +2625,11 @@ def adjusted_path(tools_to_activate, system=False, user=False):
26302625
existing_path = win_get_environment_variable('PATH', system=system, user=user, fallback=True).split(ENVPATH_SEPARATOR)
26312626
else:
26322627
existing_path = os.environ['PATH'].split(ENVPATH_SEPARATOR)
2633-
emsdk_root_path = to_unix_path(emsdk_path())
26342628

26352629
existing_emsdk_tools = []
26362630
existing_nonemsdk_path = []
26372631
for entry in existing_path:
2638-
if to_unix_path(entry).startswith(emsdk_root_path):
2632+
if to_unix_path(entry).startswith(EMSDK_PATH):
26392633
existing_emsdk_tools.append(entry)
26402634
else:
26412635
existing_nonemsdk_path.append(entry)
@@ -2678,7 +2672,7 @@ def get_env_vars_to_add(tools_to_activate, system, user):
26782672
info('')
26792673

26802674
# A core variable EMSDK points to the root of Emscripten SDK directory.
2681-
env_vars_to_add += [('EMSDK', to_unix_path(emsdk_path()))]
2675+
env_vars_to_add += [('EMSDK', EMSDK_PATH)]
26822676

26832677
for tool in tools_to_activate:
26842678
config = tool.activated_config()
@@ -2706,7 +2700,7 @@ def get_env_vars_to_add(tools_to_activate, system, user):
27062700
em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
27072701
env_vars_to_add += [('EM_CACHE', em_cache_dir)]
27082702
if version < [1, 39, 13]:
2709-
env_vars_to_add += [('EM_CONFIG', os.path.normpath(dot_emscripten_path()))]
2703+
env_vars_to_add += [('EM_CONFIG', os.path.normpath(EM_CONFIG_PATH))]
27102704

27112705
envs = tool.activated_environment()
27122706
for env in envs:
@@ -3020,7 +3014,7 @@ def extract_string_arg(name):
30203014
activating = cmd == 'activate'
30213015
args = [expand_sdk_name(a, activating=activating) for a in args]
30223016

3023-
load_dot_emscripten()
3017+
load_em_config()
30243018
load_sdk_manifest()
30253019

30263020
# Apply any overrides to git branch names to clone from.

0 commit comments

Comments
 (0)