Coretex
_coretex.py
1 from pathlib import Path
2 
3 import sys
4 import logging
5 import runpy
6 
7 from coretex import _task
8 from coretex.networking import RequestFailedError
9 
10 
11 if __name__ == "__main__":
12  taskRun, callback = _task.processRemote(sys.argv)
13 
14  try:
15  _task._prepareForExecution(taskRun)
16  _task.current_task_run.setCurrentTaskRun(taskRun)
17 
18  callback.onStart()
19 
20  logging.getLogger("coretexpylib").info(">> [Coretex] TaskRun execution started")
21  logging.getLogger("coretexpylib").info(f"\tPython: {sys.executable}")
22 
23  entryPointDir = str(Path(taskRun.entryPoint).parent)
24  if entryPointDir not in sys.path:
25  sys.path.append(entryPointDir)
26 
27  _coretexPath = str(Path(__file__).resolve().parent)
28  if _coretexPath in sys.path:
29  sys.path.remove(_coretexPath)
30 
31  # Run the entry point script
32  runpy.run_path(taskRun.entryPoint, {}, "__main__")
33 
34  callback.onSuccess()
35  except RequestFailedError:
36  callback.onNetworkConnectionLost()
37  except KeyboardInterrupt:
38  callback.onKeyboardInterrupt()
39  except BaseException as ex:
40  callback.onException(ex)
41 
42  # sys.exit is ok here, finally block is guaranteed to execute
43  # due to how sys.exit is implemented (it internally raises SystemExit exception)
44  sys.exit(1)
45  finally:
46  callback.onCleanUp()