Clean up perforce connection to use a context manager.

This change makes a long-awaited cleanup to the perforce connection logic,
using a context manager rather than an ugly except block. I've also moved the
import of P4Exception to hopefully work better across a range of systems.

Testing done:
Ran unit tests.

Fixes bug 3683.

Reviewed at https://reviews.reviewboard.org/r/6707/
Merge branch 'release-2.0.x'