[WIP] Groom migration scripts.

### Motivation
Fix issue #956.

### Changelog
* Use context managers aggressively.
* Groom imports
* Install a `main` function in every script.
* Fix processing of `sys.argv`.

### TODO:
* Use `argparse` instead of messing with `sys.argv` manually.
* Print an error message if neither `upgrade` or `downgrade` are given as commands.

@lolosk: Please review. If you like to do some of the `TODO` work, do a PR on my PR.