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/windows #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ dmypy.json

# Log history
*.log
*.log.*
*.log.*

# Storage volumes
volumes/
13 changes: 11 additions & 2 deletions drama/process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import tempfile

from abc import abstractmethod
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -62,7 +64,11 @@ def __init__(

# logging
self.logger = get_logger(__name__)
self.logging_file = tempfile.NamedTemporaryFile(dir=storage.temp_dir)
# Note that opening a NamedTemporaryFile multiple times will cause a
# PermissionError on Windows. To avoid this, one solution is to set the
# delete flag to False. This, however, requires to delete the file manually
# when it is closed. Base on: https://stackoverflow.com/questions/23212435.
self.logging_file = tempfile.NamedTemporaryFile(dir=storage.temp_dir, delete=False)

@abstractmethod
def to_downstream(self, data: DataType) -> Message:
Expand Down Expand Up @@ -302,8 +308,11 @@ def close(self, force_interruption: bool = False, remove_local_dir: bool = False
# send log to remote storage
logging_remote = self.storage.put_file(self.logging_file.name, rename=logging_filename)

# once uploaded, delete named temporal file in local temp dir
# once uploaded, delete named temporal file in local temp dir.
self.logging_file.close()
# Delete the temporary file explicitly since it was opened with the
# delete flag set to False.
os.unlink(self.logging_file.name)

if remove_local_dir:
# remove local directory without deleting the logging file (always kept for debugging purposes)
Expand Down
7 changes: 6 additions & 1 deletion drama/storage/backend/local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import shutil
from pathlib import Path
from typing import List, Optional, Union
Expand Down Expand Up @@ -27,7 +28,11 @@ def put_file(self, file_path: Union[str, Path], rename: Optional[str] = None) ->
return LocalResource(resource=str(file_path))

def get_file(self, data_file: str) -> str:
if not Path(data_file).is_file():
# Use os.path.isfile(data_file) instead of Path(data_file).is_file()
# if the data_file may contain a URL. On WIndows systems, the latter
# will raise a OSError: [WinError 123]. As an alternative, catch the
# OSError and raise a FileNotFoundError.
if not os.path.isfile(data_file):
raise FileNotFoundError()
return data_file

Expand Down
2 changes: 1 addition & 1 deletion drama/storage/backend/minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def put_file(self, file_path: Union[str, Path], rename: Optional[str] = None) ->

def get_file(self, data_file: str) -> str:
if not data_file.startswith("minio://"):
raise NotValidScheme("Object file prefix is invalid: expected `minio://`")
raise NotValidScheme(f"Object file prefix for '{data_file}' is invalid: expected `minio://`")

# remove scheme and deconstruct path
bucket_name, object_name = data_file[len("minio://") :].split("/", 1)
Expand Down