Skip to content

Commit

Permalink
wrap atomic_write preserving permissions of mets.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Oct 15, 2020
1 parent 56c4aa6 commit 1776547
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ocrd/ocrd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import cv2
from PIL import Image
import numpy as np
from atomicwrites import atomic_write
from deprecated.sphinx import deprecated

from ocrd_models import OcrdMets, OcrdExif, OcrdFile
from ocrd_models.ocrd_page import parse
from ocrd_utils import (
atomic_write,
getLogger,
image_from_polygon,
coordinates_of_segment,
Expand Down
4 changes: 1 addition & 3 deletions ocrd/ocrd/workspace_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import hashlib

from ocrd_models import OcrdMets
from ocrd_utils import getLogger
from ocrd_utils import getLogger, atomic_write

from .constants import BACKUP_DIR

from atomicwrites import atomic_write

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

Expand Down
1 change: 0 additions & 1 deletion ocrd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ opencv-python-headless
Flask
jsonschema
pyyaml
atomicwrites >= 1.3.0
Deprecated == 1.2.0
1 change: 1 addition & 0 deletions ocrd_utils/ocrd_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@

from .os import (
abspath,
atomic_write,
pushd_popd,
unzip_file_to_dir)

Expand Down
18 changes: 14 additions & 4 deletions ocrd_utils/ocrd_utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
'abspath',
'pushd_popd',
'unzip_file_to_dir',
'atomic_write',
]

from atomicwrites import atomic_write as atomic_write_
from tempfile import TemporaryDirectory
import contextlib
from os import getcwd, chdir
import os.path
from os import getcwd, chdir, stat, chmod
from os.path import exists, abspath as abspath_

from zipfile import ZipFile

Expand All @@ -22,7 +24,7 @@ def abspath(url):
"""
if url.startswith('file://'):
url = url[len('file://'):]
return os.path.abspath(url)
return abspath_(url)

@contextlib.contextmanager
def pushd_popd(newcwd=None, tempdir=False):
Expand Down Expand Up @@ -53,4 +55,12 @@ def unzip_file_to_dir(path_to_zip, output_directory):
z.extractall(output_directory)
z.close()


@contextlib.contextmanager
def atomic_write(fpath, overwrite=False):
if exists(fpath):
mode = stat(fpath).st_mode
else:
mode = 0o664
with atomic_write_(fpath, overwrite=True) as f:
yield f
chmod(fpath, mode)
1 change: 1 addition & 0 deletions ocrd_utils/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Pillow >= 7.2.0
# All current versions of TensorFlow require numpy < 1.19.0,
# so make sure that now newer version is installed here.
numpy >= 1.17.0, < 1.19.0
atomicwrites >= 1.3.0
18 changes: 16 additions & 2 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import walk
from os import walk, stat, chmod
from stat import filemode
from subprocess import run, PIPE
from os.path import join, exists, abspath, basename, dirname
from tempfile import TemporaryDirectory, mkdtemp
Expand All @@ -10,7 +11,7 @@
from tests.base import TestCase, assets, main, copy_of_directory

from ocrd_models.ocrd_page import parseString
from ocrd_utils import pushd_popd
from ocrd_utils import pushd_popd, initLogging
from ocrd.resolver import Resolver
from ocrd.workspace import Workspace

Expand Down Expand Up @@ -278,6 +279,19 @@ def test_image_from_page_basic(self):
img, info, exif = ws.image_from_page(pcgts.get_Page(), page_id='PHYS_0017')
self.assertEquals(info['features'], 'binarized,clipped')

def test_mets_permissions(self):
initLogging()
with TemporaryDirectory() as tempdir:
ws = self.resolver.workspace_from_nothing(tempdir)
ws.save_mets()
mets_path = join(ws.directory, 'mets.xml')
assert filemode(stat(mets_path).st_mode) == '-rw-rw-r--'
chmod(mets_path, 0o777)
ws.save_mets()
assert filemode(stat(mets_path).st_mode) == '-rwxrwxrwx'




if __name__ == '__main__':
main(__file__)

0 comments on commit 1776547

Please sign in to comment.