Skip to content

Commit

Permalink
[WIP] atomic write of mets.xml, fix #278 (#285)
Browse files Browse the repository at this point in the history
[WIP] atomic write of mets.xml, fix #278
  • Loading branch information
kba authored Aug 20, 2019
2 parents 8579409 + 39cce9f commit e678c0d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Versioned according to [Semantic Versioning](http://semver.org/).
Changed:

* workspace bagger will create files with extension
* `save_mets` is atomic now, #278, #285

## [1.0.0b15] - 2019-08-14

Expand Down
5 changes: 3 additions & 2 deletions ocrd/ocrd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cv2
from PIL import Image
import numpy as np
from atomicwrites import atomic_write

from ocrd_models import OcrdMets, OcrdExif
from ocrd_utils import getLogger, is_local_filename, abspath, pushd_popd
Expand Down Expand Up @@ -170,8 +171,8 @@ def save_mets(self):
log.info("Saving mets '%s'" % self.mets_target)
if self.automatic_backup:
WorkspaceBackupManager(self).add()
with open(self.mets_target, 'wb') as f:
f.write(self.mets.to_xml(xmllint=True))
with atomic_write(self.mets_target, overwrite=True) as f:
f.write(self.mets.to_xml(xmllint=True).decode('utf-8'))

def resolve_image_exif(self, image_url):
"""
Expand Down
6 changes: 4 additions & 2 deletions ocrd/ocrd/workspace_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from .constants import BACKUP_DIR

from atomicwrites import atomic_write

def _chksum(s):
return hashlib.sha256(s).hexdigest()

Expand Down Expand Up @@ -83,8 +85,8 @@ def add(self):
mets_file = join(d, 'mets.xml')
log.info("Backing up to %s" % mets_file)
makedirs(d)
with open(mets_file, 'wb') as f:
f.write(mets_str)
with atomic_write(mets_file, overwrite=True) as f:
f.write(mets_str.decode('utf-8'))
return chksum

def list(self):
Expand Down
1 change: 1 addition & 0 deletions ocrd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ opencv-python-headless
Flask
jsonschema
pyyaml
atomicwrites >= 1.3.0

0 comments on commit e678c0d

Please sign in to comment.