diff options
author | Suchita Lad | 2024-06-12 17:43:56 +0530 |
---|---|---|
committer | Suchita Lad | 2024-06-12 17:43:56 +0530 |
commit | d50b4920e2626844328dbc9578fb813b798cc3f8 (patch) | |
tree | 5663278e520fff94122ae914455d84979b2846f2 | |
parent | c43bbaf6929ada364c0def31378c9256d35ef721 (diff) | |
download | Common-Interface-Project-d50b4920e2626844328dbc9578fb813b798cc3f8.tar.gz Common-Interface-Project-d50b4920e2626844328dbc9578fb813b798cc3f8.tar.bz2 Common-Interface-Project-d50b4920e2626844328dbc9578fb813b798cc3f8.zip |
Updated files of simulationAPI
-rw-r--r-- | blocks/simulationAPI/helpers/ngspice_helper.py | 69 | ||||
-rw-r--r-- | blocks/simulationAPI/urls.py | 3 | ||||
-rw-r--r-- | blocks/simulationAPI/views.py | 38 |
3 files changed, 89 insertions, 21 deletions
diff --git a/blocks/simulationAPI/helpers/ngspice_helper.py b/blocks/simulationAPI/helpers/ngspice_helper.py index 85db6c0f..2f9a0ad7 100644 --- a/blocks/simulationAPI/helpers/ngspice_helper.py +++ b/blocks/simulationAPI/helpers/ngspice_helper.py @@ -37,29 +37,56 @@ class CannotRunParser(Exception): """ Base class for exceptions in this module. """ +def CreateXml(file_path, parameters, file_id): + # file_path = file_obj.file.path + # print(file_obj) + # print('*********') + parameters = json.loads(parameters) + current_dir = settings.MEDIA_ROOT+'/'+str(file_id) + # Make Unique Directory for simulation to run + Path(current_dir).mkdir(parents=True, exist_ok=True) + (xcosfilebase, __) = os.path.splitext(file_path) + xcosfile = xcosfilebase + '.xcos' + logger.info('will run %s %s', 'MxGraphParser', file_path) + proc = subprocess.Popen([MxGraphParser, file_path], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + cwd=current_dir) + (stdout, stderr) = proc.communicate() + + if proc.returncode != 0: + logger.error('%s error encountered', 'MxGraphParser') + logger.error(stderr) + logger.error(proc.returncode) + logger.error(stdout) + raise CannotRunParser('exited with error') + + logger.info('Ran %s', 'MxGraphParser') + return current_dir, xcosfile, file_path + +def CreateXcos(file_path, parameters, file_id): + print('hello') + try: + current_dir, xcosfile, file_path = CreateXml(file_path, parameters, file_id) + print(xcosfile) + return xcosfile + except BaseException as e: + logger.exception('Encountered Exception:') + logger.info('removing %s', file_path) + os.remove(file_path) + target = os.listdir(current_dir) + for item in target: + logger.info('removing %s', item) + os.remove(os.path.join(current_dir, item)) + logger.info('removing %s', current_dir) + os.rmdir(current_dir) + logger.info('Deleted Files') + raise e + + def ExecXml(file_obj): + print('hellooo', file_obj) try: - file_path = file_obj.file.path - parameters = json.loads(file_obj.parameters) - current_dir = settings.MEDIA_ROOT+'/'+str(file_obj.file_id) - # Make Unique Directory for simulation to run - Path(current_dir).mkdir(parents=True, exist_ok=True) - (xcosfilebase, __) = os.path.splitext(file_path) - xcosfile = xcosfilebase + '.xcos' - logger.info('will run %s %s', 'MxGraphParser', file_path) - proc = subprocess.Popen([MxGraphParser, file_path], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - cwd=current_dir) - (stdout, stderr) = proc.communicate() - - if proc.returncode != 0: - logger.error('%s error encountered', 'MxGraphParser') - logger.error(stderr) - logger.error(proc.returncode) - logger.error(stdout) - raise CannotRunParser('exited with error') - - logger.info('Ran %s', 'MxGraphParser') + current_dir, xcosfile, file_path = CreateXml(file_obj.file.path, file_obj.parameters, file_obj.file_id) (logfilefd, log_name) = mkstemp(prefix=datetime.now().strftime( 'scilab-log-%Y%m%d-'), suffix='.txt', dir=current_dir) diff --git a/blocks/simulationAPI/urls.py b/blocks/simulationAPI/urls.py index f19eddc1..d6117bb6 100644 --- a/blocks/simulationAPI/urls.py +++ b/blocks/simulationAPI/urls.py @@ -6,6 +6,9 @@ urlpatterns = [ path('upload', simulationAPI_views.XmlUploader.as_view(), name='xmlUploader'), + path('save', simulationAPI_views.XmlSave.as_view(), + name='xmlSave'), + path('status/<uuid:task_id>', simulationAPI_views.CeleryResultView.as_view(), name='celery_status'), diff --git a/blocks/simulationAPI/views.py b/blocks/simulationAPI/views.py index f593da80..6daa691c 100644 --- a/blocks/simulationAPI/views.py +++ b/blocks/simulationAPI/views.py @@ -1,4 +1,5 @@ import os +from django.conf import settings import time import uuid from celery.result import AsyncResult @@ -15,6 +16,7 @@ from simulationAPI.models import TaskFile from simulationAPI.negotiation import IgnoreClientContentNegotiation from simulationAPI.serializers import TaskSerializer from simulationAPI.tasks import process_task +from simulationAPI.helpers.ngspice_helper import CreateXcos SCILAB_INSTANCE_TIMEOUT_INTERVAL = 300 @@ -59,6 +61,42 @@ class XmlUploader(APIView): return Response(response_data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +class XmlSave(APIView): + ''' + API for XmlSave + + Requires a multipart/form-data POST Request with Xml file in the + 'file' parameter + ''' + permission_classes = (AllowAny,) + parser_classes = (MultiPartParser, FormParser,) + + def post(self, request, *args, **kwargs): + logger.info('Got POST for Xml save: data=%s', request.data) + file = request.FILES.get('file', None) + if not file: + return Response({"error": "No file provided"}, status=status.HTTP_400_BAD_REQUEST) + file_name = file.name + file_path = os.path.join(settings.MEDIA_ROOT, 'uploads', file_name) + + # Ensure the directory exists + os.makedirs(os.path.dirname(file_path), exist_ok=True) + + with open(file_path, 'wb+') as destination: + for chunk in file.chunks(): + destination.write(chunk) + + # Update the request data to include the file path + data = request.data.copy() + data['file_path'] = file_path + filename = CreateXcos(data['file_path'], '{}', 'abcd') + with open(filename, 'r') as file: + filecontent = file.read() + + response = Response(filecontent, status=status.HTTP_200_OK, content_type='application/octet-stream') + response['Content-Disposition'] = f'attachment; filename="{os.path.basename(filename)}"' + return response class CeleryResultView(APIView): |