utils: use builtin Python tempfile module

Reimplement tmp_file_pattern() and tmp_filename() using primitives from
Python's tempfile module.  Remove the tmp_dir() function and instead use
tempfile.gettempdir() to increase portability.  (tmp_dir() hard-coded
'/tmp' as a fallback, which doesn't work on Windows.)  Also change
tmp_filename() to use tempfile.mkstemp() to avoid (mostly theoretical)
race conditions in the generation of the temporary filename.

Signed-off-by: Daniel Harding <dharding@living180.net>
cmds: better temp file cleanup

Use try/finally blocks to ensure that files created by
utils.tmp_filename() are always removed even in case of an exception.

Signed-off-by: Daniel Harding <dharding@living180.net>
utils: fix tmp_file_pattern()'s pattern

tmp_file_pattern() was returning a regex-style pattern, which was then
being passed to glob.glob().  As a result, it did not match the paths it
was intended to.  Update tmp_file_pattern() to return a glob-style
pattern.

Signed-off-by: Daniel Harding <dharding@living180.net>
Merge pull request #400 from living180/temp_work

utils: better temp file handling

Signed-off-by: David Aguilar <davvid@gmail.com>