Skip to content

Commit

Permalink
Use constants for fixed paths. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Nov 30, 2022
1 parent 87e02fc commit 0b2fa6e
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions emsdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,14 @@ def to_unix_path(p):
return p.replace('\\', '/')


def emsdk_path():
return to_unix_path(os.path.dirname(os.path.realpath(__file__)))
emsdk_path = to_unix_path(os.path.dirname(os.path.realpath(__file__)))


EMSDK_SET_ENV = ""
if POWERSHELL:
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.ps1')
EMSDK_SET_ENV = os.path.join(emsdk_path, 'emsdk_set_env.ps1')
else:
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.bat')
EMSDK_SET_ENV = os.path.join(emsdk_path, 'emsdk_set_env.bat')


# Parses https://github.com/emscripten-core/emscripten/tree/d6aced8 to a pair (https://github.com/emscripten-core/emscripten, d6aced8)
Expand Down Expand Up @@ -486,7 +485,7 @@ def sdk_path(path):
if os.path.isabs(path):
return path

return to_unix_path(os.path.join(emsdk_path(), path))
return to_unix_path(os.path.join(emsdk_path, path))


# Removes a single file, suppressing exceptions on failure.
Expand Down Expand Up @@ -1615,7 +1614,7 @@ def to_native_path(p):
# Finds and returns a list of the directories that need to be added to PATH for
# the given set of tools.
def get_required_path(active_tools):
path_add = [to_native_path(emsdk_path())]
path_add = [to_native_path(emsdk_path)]
for tool in active_tools:
if hasattr(tool, 'activated_path'):
path = to_native_path(tool.expand_vars(tool.activated_path))
Expand All @@ -1625,10 +1624,7 @@ def get_required_path(active_tools):

# Returns the absolute path to the file '.emscripten' for the current user on
# this system.
def dot_emscripten_path():
return os.path.join(emsdk_path(), ".emscripten")


dot_emscripten_path = os.path.join(emsdk_path, ".emscripten")
dot_emscripten = {}


Expand All @@ -1648,7 +1644,7 @@ def load_dot_emscripten():
dot_emscripten.clear()
lines = []
try:
lines = open(dot_emscripten_path(), "r").read().split('\n')
lines = open(dot_emscripten_path, "r").read().split('\n')
except:
pass
for line in lines:
Expand Down Expand Up @@ -1685,17 +1681,17 @@ def generate_dot_emscripten(active_tools):
JS_ENGINES = [NODE_JS]
'''

cfg = cfg.replace("'" + emsdk_path(), "emsdk_path + '")
cfg = cfg.replace("'" + emsdk_path, "emsdk_path + '")

if os.path.exists(dot_emscripten_path()):
backup_path = dot_emscripten_path() + ".old"
move_with_overwrite(dot_emscripten_path(), backup_path)
if os.path.exists(dot_emscripten_path):
backup_path = dot_emscripten_path + ".old"
move_with_overwrite(dot_emscripten_path, backup_path)

with open(dot_emscripten_path(), "w") as text_file:
with open(dot_emscripten_path, "w") as text_file:
text_file.write(cfg)

# Clear old emscripten content.
rmfile(os.path.join(emsdk_path(), ".emscripten_sanity"))
rmfile(os.path.join(emsdk_path, ".emscripten_sanity"))

path_add = get_required_path(active_tools)
if not WINDOWS:
Expand Down Expand Up @@ -1933,7 +1929,7 @@ def is_active(self):

# all paths are stored dynamically relative to the emsdk root, so
# normalize those first.
dot_emscripten_key = dot_emscripten[key].replace("emsdk_path + '", "'" + emsdk_path())
dot_emscripten_key = dot_emscripten[key].replace("emsdk_path + '", "'" + emsdk_path)
dot_emscripten_key = dot_emscripten_key.strip("'")
if dot_emscripten_key != value:
debug_print(str(self) + ' is not active, because key="' + key + '" has value "' + dot_emscripten_key + '" but should have value "' + value + '"')
Expand Down Expand Up @@ -2013,7 +2009,7 @@ def install_sdk(self):
if getattr(self, 'custom_install_script', None) == 'emscripten_npm_install':
# upstream tools have hardcoded paths that are not stored in emsdk_manifest.json registry
install_path = 'upstream' if 'releases-upstream' in self.version else 'fastcomp'
emscripten_dir = os.path.join(emsdk_path(), install_path, 'emscripten')
emscripten_dir = os.path.join(emsdk_path, install_path, 'emscripten')
# Older versions of the sdk did not include the node_modules directory
# and require `npm ci` to be run post-install
if not os.path.exists(os.path.join(emscripten_dir, 'node_modules')):
Expand Down Expand Up @@ -2278,14 +2274,14 @@ def python_2_3_sorted(arr, cmp):


def is_emsdk_sourced_from_github():
return os.path.exists(os.path.join(emsdk_path(), '.git'))
return os.path.exists(os.path.join(emsdk_path, '.git'))


def update_emsdk():
if is_emsdk_sourced_from_github():
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)')
sys.exit(1)
if not download_and_unzip(emsdk_zip_download_url, emsdk_path(), clobber=False):
if not download_and_unzip(emsdk_zip_download_url, emsdk_path, clobber=False):
sys.exit(1)


Expand Down Expand Up @@ -2547,7 +2543,7 @@ def process_tool_list(tools_to_activate):


def write_set_env_script(env_string):
assert(CMD or POWERSHELL)
assert CMD or POWERSHELL
open(EMSDK_SET_ENV, 'w').write(env_string)


Expand Down Expand Up @@ -2630,7 +2626,7 @@ def adjusted_path(tools_to_activate, system=False, user=False):
existing_path = win_get_environment_variable('PATH', system=system, user=user, fallback=True).split(ENVPATH_SEPARATOR)
else:
existing_path = os.environ['PATH'].split(ENVPATH_SEPARATOR)
emsdk_root_path = to_unix_path(emsdk_path())
emsdk_root_path = to_unix_path(emsdk_path)

existing_emsdk_tools = []
existing_nonemsdk_path = []
Expand Down Expand Up @@ -2678,7 +2674,7 @@ def get_env_vars_to_add(tools_to_activate, system, user):
info('')

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

for tool in tools_to_activate:
config = tool.activated_config()
Expand Down Expand Up @@ -2706,7 +2702,7 @@ def get_env_vars_to_add(tools_to_activate, system, user):
em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
env_vars_to_add += [('EM_CACHE', em_cache_dir)]
if version < [1, 39, 13]:
env_vars_to_add += [('EM_CONFIG', os.path.normpath(dot_emscripten_path()))]
env_vars_to_add += [('EM_CONFIG', os.path.normpath(dot_emscripten_path))]

envs = tool.activated_environment()
for env in envs:
Expand Down

0 comments on commit 0b2fa6e

Please sign in to comment.