title: Python: Spawn sudo process (in new terminal), wait until completed
body: <p>Edit: my final code goes something like this:</p>
  
  <pre><code>#WARNING: all " in command need to be escaped: \\"
  def spawnInNewTerminal(command):
      #creates lock file
      lock = open(lockPath, 'w')
      lock.write("Currently performing task in separate terminal.")
      lock.close()
  
      #adds line to command to remove lock file
      command += ";rm " + lockPath
  
      #executes the command in a new terminal
      process = subprocess.Popen (
          ['x-terminal-emulator', '-e',  'sh -c "{0}"'.format(command) ]
          , stdout=subprocess.PIPE )
      process.wait()
  
      #doesn't let us proceed until the lock file has been removed by the bash command
      while os.path.exists(lockPath):
          time.sleep(0.1)
  </code></pre>
  
  <hr>
  
  <p>Original question:</p>
  
  <p>I am writing a simple wrapper that installs any missing packages "on the fly" before finally running LuaLaTeX.  It mostly works, but near the end, I have to run the command</p>
  
  <pre><code>sudo tlmgr install [string of packages]
  </code></pre>
  
  <p>and furthermore, because there's no guarantee the LaTeX editor will allow user input, I have to call a new terminal to do this in so they can enter their sudo password.</p>
  
  <p>I have mostly figured this out: either</p>
  
  <pre><code>process = subprocess.Popen(
      shlex.split('''x-terminal-emulator -t \'Installing new packages\' -e \'sudo tlmgr install ''' + packagesString + '''\''''), stdout=subprocess.PIPE)
  retcode = process.wait()
  </code></pre>
  
  <p>or</p>
  
  <pre><code>os.system('''x-terminal-emulator -t \'Installing new packages\' -e \'sudo tlmgr install ''' + packagesString + '''\'''')
  </code></pre>
  
  <p>The only problem is, this line does not wait until the spawned terminal process is done.  In fact, it continues to the next line (running the actual LuaLaTeX) immediately, before the user can even enter their password or download the packages!</p>
  
  <p>From what I understand, this is because the sudo child process finishes right away.  Is there a way to make sure the tlmgr process finishes before continuing?</p>