PyXMake Developer Guide: src/PyXMake/__init__.py Source File

| PyXMake Developer Guide 1.0 PyXMake

Loading...
Searching...
No Matches
__init__.py
1# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2# PyXMake %
3# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4"""
5PyXMake is a cross-plattform build tool for source files using either Intel Fortran,
6Py2X, a SSH connection or Doxygen to build a library or to automatically create a
7documentation.
8
9@note: PyXMake.__init__()
10Created on 20.03.2018
11
12@version: 1.0
13----------------------------------------------------------------------------------------------
14@requires:
15 -
16
17@change:
18 -
19
20@contributors:
21 Falk Heinecke
22 Sebastian Freund
23 Andreas Schuster
24
25@author: garb_ma [DLR-FA,STM Braunschweig]
26----------------------------------------------------------------------------------------------
27"""
28
29## @package PyXMake
30# PyXMake package for programmable build events on Linux and Windows.
31## @authors
32# Marc Garbade,
33# Falk Heinecke,
34# Sebastian Freund,
35# Andreas Schuster
36## @date
37# 20.03.2018
38## @par Notes/Changes
39# - Added documentation // mg 29.03.2018
40
41import os,sys,site
42import subprocess
43import itertools
44
45try:
46 import importlib.util
47 from importlib.util import spec_from_file_location as load_source
48except:
49 from imp import load_source #@Reimport
50
51## Provide canonical version identifiers
52__version__ = "1.19.2"
53__version_info__ = tuple(map(lambda n: int(n) if n.isdigit() else n,__version__.split(".")))
54
55## Absolute system path to PyXMake.
56PyXMakePath = os.path.dirname(os.path.abspath(__file__))
57
58## Get the current project name
59__project__ = os.path.basename(os.path.normpath(PyXMakePath))
60
61## Define all wild imports
62__all__ = [".".join([__project__,x]) for x in ("Plugin","VTL")]
63
64## Add additional path to environment variable
65if os.path.exists(os.path.join(sys.prefix,"conda-meta")) and not os.path.join(sys.prefix,"Library","bin") in os.getenv("PATH",""): # pragma: no cover
66 os.environ["PATH"] = os.pathsep.join([os.path.join(sys.prefix,"Library","bin"),os.getenv("PATH","")])
67
68## Detect CLI mode and set frozen state accordingly
69if sys.stdin and sys.stdin.isatty(): setattr(sys, "frozen", getattr(sys, "frozen", sys.stdin and sys.stdin.isatty()))
70
71## Silently install all dependencies during initial startup
72if (not os.path.exists(os.path.join(PyXMakePath,"VTL","examples")) or
73 not os.listdir(os.path.join(PyXMakePath,"VTL","examples")) or
74 not os.path.exists(os.path.join(PyXMakePath,"VTL","examples","abaqus.py"))) and not getattr(sys, 'frozen', False):
75 subprocess.check_call([sys.executable,os.path.join(PyXMakePath,"__setenv__.py"), os.path.join(PyXMakePath,"VTL","__install__.py")])
76
77## Backwards compatibility patch for process with activate poetry instance.
78# poetry uses its own version of typing extensions, even though the underlying system provides one.
79# This recipe will attempt to fix the issue by attempting to load the unsupported version first and
80# if this is successful - delete the module and reload the system one.
81if sys.version_info[0] >= 3:
82 # Attempt to import poetry. If poetry is not found, do nothing
83 try: from poetry.core._vendor import typing_extensions as _ #@UnresolvedImport #@UnusedImport
84 except ImportError: pass
85 else:
86 # We have an active poetry instance within the environment. Check version of typing
87 try: from typing_extensions import TypeAliasType as _ #@UnresolvedImport @UnusedImport @Reimport
88 except ImportError:
89 # Local import definition
90 import copy
91 # Create a local copy of system path
92 sys_path_backup = copy.deepcopy(sys.path)
93 try:
94 # Delete wrong module from system path
95 del sys.modules["typing_extensions"]
96 # Reorder all elements and remove all references to poetry's vendor path
97 sys.path = [x for x in sorted(set(sys_path_backup), key=lambda x: 'site-packages' in x) if not x.endswith("_vendor")]
98 # Verify that all import definitions work from here on
99 from typing_extensions import TypeAliasType as _#@UnresolvedImport @UnusedImport @Reimport
100 from pydantic import BaseModel as _ #@UnusedImport @Reimport
101 except: pass
102 # Reset local system path
103 finally: sys.path = sys_path_backup
104 else: pass
105
106## Register development extension (if installed)
107if not any(path in sys.modules for path in __all__) and not getattr(sys, "frozen",False):
108 for modname, sitedir in list(itertools.product(__all__,sorted(set(site.getsitepackages()+sys.path)))):
109 try:
110 # Only execute valid files. Skip all others.
111 expression = 'os.path.join(sitedir,*modname.split("."),"__init__.py")'
112 candidate = eval('lambda: ' + expression); candidate = candidate()
113 if not os.path.exists(candidate): continue
114 # Load the given source into current instance
115 expression = 'load_source(modname, os.path.join(sitedir,*modname.split("."),"__init__.py"))'
116 spec = eval('lambda: ' + expression); spec = spec()
117 # In later releases, module specification and loading is split.
118 module = importlib.util.module_from_spec(spec)
119 sys.modules[modname] = module
120 spec.loader.exec_module(module)
121 except: continue
122else: pass
123
124## Provide an alias for the VTL module for forwards compatibility
125# Fail gracefully for now
126try: from . import VTL as Command
127except: pass
128
129if __name__ == '__main__':
130 pass