Avoid stuffing variables from top-level “grail” module into __main__
Replace some instances of mktemp() with NamedTemporaryFile()

* mktemp() deprecated since Python 2.3
* Other instances might require a temporary directory or spawning a process
  redirecting to a temporary file