code: def open_file_for_write(filepath, mode=None):
      
      stream = StringIO()
      yield stream
      content = stream.getvalue()
  
      filepath = os.path.realpath(filepath)
      tmpdir = tmpdir_manager.mkdtemp()
      cache_filepath = os.path.join(tmpdir, os.path.basename(filepath))
  
      debug_print("Writing to %s (local cache of %s)", cache_filepath, filepath)
  
      with atomic_write(filepath, overwrite=True) as f:
          f.write(content)
  
      if mode is not None:
          os.chmod(filepath, mode)
  
      with open(cache_filepath, 'w') as f:
          f.write(content)
  
      file_cache[filepath] = cache_filepath
docs: Writes both to given filepath, and tmpdir location.
  
      This is to get around the problem with some NFS's where immediately reading
      a file that has just been written is problematic. Instead, any files that we
      write, we also write to /tmp, and reads of these files are redirected there.
  
      Args:
          filepath (str): File to write.
          mode (int): Same mode arg as you would pass to `os.chmod`.
  
      Yields:
          File-like object.