Skip to content
Closed
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pydantic==1.10.4
python-ms-core==0.0.22
uvicorn==0.20.0
html_testRunner==1.2.1
python-osw-validation==0.2.4
python-osw-validation==0.2.6
3 changes: 0 additions & 3 deletions src/osw_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import uuid
import logging
import datetime
import urllib.parse
from typing import List
from python_ms_core import Core
Expand Down Expand Up @@ -90,7 +88,6 @@ def send_status(self, result: ValidationResult, upload_message: Upload):
except Exception as e:
logger.error(f'Error occurred while publishing message for : {upload_message.message_id} with error: {e}')


def has_permission(self, roles: List[str], queue_message: Upload) -> bool:
try:
permission_request = PermissionRequest(
Expand Down
36 changes: 20 additions & 16 deletions src/validation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os
import uuid
import time
import shutil
import logging
import traceback
from pathlib import Path
from .config import Settings
from python_osw_validation import OSWValidation
from .models.queue_message_content import ValidationResult
import uuid

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# Path used for download file generation.
DOWNLOAD_FILE_PATH = f'{Path.cwd()}/downloads'
DOWNLOAD_DIR = f'{Path.cwd()}/downloads'

logging.basicConfig()
logger = logging.getLogger('OSW_VALIDATION')
Expand All @@ -25,11 +26,22 @@ def __init__(self, file_path=None, storage_client=None):
self.file_path = file_path
self.file_relative_path = file_path.split('/')[-1]
self.client = self.storage_client.get_container(container_name=self.container_name)
is_exists = os.path.exists(DOWNLOAD_DIR)
unique_id = self.get_unique_id()
if not is_exists:
os.makedirs(DOWNLOAD_DIR)
self.unique_dir_path = os.path.join(DOWNLOAD_DIR, unique_id)
if not os.path.exists(self.unique_dir_path):
os.makedirs(self.unique_dir_path)

def validate(self, max_errors=20) -> ValidationResult:
return self.is_osw_valid(max_errors)
try:
return self.is_osw_valid(max_errors)
finally:
Validation.clean_up(self.unique_dir_path)

def is_osw_valid(self, max_errors) -> ValidationResult:
start_time = time.time()
result = ValidationResult()
result.is_valid = False
result.validation_message = ''
Expand All @@ -47,24 +59,19 @@ def is_osw_valid(self, max_errors) -> ValidationResult:
else:
result.validation_message = 'Failed to validate because unknown file format'
logger.error(f' Failed to validate because unknown file format')

end_time = time.time()
time_taken = end_time - start_time
logger.info(f'Validation completed in {time_taken} seconds')
return result

# Downloads the single file into a unique directory
def download_single_file(self, file_upload_path=None) -> str:
is_exists = os.path.exists(DOWNLOAD_FILE_PATH)
unique_id = self.get_unique_id()
if not is_exists:
os.makedirs(DOWNLOAD_FILE_PATH)
unique_directory = os.path.join(DOWNLOAD_FILE_PATH,unique_id)
if not os.path.exists(unique_directory):
os.makedirs(unique_directory)


file = self.storage_client.get_file_from_url(self.container_name, file_upload_path)
try:
if file.file_path:
file_path = os.path.basename(file.file_path)
local_download_path = os.path.join(unique_directory,file_path)
local_download_path = os.path.join(self.unique_dir_path, file_path)
with open(local_download_path, 'wb') as blob:
blob.write(file.get_stream())
logger.info(f' File downloaded to location: {local_download_path}')
Expand All @@ -80,14 +87,11 @@ def get_unique_id(self) -> str:
unique_id = uuid.uuid1().hex[0:24]
return unique_id



@staticmethod
def clean_up(path):
if os.path.isfile(path):
logger.info(f' Removing File: {path}')
os.remove(path)
else:
# folder = os.path.join(DOWNLOAD_FILE_PATH, path)
logger.info(f' Removing Folder: {path}')
shutil.rmtree(path, ignore_errors=False)