Fix race between `install tot` and `active tot` (#749)

Since #732 we lookup tot release dynamically, but we don't
necessarily want to do this for the `activate` command otherwise
`install` followed by `activate` a can fail if a new build was
produced inbetween.
diff --git a/emsdk.py b/emsdk.py
index 1148412..e457be8 100644
--- a/emsdk.py
+++ b/emsdk.py
@@ -2155,6 +2155,15 @@
   return load_releases_info.cached_info
 
 
+def get_installed_sdk_version():
+  version_file = sdk_path(os.path.join('upstream', '.emsdk_version'))
+  if not os.path.exists(version_file):
+    return None
+  with open(version_file) as f:
+    version = f.read()
+  return version.split('-')[2]
+
+
 # Get a list of tags for emscripten-releases.
 def load_releases_tags():
   tags = []
@@ -2172,13 +2181,9 @@
 
   # Explicitly add the currently installed SDK version.  This could be a custom
   # version (installed explicitly) so it might not be part of the main list loaded above.
-  version_file = sdk_path(os.path.join('upstream', '.emsdk_version'))
-  if os.path.exists(version_file):
-    with open(version_file) as f:
-      version = f.read()
-    version = version.split('-')[2]
-    if version not in tags:
-      tags.append(version)
+  installed = get_installed_sdk_version()
+  if installed and installed not in tags:
+    tags.append(installed)
 
   return tags, tags_fastcomp
 
@@ -2578,7 +2583,7 @@
     exit_with_error('The fastcomp backend is not getting new builds or releases. Please use the upstream llvm backend or use an older version than 2.0.0 (such as 1.40.1).')
 
 
-def expand_sdk_name(name):
+def expand_sdk_name(name, activating):
   if name in ('latest-fastcomp', 'latest-releases-fastcomp', 'tot-fastcomp', 'sdk-nightly-latest'):
     exit_with_fastcomp_error()
   if name in ('latest', 'sdk-latest', 'latest-64bit', 'sdk-latest-64bit'):
@@ -2587,6 +2592,15 @@
   elif name in ('latest-upstream', 'latest-clang-upstream', 'latest-releases-upstream'):
     return str(find_latest_releases_sdk('upstream'))
   elif name in ('tot', 'sdk-tot', 'tot-upstream'):
+    if activating:
+      # When we are activating a tot release, assume that the currently
+      # installed SDK, if any, is the tot release we want to activate.
+      # Without this `install tot && activate tot` will race with the builders
+      # that are producing new builds.
+      installed = get_installed_sdk_version()
+      if installed:
+        debug_print('activating currently installed SDK; not updating tot version')
+        return 'sdk-releases-upstream-%s-64bit' % installed
     return str(find_tot_sdk())
   else:
     # check if it's a release handled by an emscripten-releases version,
@@ -2770,7 +2784,8 @@
 
   # Replace meta-packages with the real package names.
   if cmd in ('update', 'install', 'activate'):
-    args = [expand_sdk_name(a) for a in args]
+    activating = cmd == 'activate'
+    args = [expand_sdk_name(a, activating=activating) for a in args]
 
   load_dot_emscripten()
   load_sdk_manifest()