Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filemode of persisted crfsuite files #844

Merged
merged 5 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- Invalidate importlib caches after dynamically installing module [#838](https://github.com/snipsco/snips-nlu/pull/838)
- Automatically generate documentation for supported languages and builtin entities [#841](https://github.com/snipsco/snips-nlu/pull/841)
- Fix issue when cleaning up crfsuite files [#843](https://github.com/snipsco/snips-nlu/pull/843)
- Fix filemode of persisted crfsuite files [#844](https://github.com/snipsco/snips-nlu/pull/844)

## [0.20.0] - 2019-07-16
### Added
Expand Down
12 changes: 10 additions & 2 deletions snips_nlu/slot_filler/crf_slot_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import math
import os
import shutil
import tempfile
from builtins import range
Expand All @@ -30,6 +31,8 @@
from snips_nlu.slot_filler.feature_factory import CRFFeatureFactory
from snips_nlu.slot_filler.slot_filler import SlotFiller

CRF_MODEL_FILENAME = "model.crfsuite"

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -350,9 +353,14 @@ def persist(self, path):

crf_model_file = None
if self.crf_model is not None:
destination = path / Path(self.crf_model.modelfile.name).name
crf_model_file = CRF_MODEL_FILENAME
destination = path / crf_model_file
shutil.copy(self.crf_model.modelfile.name, str(destination))
crf_model_file = str(destination.name)
# On windows, permissions of crfsuite files are correct
if os.name == "posix":
umask = os.umask(0o022) # retrieve the system umask
os.umask(umask) # restore the sys umask to its original value
os.chmod(str(destination), 0o644 & ~umask)

model = {
"language_code": self.language,
Expand Down
30 changes: 26 additions & 4 deletions snips_nlu/tests/test_crf_slot_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from __future__ import unicode_literals

import io
import os
from builtins import range
from pathlib import Path
from unittest import skipIf

from mock import MagicMock, PropertyMock
from sklearn_crfsuite import CRF
Expand All @@ -17,7 +19,7 @@
from snips_nlu.preprocessing import tokenize, Token
from snips_nlu.result import unresolved_slot
from snips_nlu.slot_filler.crf_slot_filler import (
CRFSlotFiller, _ensure_safe, _encode_tag)
CRFSlotFiller, _ensure_safe, _encode_tag, CRF_MODEL_FILENAME)
from snips_nlu.slot_filler.crf_utils import TaggingScheme
from snips_nlu.slot_filler.feature_factory import (
IsDigitFactory, NgramFactory, ShapeNgramFactory)
Expand Down Expand Up @@ -513,8 +515,7 @@ def test_should_be_serializable(self):
metadata_path = self.tmp_file_path / "metadata.json"
self.assertJsonContent(metadata_path, {"unit_name": "crf_slot_filler"})

expected_crf_file = Path(slot_filler.crf_model.modelfile.name).name
self.assertTrue((self.tmp_file_path / expected_crf_file).exists())
self.assertTrue((self.tmp_file_path / CRF_MODEL_FILENAME).exists())

expected_feature_factories = [
{
Expand All @@ -532,7 +533,7 @@ def test_should_be_serializable(self):
tagging_scheme=TaggingScheme.BILOU,
feature_factory_configs=expected_feature_factories)
expected_slot_filler_dict = {
"crf_model_file": expected_crf_file,
"crf_model_file": CRF_MODEL_FILENAME,
"language_code": "en",
"config": expected_config.to_dict(),
"intent": intent,
Expand Down Expand Up @@ -1034,3 +1035,24 @@ def test_should_cleanup(self):

# Then
self.assertFalse(crf_file.exists())

@skipIf(os.name != "posix", "files permissions are different on windows")
def test_crfsuite_files_modes_should_be_644(self):
# Given
dataset_stream = io.StringIO("""
---
type: intent
name: MakeTea
utterances:
- make me a [beverage_temperature:Temperature](hot) cup of tea
- make me [number_of_cups:snips/number](five) tea cups""")
dataset = Dataset.from_yaml_files("en", [dataset_stream]).json

# When
slot_filler = CRFSlotFiller().fit(dataset, "MakeTea")
slot_filler.persist(self.tmp_file_path)

# Then
crfmodel_file = str(self.tmp_file_path / CRF_MODEL_FILENAME)
filemode = oct(os.stat(crfmodel_file).st_mode & 0o0777)
self.assertEqual(oct(0o644), filemode)