~smoser/cloud-init/azure_dhcp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
# vi: ts=4 expandtab

import tempfile
import os
import json


def atomic_write_file(path, content, mode='w'):
    tf = None
    try:
        tf = tempfile.NamedTemporaryFile(dir=os.path.dirname(path),
                                         delete=False, mode=mode)
        tf.write(content)
        tf.close()
        os.rename(tf.name, path)
    except Exception as e:
        if tf is not None:
            os.unlink(tf.name)
        raise e


def atomic_write_json(path, data):
    return atomic_write_file(path, json.dumps(data, indent=1,
                                              sort_keys=True) + "\n")