Created
April 5, 2016 23:25
-
-
Save rewitt1/8f6e2affbda32fe8959925152a54d598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _safe_append(filename, workspacedir, data): | |
"""Safely add data to a file to prevent corruption in the case of """ | |
"""power failure. """ | |
fd, tmp = tempfile.mkstemp(prefix='tmpfile', dir=workspacedir) | |
try: | |
# close it and open it again so that "with" can be used | |
os.close(fd) | |
shutil.copyfile(filename, tmp) | |
with open(tmp, 'a') as f: | |
f.write(data) | |
_atomic_copy(tmp, filename) | |
finally: | |
try: | |
os.remove(tmp) | |
except OSError: | |
pass | |
def _atomic_copy(old, new): | |
old = os.path.realpath(old) | |
olddir = os.path.dirname(old) | |
new = os.path.realpath(new) | |
with open(old, "r") as f: | |
fd = f.fileno() | |
os.fdatasync(fd) | |
# Remember first sync the file AND directory to make sure data | |
# is written out | |
fd = os.open(os.path.dirname(olddir), os.O_RDONLY) | |
os.fsync(fd) | |
os.close(fd) | |
# Rename should be atomic with respect to disk, yes all of this assumes | |
# linux and possibly non-network filesystems. | |
os.rename(old, new) | |
def _enable_devtool_inc(workspacedir, basepath): | |
""" Make sure devtool.inc gets added to local.conf """ | |
local_conf = os.path.join(basepath, 'conf', 'local.conf') | |
# Exit if already included | |
with open(local_conf, 'r') as f: | |
for line in f: | |
if re.match(r'^\s*include\s+devtool.inc\s*$', line): | |
logger.info("devtool.inc already included skipping") | |
return | |
_safe_append(local_conf, workspacedir, "include devtool.inc\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment