PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/site-packages/setuptools/depends.py

https://gitlab.com/areema/myproject
Python | 217 lines | 186 code | 9 blank | 22 comment | 9 complexity | 469b198e31c7c3349cea8a0163e35a60 MD5 | raw file
  1. import sys
  2. import imp
  3. import marshal
  4. from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN
  5. from distutils.version import StrictVersion
  6. from setuptools.extern import six
  7. __all__ = [
  8. 'Require', 'find_module', 'get_module_constant', 'extract_constant'
  9. ]
  10. class Require:
  11. """A prerequisite to building or installing a distribution"""
  12. def __init__(self, name, requested_version, module, homepage='',
  13. attribute=None, format=None):
  14. if format is None and requested_version is not None:
  15. format = StrictVersion
  16. if format is not None:
  17. requested_version = format(requested_version)
  18. if attribute is None:
  19. attribute = '__version__'
  20. self.__dict__.update(locals())
  21. del self.self
  22. def full_name(self):
  23. """Return full package/distribution name, w/version"""
  24. if self.requested_version is not None:
  25. return '%s-%s' % (self.name,self.requested_version)
  26. return self.name
  27. def version_ok(self, version):
  28. """Is 'version' sufficiently up-to-date?"""
  29. return self.attribute is None or self.format is None or \
  30. str(version) != "unknown" and version >= self.requested_version
  31. def get_version(self, paths=None, default="unknown"):
  32. """Get version number of installed module, 'None', or 'default'
  33. Search 'paths' for module. If not found, return 'None'. If found,
  34. return the extracted version attribute, or 'default' if no version
  35. attribute was specified, or the value cannot be determined without
  36. importing the module. The version is formatted according to the
  37. requirement's version format (if any), unless it is 'None' or the
  38. supplied 'default'.
  39. """
  40. if self.attribute is None:
  41. try:
  42. f,p,i = find_module(self.module,paths)
  43. if f: f.close()
  44. return default
  45. except ImportError:
  46. return None
  47. v = get_module_constant(self.module, self.attribute, default, paths)
  48. if v is not None and v is not default and self.format is not None:
  49. return self.format(v)
  50. return v
  51. def is_present(self, paths=None):
  52. """Return true if dependency is present on 'paths'"""
  53. return self.get_version(paths) is not None
  54. def is_current(self, paths=None):
  55. """Return true if dependency is present and up-to-date on 'paths'"""
  56. version = self.get_version(paths)
  57. if version is None:
  58. return False
  59. return self.version_ok(version)
  60. def _iter_code(code):
  61. """Yield '(op,arg)' pair for each operation in code object 'code'"""
  62. from array import array
  63. from dis import HAVE_ARGUMENT, EXTENDED_ARG
  64. bytes = array('b',code.co_code)
  65. eof = len(code.co_code)
  66. ptr = 0
  67. extended_arg = 0
  68. while ptr<eof:
  69. op = bytes[ptr]
  70. if op>=HAVE_ARGUMENT:
  71. arg = bytes[ptr+1] + bytes[ptr+2]*256 + extended_arg
  72. ptr += 3
  73. if op==EXTENDED_ARG:
  74. long_type = six.integer_types[-1]
  75. extended_arg = arg * long_type(65536)
  76. continue
  77. else:
  78. arg = None
  79. ptr += 1
  80. yield op,arg
  81. def find_module(module, paths=None):
  82. """Just like 'imp.find_module()', but with package support"""
  83. parts = module.split('.')
  84. while parts:
  85. part = parts.pop(0)
  86. f, path, (suffix,mode,kind) = info = imp.find_module(part, paths)
  87. if kind==PKG_DIRECTORY:
  88. parts = parts or ['__init__']
  89. paths = [path]
  90. elif parts:
  91. raise ImportError("Can't find %r in %s" % (parts,module))
  92. return info
  93. def get_module_constant(module, symbol, default=-1, paths=None):
  94. """Find 'module' by searching 'paths', and extract 'symbol'
  95. Return 'None' if 'module' does not exist on 'paths', or it does not define
  96. 'symbol'. If the module defines 'symbol' as a constant, return the
  97. constant. Otherwise, return 'default'."""
  98. try:
  99. f, path, (suffix, mode, kind) = find_module(module, paths)
  100. except ImportError:
  101. # Module doesn't exist
  102. return None
  103. try:
  104. if kind==PY_COMPILED:
  105. f.read(8) # skip magic & date
  106. code = marshal.load(f)
  107. elif kind==PY_FROZEN:
  108. code = imp.get_frozen_object(module)
  109. elif kind==PY_SOURCE:
  110. code = compile(f.read(), path, 'exec')
  111. else:
  112. # Not something we can parse; we'll have to import it. :(
  113. if module not in sys.modules:
  114. imp.load_module(module, f, path, (suffix, mode, kind))
  115. return getattr(sys.modules[module], symbol, None)
  116. finally:
  117. if f:
  118. f.close()
  119. return extract_constant(code, symbol, default)
  120. def extract_constant(code, symbol, default=-1):
  121. """Extract the constant value of 'symbol' from 'code'
  122. If the name 'symbol' is bound to a constant value by the Python code
  123. object 'code', return that value. If 'symbol' is bound to an expression,
  124. return 'default'. Otherwise, return 'None'.
  125. Return value is based on the first assignment to 'symbol'. 'symbol' must
  126. be a global, or at least a non-"fast" local in the code block. That is,
  127. only 'STORE_NAME' and 'STORE_GLOBAL' opcodes are checked, and 'symbol'
  128. must be present in 'code.co_names'.
  129. """
  130. if symbol not in code.co_names:
  131. # name's not there, can't possibly be an assigment
  132. return None
  133. name_idx = list(code.co_names).index(symbol)
  134. STORE_NAME = 90
  135. STORE_GLOBAL = 97
  136. LOAD_CONST = 100
  137. const = default
  138. for op, arg in _iter_code(code):
  139. if op==LOAD_CONST:
  140. const = code.co_consts[arg]
  141. elif arg==name_idx and (op==STORE_NAME or op==STORE_GLOBAL):
  142. return const
  143. else:
  144. const = default
  145. def _update_globals():
  146. """
  147. Patch the globals to remove the objects not available on some platforms.
  148. XXX it'd be better to test assertions about bytecode instead.
  149. """
  150. if not sys.platform.startswith('java') and sys.platform != 'cli':
  151. return
  152. incompatible = 'extract_constant', 'get_module_constant'
  153. for name in incompatible:
  154. del globals()[name]
  155. __all__.remove(name)
  156. _update_globals()