Package sourceinfo :: Module bininfo

Source Code for Module sourceinfo.bininfo

  1  # -*- coding: utf-8 -*- 
  2  """pysourceinfo.bininfo - runtime information on compiled Python binaries. 
  3   
  4  Details see manuals. 
  5  """ 
  6  from __future__ import absolute_import 
  7  from __future__ import print_function 
  8   
  9  import os 
 10  import sys 
 11  import re 
 12  from inspect import stack, getmodule, isbuiltin, ismodule 
 13   
 14  from pythonids import PYV3 
 15   
 16   
 17  __author__ = 'Arno-Can Uestuensoez' 
 18  __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints" 
 19  __copyright__ = "Copyright (C) 2010-2017 Arno-Can Uestuensoez" \ 
 20                  " @Ingenieurbuero Arno-Can Uestuensoez" 
 21  __version__ = '0.1.34' 
 22  __uuid__ = '9de52399-7752-4633-9fdc-66c87a9200b8' 
 23   
 24  __docformat__ = "restructuredtext en" 
 25   
 26  #--- 
 27  # 
 28  # redundant for autonomous load-independence by reduction of dependencies 
 29  # 
30 -def __getpythonpath_rel(fpname, plist=None):
31 """Relative path name for first match on plist.""" 32 if not plist: 33 plist = sys.path 34 # for now dirs with terminating os.sep 35 _fp = os.path.normpath(os.path.abspath(fpname)) 36 _fp = re.escape(_fp) 37 for _sp in plist: 38 _sp = os.path.normpath(os.path.abspath(_sp)) 39 _sp = re.escape(_sp) 40 if _fp and _fp.startswith(_sp): 41 _r = _fp.replace(_sp, "") 42 if _r and _r[0:2] == re.escape(os.sep): 43 return re.sub(r'\\(.)', r'\1', _r[2:]) 44 elif _r and _r[0] == os.sep: 45 return re.sub(r'\\(.)', r'\1', _r[1:]) 46 if not _r: 47 return '.' 48 return re.sub(r'\\(.)', r'\1', _r)
49 #--- 50 51
52 -def getcaller_bin_filename(spos=1):
53 """Filename of binary caller module. 54 55 Args: 56 spos: 57 Caller position on the stack. 58 59 Returns: 60 Returns the filename. 61 62 Raises: 63 pass-through 64 65 """ 66 return os.path.basename(getcaller_bin_filepathname(spos))
67 68
69 -def getcaller_bin_filepathname(spos=1):
70 """File pathname of caller module. 71 72 Args: 73 spos: 74 Caller position on the stack. 75 76 Returns: 77 Returns the file pathname. 78 79 Raises: 80 pass-through 81 82 """ 83 _sf = stack() 84 if spos >= len(_sf): 85 return None 86 module = getmodule(_sf[spos][0]) 87 if not PYV3: 88 if module: 89 return os.path.abspath(module.__file__) # source 90 else: 91 # PEP 3147 -- PYC Repository Directories 92 ret = find_loader(module.__name__) #@UndefinedVariable 93 if ret and hasattr(ret, 'path'): 94 # check python native 95 cdir = os.path.dirname(ret.path) + os.path.sep + '__pycache__' 96 if os.path.exists(cdir): 97 mname = re.sub(r"^.*[.]", '', ret.name) 98 for xf in os.walk(cdir): 99 res = None 100 for xi in xf[2]: 101 if vers == re.sub( #@UndefinedVariable 102 mname + r".cpython-(3[0-9]).py[co]$", r'\1', xi): 103 res = cdir + os.path.sep + xi 104 if res[-1] == 'o': # prio over '.pyc' on '.pyo' 105 break 106 return res 107 else: 108 return ret.path 109 else: 110 if ret: 111 if hasattr(ret, 'name'): 112 fn = ret.get_filename(module.__name__) 113 # fd = ret.get_data(fn) 114 return fn 115 116 # FIXME: module.__name__ => 'baseOID' 117 if module.__name__ in sys.builtin_module_names: 118 return None 119 else: 120 bn = sys.modules[module.__name__].__file__ 121 if bn and os.path.exists(bn): 122 return bn
123
124 -def getcaller_bin_pathname(spos=1):
125 """pathname of caller source file. 126 127 Args: 128 spos: 129 Caller position on the stack. 130 131 Returns: 132 Returns the filename. 133 134 Raises: 135 passed through exceptions 136 137 """ 138 if not PYV3: 139 return os.path.dirname(getcaller_bin_filepathname(spos + 1)) 140 else: 141 _sf = stack() 142 module = getmodule(_sf[spos][0]) 143 if hasattr(module, '__file__'): 144 f = module.__file__ 145 return os.path.dirname(f)
146 # PEP 3147 -- PYC Repository Directories 147 148
149 -def getcaller_bin_pathname_rel(spos=1):
150 """Relative pathname to first matching package directory of caller. 151 Evaluates 'sys.path' first, else switches to 'inspect'. 152 153 Args: 154 spos: 155 Caller position on the stack. 156 157 Returns: 158 Returns the path name to the package. 159 160 Raises: 161 pass-through 162 163 """ 164 ax = os.path.normpath(getcaller_bin_filepathname( 165 spos + 1)) # TODO: check for normpath 166 for si in sys.path: 167 si = os.path.normpath(si) 168 if ax.startswith(si): 169 return re.sub(si + r"[/\\\\]*(.+?)[/\\\\]*[^/\\\\]+.py[oc]*$", r"\1", ax)
170 171
172 -def getcaller_bin_pathname_sub(spos=1):
173 """sub-pathname to first matching module directory of caller. 174 Evaluates 'sys.path' first, else switches to 'inspect'. 175 176 Args: 177 spos: 178 Caller position on the stack. 179 180 Returns: 181 Returns the path name to the package. 182 183 Raises: 184 passed through exceptions 185 186 """ 187 ax = getcaller_bin_pathname_rel(spos + 1) 188 return re.sub(r"[^/\\\\]*[/\\\\](.*)", r"\1", ax)
189 190
191 -def getmodule_bin_filename(mod):
192 """Basename of file for loaded module *mod*. 193 194 Args: 195 mod: 196 Reference to a loaded module. 197 198 Returns: 199 Returns the basename of the loaded module. 200 201 Raises: 202 passed through exceptions 203 204 """ 205 if mod and ismodule(mod) and not isbuiltin(mod): 206 if hasattr(mod, '__file__') and os.path.exists(mod.__file__): 207 return os.path.basename(mod.__file__)
208 209
210 -def getmodule_bin_filepathname(mod):
211 """File pathname of loaded module *mod*. 212 213 Args: 214 mod: 215 Reference to a loaded module. 216 217 Returns: 218 Returns the file pathname of the loaded module. 219 220 Raises: 221 pass-through 222 223 """ 224 if mod and ismodule(mod) and not isbuiltin(mod): 225 if hasattr(mod, '__file__') and os.path.exists(mod.__file__): 226 return os.path.abspath(os.path.normpath(mod.__file__))
227 228
229 -def getmodule_bin_pathname(mod):
230 """Path name of loaded module. 231 232 Args: 233 mod: 234 Reference to a loaded module. 235 236 Returns: 237 Returns the pathname of the loaded module. 238 239 Raises: 240 pass-through 241 242 """ 243 if mod and ismodule(mod) and not isbuiltin(mod): 244 if hasattr(mod, '__file__'): 245 return os.path.normpath(os.path.dirname(mod.__file__))
246 247
248 -def getmodule_bin_pathname_rel(mod, plist=None):
249 """Relative path name to PYTHONPATH for loaded module. 250 251 Args: 252 mod: 253 Reference to a loaded module. 254 255 Returns: 256 Returns the relative pathname of the loaded module. 257 258 Raises: 259 pass-through 260 261 """ 262 if mod and ismodule(mod) and not isbuiltin(mod): 263 if hasattr(mod, '__file__'): 264 return __getpythonpath_rel(mod.__file__, plist)
265 266
267 -def getmodule_bin_pathname_sub(mod, plist=None):
268 """Path name for loaded module, relative to package path. 269 270 Args: 271 mod: 272 Reference to a loaded module. 273 274 Returns: 275 Returns the sub pathname of the loaded module. 276 277 Raises: 278 passed through exceptions 279 280 """ 281 ax = getmodule_bin_pathname_rel(mod, plist) 282 if not PYV3: 283 return re.sub(r"[^/\\\\]*[/\\\\]", r"\1", ax) 284 else: 285 return re.sub(r"[^/\\\\]*[/\\\\]".encode('utf-8'), r"\1".encode('utf-8'), ax)
286