Commit c5a29e9e authored by Enrico Zini's avatar Enrico Zini
Browse files

Housekeeping backup works

parent 5b2bb688
......@@ -15,11 +15,6 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.utils.timezone import now
from django.conf import settings
import django_housekeeping as hk
......@@ -100,12 +95,9 @@ class BackupDB(hk.Task):
return
# Write the backup file
with utils.atomic_writer(fname, 0o640) as fd:
try:
gzfd = gzip.GzipFile(filename=fname[:-3], mode="w", compresslevel=9, fileobj=fd)
with utils.atomic_writer(fname, mode="wb", chmod=0o640) as fd:
with gzip.open(fd, mode="wt", compresslevel=9) as gzfd:
json.dump(people, gzfd, cls=Serializer, indent=2)
finally:
gzfd.close()
class ComputeActiveAM(hk.Task):
......@@ -437,7 +429,7 @@ class ProcmailIncludes(hk.Task):
return
# Generate AM list
with utils.atomic_writer(os.path.join(root, "am.include"), 0o600) as fd:
with utils.atomic_writer(os.path.join(root, "am.include"), mode="wt", chmod=0o600) as fd:
print("# Automatically generated by NM housekeeping at {} - do not edit".format(now()), file=fd)
for am in bmodels.AM.objects.all().select_related("person"):
if not (am.is_admin or am.is_am): continue
......@@ -445,7 +437,7 @@ class ProcmailIncludes(hk.Task):
print("! {}".format(am.person.email), file=fd)
# Generate NM CTTE list
with utils.atomic_writer(os.path.join(root, "nm-committee.include"), 0o600) as fd:
with utils.atomic_writer(os.path.join(root, "nm-committee.include"), mode="wt", chmod=0o600) as fd:
print("# Automatically generated by NM housekeeping at {} - do not edit".format(now()), file=fd)
for am in bmodels.AM.objects.all().select_related("person"):
if not (am.is_admin or am.is_am_ctte): continue
......
# coding: utf-8
# nm.debian.org website backend
#
# Copyright (C) 2012 Enrico Zini <enrico@debian.org>
......@@ -28,14 +26,15 @@ class atomic_writer(object):
"""
Atomically write to a file
"""
def __init__(self, fname, mode=0o664, sync=True):
def __init__(self, fname, mode="w+b", chmod=0o664, sync=True, **kw):
self.fname = fname
self.mode = mode
self.chmod = chmod
self.sync = sync
dirname = os.path.dirname(self.fname)
if not os.path.isdir(dirname):
os.makedirs(dirname)
self.outfd = tempfile.NamedTemporaryFile(dir=dirname)
self.fd, self.abspath = tempfile.mkstemp(dir=dirname, text="b" not in mode)
self.outfd = open(self.fd, mode, closefd=True, **kw)
def __enter__(self):
return self.outfd
......@@ -43,11 +42,11 @@ class atomic_writer(object):
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self.outfd.flush()
if self.sync:
os.fdatasync(self.outfd.fileno())
os.fchmod(self.outfd.fileno(), self.mode)
os.rename(self.outfd.name, self.fname)
self.outfd.delete = False
if self.sync: os.fdatasync(self.fd)
os.fchmod(self.fd, self.chmod)
os.rename(self.abspath, self.fname)
else:
os.unlink(self.abspath)
self.outfd.close()
return False
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment