Backport PR #6102: Add newline if missing to end of script magic cell

The script magic %%cmd fails in IPython notebook if trailing newlines are omitted.

In IPython console, %%cmd behaves correctly:
```
In [1]: %%cmd
...
Backport PR #6114: update hmac signature comparison

see https://docs.python.org/2/library/hmac.html#hmac.compare_digest

pinging @rgbkrk

- [x] leave a comment in the source code about why we do this
Backport PR #6117: Remove / from route of TreeRedirectHandler.

When base_url is set, navigating to http://{url}/{base_url} returns 404

This is due to the TreeRedirectHandler only picking up http://{url}/{base_url}/ and the TrailingSlashHandler being set to {base_url}/.*/.

This change will cause http://{url}/{base_url} to correctly redirect to http://{url}/{base_url}/tree, as expected.
...
Backport PR #6120: Only allow iframe embedding on same origin.

This sets the iframe security policy by default to only allow same origin embedding of the notebook. This can be overridden by changing `headers['X-Frame-Options']`.
Backport PR #6134: remove rackcdn https workaround for mathjax cdn

cdn.mathjax.org works over https now
Backport PR #6195: Close handle on new temporary files before returning filename

On Windows, a file can only have one open writable handle at a time, so we need to close the one created by mkstemp before anything else can use that file.

Closes gh-5946