Updates to api.py

This fixes a number of issues with api.py:

* File locking doesn't work with NFS
* megacrond throws IOError when file doesn't exist
* Potential race condition when creating directory
* 'db.p~' not found error
* merge all read/write functions into one context manager

With this commit the project seems to work correctly with the shared file system on the three wmflabs servers, although I have not tested it extensively. I will leave it installed there so you can test it out if you like.