PyXMake Developer Guide: D:/03_Workspaces/01_Eclipse/pyx_core/PyXMake/VTL/api.py Source File

| PyXMake Developer Guide 1.0 PyXMake

D:/03_Workspaces/01_Eclipse/pyx_core/PyXMake/VTL/api.py
1 # -*- coding: utf-8 -*-
2 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 # % PyXMake - Build environment for PyXMake %
4 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 """
6 API setup example for PyXMake.
7
8 @note: Run HTML APIs of PyXMake (in a Docker container or locally).
9 Created on 28.02.2020
10
11 @version: 1.0
12 ----------------------------------------------------------------------------------------------
13 @requires:
14 - PyXMake
15
16 @change:
17 -
18
19 @author: garb_ma [DLR-FA,STM Braunschweig]
20 ----------------------------------------------------------------------------------------------
21 """
22 import os, sys
23 import platform
24 import uvicorn
25
26 from fastapi import FastAPI, File, UploadFile, Query
27 from starlette.responses import RedirectResponse, FileResponse
28 from starlette.staticfiles import StaticFiles
29 from starlette.requests import Request
30 from typing import List
31
32 try:
33 import PyXMake
34 except ImportError:
35 # Script is executed as a plug-in
36 sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
37 import PyXMake
38 finally:
39 from PyXMake.Tools import Utility #@UnresolvedImport
40 from PyXMake import VTL #@UnresolvedImport
41
42 # Some immutable variables
43 __pyx_api_delimn = "/"
44 __pyx_doc_path = os.path.normpath(os.path.join(os.path.dirname(VTL.__file__),"doc","pyx_core","html"))
45 __pyx_url_path = __pyx_api_delimn.join(["https:","","fa-jenkins:8080","job","STM_Routines_ShortTest",
46 "Main_20Documentation","_subDocus","Tools_Utilities",str(PyXMake.__name__),
47 "documentation.html"])
48 # These are the captions for the Swagger UI
49 __pyx_guide = "Guide"
50 __pyx_interface = "Interface"
51
52 # Create a new API for PyXMake
53 app = FastAPI(title="PyXMake API",
54 version ="1.0",
55 description ="PyXMake is a python based make tool for daily software builds. This comprises daily library compilation for Python using f2py \
56 (currently MCODAC, BoxBeam & Beos), daily updated html documentations using Doxygen (currently MCODAC, BoxBeam, PyXMake & PyCODAC) \
57 and builds of shared & static libraries using Intel Fortran (currently MCODAC, BoxBeam). ",
58 docs_url = __pyx_api_delimn.join(["",str(PyXMake.__name__),"api","documentation"]))
59
60 # Mount static HTML files created by DoxyGen to created web application
61 app.mount(__pyx_api_delimn.join(["",str(PyXMake.__name__),"dev","documentation"]), StaticFiles(directory=__pyx_doc_path, html=True))
62
63 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 # % PyXMake - Guide %
65 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 @app.get(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","documentation"]), tags=[str(__pyx_guide)])
67 def api_self_guide():
68 """
69 Method pointing to the main documentation of the API.
70 """
71 return RedirectResponse(url=__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","documentation",""]))
72
73 @app.get(__pyx_api_delimn.join(["",str(PyXMake.__name__),"user","documentation"]), tags=[str(__pyx_guide)])
74 def api_user_guide():
75 """
76 Method pointing to the user documentation of the underlying package.
77 """
78 return RedirectResponse(url=__pyx_url_path)
79
80 @app.get(__pyx_api_delimn.join(["",str(PyXMake.__name__),"dev","documentation"]), tags=[str(__pyx_guide)])
81 def api_dev_guide():
82 """
83 Method pointing to the developer documentation of the underlying package.
84 """
85 return RedirectResponse(url=__pyx_api_delimn.join(["",str(PyXMake.__name__),"dev","documentation",""]))
86
87 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 # % PyXMake - Interface %
89 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","abaqus","{zip}"]), tags=[str(__pyx_interface)])
91 def api_abaqus(BuildID: str, Source: List[str] = Query("mcd_astandard"), ZIP: UploadFile = File(...)):
92 """
93 API for PyXMake to create an ABAQUS compatible Fortran library for Windows machines by using the Intel Fortran Compiler.
94 """
95 # Check if an old file is present in the current workspace. Delete it.
96 if os.path.exists(os.path.join(VTL.Scratch,ZIP.filename)):
97 os.remove(os.path.join(VTL.Scratch,ZIP.filename))
98 # Everything is done within a temporary directory. Update the uploaded ZIP folder with new content. Delete the input.
99 with Utility.TemporaryDirectory(VTL.Scratch), Utility.UpdateZIP(ZIP.filename, ZIP.file, VTL.Scratch, update=False):
100 # Get all relevant uploaded files.
101 files = Utility.FileWalk(Source, path=os.getcwd())
102 # Monitor the build process. Everything is returned to a file
103 with Utility.FileOutput('result.log'):
104 VTL.abaqus(BuildID, files, source=os.getcwd(), scratch=os.getcwd(), output=os.getcwd(), verbosity=2)
105 # Present result to FAST API
106 return FileResponse(path=os.path.join(VTL.Scratch,ZIP.filename),filename=ZIP.filename)
107
108 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","cxx","{zip}"]), tags=[str(__pyx_interface)])
109 def api_cxx(string_to_channel_through: str) -> dict:
110 """
111 Dummy function for showing a quick response
112 """
113 # from PyXMake.VTL import cxx
114 string = "hello"
115 return {"return_code": string}
116
117 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","doxygen","{zip}"]), tags=[str(__pyx_interface)])
118 def api_doxygen(string_to_channel_through: str) -> dict:
119 """
120 Dummy function for showing a quick response
121 """
122 # from PyXMake.VTL import doxygen
123 string = "hello"
124 return {"return_code": string}
125
126 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","ifort","{zip}"]), tags=[str(__pyx_interface)])
127 def api_ifort(BuildID: str, Source: List[str] = Query([x for x in VTL.GetSourceCode(0)]), ZIP: UploadFile = File(...)):
128 """
129 API for PyXMake to create a static library for Windows machines by using the Intel Fortran Compiler.
130 """
131 # Check if an old file is present in the current workspace. Delete it.
132 if os.path.exists(os.path.join(VTL.Scratch,ZIP.filename)):
133 os.remove(os.path.join(VTL.Scratch,ZIP.filename))
134 # Everything is done within a temporary directory. Update the uploaded ZIP folder with new content. Delete the input.
135 with Utility.TemporaryDirectory(VTL.Scratch), Utility.UpdateZIP(ZIP.filename, ZIP.file, VTL.Scratch, update=False):
136 # Get all relevant uploaded files.
137 files = Utility.FileWalk(Source, path=os.getcwd())
138 # Create two dummy directories to avoid problems during build event.
139 os.makedirs("include",exist_ok=True); os.makedirs("lib",exist_ok=True)
140 # Monitor the build process. Everything is returned to a file
141 with Utility.FileOutput('result.log'):
142 VTL.ifort(BuildID, files, source=os.getcwd(), scratch=os.getcwd(), make=os.getcwd(), verbosity=2)
143 # Present result to FAST API
144 return FileResponse(path=os.path.join(VTL.Scratch,ZIP.filename),filename=ZIP.filename)
145
146 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","java","{zip}"]), tags=[str(__pyx_interface)])
147 def api_java(BuildID: str, Source: List[str] = Query([x for x in VTL.GetSourceCode(0)]), ZIP: UploadFile = File(...)):
148 """
149 API for PyXMake to create a Java compatible Fortran library for Windows machines by using the Intel Fortran Compiler.
150 """
151 # Check if an old file is present in the current workspace. Delete it.
152 if os.path.exists(os.path.join(VTL.Scratch,ZIP.filename)):
153 os.remove(os.path.join(VTL.Scratch,ZIP.filename))
154 # Everything is done within a temporary directory. Update the uploaded ZIP folder with new content. Delete the input.
155 with Utility.TemporaryDirectory(VTL.Scratch), Utility.UpdateZIP(ZIP.filename, ZIP.file, VTL.Scratch, update=False):
156 # Get all relevant uploaded files.
157 files = Utility.FileWalk(Source, path=os.getcwd())
158 # Monitor the build process. Everything is returned to a file
159 with Utility.FileOutput('result.log'):
160 VTL.java(BuildID, files, source=os.getcwd(), scratch=os.getcwd(), output=os.getcwd(), verbosity=2)
161 # Present result to FAST API
162 return FileResponse(path=os.path.join(VTL.Scratch,ZIP.filename),filename=ZIP.filename)
163
164 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","f2py","{zip}"]), tags=[str(__pyx_interface)])
165 def api_py2x(BuildID: str, Source: List[str] = Query([x for x in VTL.GetSourceCode(0)]), ZIP: UploadFile = File(...)):
166 """
167 API for PyXMake to create a Python compatible Fortran library for Windows machines by using the Intel Fortran Compiler.
168 """
169 # Check if an old file is present in the current workspace. Delete it.
170 if os.path.exists(os.path.join(VTL.Scratch,ZIP.filename)):
171 os.remove(os.path.join(VTL.Scratch,ZIP.filename))
172 # Everything is done within a temporary directory. Update the uploaded ZIP folder with new content. Delete the input.
173 with Utility.TemporaryDirectory(VTL.Scratch), Utility.UpdateZIP(ZIP.filename, ZIP.file, VTL.Scratch, update=False):
174 # Get all relevant uploaded files.
175 files = Utility.FileWalk(Source, path=os.getcwd())
176 # Monitor the build process. Everything is returned to a file
177 with Utility.FileOutput('result.log'):
178 VTL.py2x(BuildID, files, source=os.getcwd(), scratch=os.getcwd(), output=os.getcwd(), verbosity=2)
179 # Present result to FAST API
180 return FileResponse(path=os.path.join(VTL.Scratch,ZIP.filename),filename=ZIP.filename)
181
182 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","ssh_f2py","{zip}"]), tags=[str(__pyx_interface)])
183 def api_ssh_f2py(time_sleep: int) -> dict:
184 """
185 Dummy function for showing a long response time
186 """
187 # from PyXMake.VTL import ssh_f2py
188 return {"return_code":str(time_sleep)}
189
190 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","ssh_ifort","{zip}"]), tags=[str(__pyx_interface)])
191 def api_ssh_ifort(time_sleep: int) -> dict:
192 """
193 Dummy function for showing a long response time
194 """
195 # from PyXMake.VTL import ssh_ifort
196 return {"return_code":str(time_sleep)}
197
198 @app.patch(__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","ssh_make","{zip}"]), tags=[str(__pyx_interface)])
199 def api_ssh_make(time_sleep: int) -> dict:
200 """
201 Dummy function for showing a long response time
202 """
203 # from PyXMake.VTL import ssh_make
204 return {"return_code":str(time_sleep)}
205
206 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 # % PyXMake - ErrorHandling %
208 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 @app.get(__pyx_api_delimn.join(["",".*"]),include_in_schema=False)
210 def invalid_request(request: Request) -> None:
211 """
212 Method redirecting invalid responses back to the main window.
213 """
214 return RedirectResponse(url=__pyx_api_delimn.join(["",str(PyXMake.__name__),"api","documentation"]))
215
216 def main(Hostname=str(platform.node()), PortID=8020):
217 """
218 Importable main function of current API.
219 """
220 uvicorn.run(app, host=Hostname, port=PortID)
221
222 if __name__ == '__main__':
223 """
224 API is initialized and run.
225 """
226 main(); sys.exit()
PyXMake.VTL.api.api_user_guide
def api_user_guide()
Definition: api.py:74
PyXMake.Tools
Module containing basic functionalities defined for convenience.
Definition: __init__.py:1
PyXMake.VTL.api.api_dev_guide
def api_dev_guide()
Definition: api.py:81
PyXMake.VTL.api.main
def main(Hostname=str(platform.node()), PortID=8020)
Definition: api.py:216
PyXMake.VTL.api.api_self_guide
def api_self_guide()
Definition: api.py:67