2022-01-07 15:58:23 -05:00
|
|
|
import datetime
|
2022-01-07 15:34:51 -05:00
|
|
|
import os
|
2022-02-02 12:59:56 -05:00
|
|
|
import shutil
|
2022-01-28 06:42:37 -05:00
|
|
|
from typing import List
|
2022-01-07 15:34:51 -05:00
|
|
|
|
|
|
|
from crc import app, session
|
|
|
|
from crc.api.common import ApiError
|
2022-01-28 06:42:37 -05:00
|
|
|
from crc.models.file import FileType, CONTENT_TYPES, File
|
2022-01-07 15:34:51 -05:00
|
|
|
|
|
|
|
from SpiffWorkflow.bpmn.parser.ValidationException import ValidationException
|
|
|
|
|
|
|
|
from lxml import etree
|
|
|
|
|
2022-02-04 13:51:39 -05:00
|
|
|
from crc.models.workflow import WorkflowSpecInfo
|
2022-02-02 12:59:56 -05:00
|
|
|
from crc.services.file_system_service import FileSystemService
|
2022-01-07 15:34:51 -05:00
|
|
|
|
2022-02-02 12:59:56 -05:00
|
|
|
|
|
|
|
class SpecFileService(FileSystemService):
|
2022-01-07 15:34:51 -05:00
|
|
|
|
2022-01-13 15:24:29 -05:00
|
|
|
"""We store spec files on the file system. This allows us to take advantage of Git for
|
|
|
|
syncing and versioning.
|
2022-01-28 06:42:37 -05:00
|
|
|
The files are stored in a directory whose path is determined by the category and spec names.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-04 13:51:39 -05:00
|
|
|
def get_files(workflow_spec: WorkflowSpecInfo, file_name=None, include_libraries=False) -> List[File]:
|
2022-01-28 06:42:37 -05:00
|
|
|
""" Returns all files associated with a workflow specification """
|
2022-02-02 12:59:56 -05:00
|
|
|
path = SpecFileService.workflow_path(workflow_spec)
|
|
|
|
files = SpecFileService._get_files(path, file_name)
|
2022-01-28 06:42:37 -05:00
|
|
|
if include_libraries:
|
2022-02-04 13:51:39 -05:00
|
|
|
for lib_name in workflow_spec.libraries:
|
|
|
|
lib_path = SpecFileService.library_path(lib_name)
|
2022-02-02 12:59:56 -05:00
|
|
|
files.extend(SpecFileService._get_files(lib_path, file_name))
|
2022-01-28 06:42:37 -05:00
|
|
|
return files
|
2022-01-13 15:24:29 -05:00
|
|
|
|
2022-01-28 06:42:37 -05:00
|
|
|
@staticmethod
|
2022-02-04 13:51:39 -05:00
|
|
|
def add_file(workflow_spec: WorkflowSpecInfo, file_name: str, binary_data: bytearray) -> File:
|
2022-01-28 06:42:37 -05:00
|
|
|
# Same as update
|
2022-02-02 12:59:56 -05:00
|
|
|
return SpecFileService.update_file(workflow_spec, file_name, binary_data)
|
2022-01-13 15:24:29 -05:00
|
|
|
|
2022-01-28 06:42:37 -05:00
|
|
|
@staticmethod
|
2022-02-04 13:51:39 -05:00
|
|
|
def update_file(workflow_spec: WorkflowSpecInfo, file_name: str, binary_data) -> File:
|
2022-01-28 06:42:37 -05:00
|
|
|
SpecFileService.assert_valid_file_name(file_name)
|
|
|
|
file_path = SpecFileService.file_path(workflow_spec, file_name)
|
|
|
|
SpecFileService.write_file_data_to_system(file_path, binary_data)
|
2022-02-02 12:59:56 -05:00
|
|
|
file = SpecFileService.to_file_object(file_name, file_path)
|
2022-02-02 13:30:54 -05:00
|
|
|
if file_name == workflow_spec.primary_file_name:
|
|
|
|
SpecFileService.set_primary_bpmn(workflow_spec, file_name, binary_data)
|
|
|
|
elif workflow_spec.primary_file_name is None and file.type == FileType.bpmn:
|
|
|
|
# If no primary process exists, make this pirmary process.
|
2022-01-28 06:42:37 -05:00
|
|
|
SpecFileService.set_primary_bpmn(workflow_spec, file_name, binary_data)
|
|
|
|
return file
|
2022-01-13 15:24:29 -05:00
|
|
|
|
2022-01-28 06:42:37 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_data(workflow_spec: WorkflowSpecModel, file_name: str):
|
|
|
|
file_path = SpecFileService.file_path(workflow_spec, file_name)
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
raise ApiError("unknown_file", f"So file found with name {file_name} in {workflow_spec.display_name}")
|
|
|
|
with open(file_path, 'rb') as f_handle:
|
|
|
|
spec_file_data = f_handle.read()
|
|
|
|
return spec_file_data
|
2022-01-13 15:24:29 -05:00
|
|
|
|
2022-01-28 06:42:37 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def file_path(spec: WorkflowSpecModel, file_name: str):
|
|
|
|
return os.path.join(SpecFileService.workflow_path(spec), file_name)
|
2022-01-13 15:24:29 -05:00
|
|
|
|
2022-01-11 15:30:22 -05:00
|
|
|
@staticmethod
|
2022-02-02 12:59:56 -05:00
|
|
|
def last_modified(spec: WorkflowSpecModel, file_name: str):
|
|
|
|
path = SpecFileService.file_path(spec, file_name)
|
|
|
|
return FileSystemService._last_modified(path)
|
2022-01-07 15:34:51 -05:00
|
|
|
|
2022-01-13 15:24:29 -05:00
|
|
|
@staticmethod
|
2022-01-28 06:42:37 -05:00
|
|
|
def delete_file(spec, file_name):
|
2022-02-02 12:59:56 -05:00
|
|
|
# Fixme: Remember to remove the lookup files when the spec file is removed.
|
|
|
|
# lookup_files = session.query(LookupFileModel).filter_by(file_model_id=file_id).all()
|
|
|
|
# for lf in lookup_files:
|
|
|
|
# session.query(LookupDataModel).filter_by(lookup_file_model_id=lf.id).delete()
|
|
|
|
# session.query(LookupFileModel).filter_by(id=lf.id).delete()
|
2022-01-28 06:42:37 -05:00
|
|
|
file_path = SpecFileService.file_path(spec, file_name)
|
2022-01-13 15:24:29 -05:00
|
|
|
os.remove(file_path)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-02 12:59:56 -05:00
|
|
|
def delete_all_files(spec):
|
|
|
|
dir_path = SpecFileService.workflow_path(spec)
|
|
|
|
if os.path.exists(dir_path):
|
|
|
|
shutil.rmtree(dir_path)
|
2022-01-20 13:05:58 -05:00
|
|
|
|