annotate atomic/writer.py @ 1:a4188f41ca35 default tip

basically the thing
author Jeff Hammel <k0scist@gmail.com>
date Fri, 02 Jan 2015 13:22:09 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 write a file atomically
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # imports
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import os
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import shutil
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import tempfile
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 # module globals
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 __all__ = ['ensure_dir', 'write']
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def ensure_dir(directory):
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 """ensure a directory exists"""
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 if os.path.exists(directory):
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 if not os.path.isdir(directory):
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 raise OSError("Not a directory: '{}'".format(directory))
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 return directory
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 os.makedirs(directory)
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 return directory
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 def write(contents, path):
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 """atomically write a file taking advantage of move"""
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 path = os.path.abspath(path)
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 fd, tmp_path = tempfile.mkstemp()
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 os.write(fd, contents)
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 os.close(fd)
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 ensure_dir(os.path.dirname(path))
a4188f41ca35 basically the thing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 shutil.move(tmp_path, path)