Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runtime.py 5.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- test-case-name: twisted.python.test.test_runtime -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. __all__ = [
  5. "seconds",
  6. "shortPythonVersion",
  7. "Platform",
  8. "platform",
  9. "platformType",
  10. ]
  11. import os
  12. import sys
  13. import warnings
  14. from time import time as seconds
  15. from typing import Optional
  16. def shortPythonVersion() -> str:
  17. """
  18. Returns the Python version as a dot-separated string.
  19. """
  20. return "%s.%s.%s" % sys.version_info[:3]
  21. knownPlatforms = {
  22. "nt": "win32",
  23. "ce": "win32",
  24. "posix": "posix",
  25. "java": "java",
  26. "org.python.modules.os": "java",
  27. }
  28. class Platform:
  29. """
  30. Gives us information about the platform we're running on.
  31. """
  32. type: Optional[str] = knownPlatforms.get(os.name)
  33. seconds = staticmethod(seconds)
  34. _platform = sys.platform
  35. def __init__(
  36. self, name: Optional[str] = None, platform: Optional[str] = None
  37. ) -> None:
  38. if name is not None:
  39. self.type = knownPlatforms.get(name)
  40. if platform is not None:
  41. self._platform = platform
  42. def isKnown(self) -> bool:
  43. """
  44. Do we know about this platform?
  45. @return: Boolean indicating whether this is a known platform or not.
  46. """
  47. return self.type != None
  48. def getType(self) -> Optional[str]:
  49. """
  50. Get platform type.
  51. @return: Either 'posix', 'win32' or 'java'
  52. """
  53. return self.type
  54. def isMacOSX(self) -> bool:
  55. """
  56. Check if current platform is macOS.
  57. @return: C{True} if the current platform has been detected as macOS.
  58. """
  59. return self._platform == "darwin"
  60. def isWinNT(self) -> bool:
  61. """
  62. Are we running in Windows NT?
  63. This is deprecated and always returns C{True} on win32 because
  64. Twisted only supports Windows NT-derived platforms at this point.
  65. @return: C{True} if the current platform has been detected as
  66. Windows NT.
  67. """
  68. warnings.warn(
  69. "twisted.python.runtime.Platform.isWinNT was deprecated in "
  70. "Twisted 13.0. Use Platform.isWindows instead.",
  71. DeprecationWarning,
  72. stacklevel=2,
  73. )
  74. return self.isWindows()
  75. def isWindows(self) -> bool:
  76. """
  77. Are we running in Windows?
  78. @return: C{True} if the current platform has been detected as
  79. Windows.
  80. """
  81. return self.getType() == "win32"
  82. def isVista(self) -> bool:
  83. """
  84. Check if current platform is Windows Vista or Windows Server 2008.
  85. @return: C{True} if the current platform has been detected as Vista
  86. """
  87. return sys.platform == "win32" and sys.getwindowsversion().major == 6
  88. def isLinux(self) -> bool:
  89. """
  90. Check if current platform is Linux.
  91. @return: C{True} if the current platform has been detected as Linux.
  92. """
  93. return self._platform.startswith("linux")
  94. def isDocker(self, _initCGroupLocation: str = "/proc/1/cgroup") -> bool:
  95. """
  96. Check if the current platform is Linux in a Docker container.
  97. @return: C{True} if the current platform has been detected as Linux
  98. inside a Docker container.
  99. """
  100. if not self.isLinux():
  101. return False
  102. from twisted.python.filepath import FilePath
  103. # Ask for the cgroups of init (pid 1)
  104. initCGroups = FilePath(_initCGroupLocation)
  105. if initCGroups.exists():
  106. # The cgroups file looks like "2:cpu:/". The third element will
  107. # begin with /docker if it is inside a Docker container.
  108. controlGroups = [
  109. x.split(b":") for x in initCGroups.getContent().split(b"\n")
  110. ]
  111. for group in controlGroups:
  112. if len(group) == 3 and group[2].startswith(b"/docker/"):
  113. # If it starts with /docker/, we're in a docker container
  114. return True
  115. return False
  116. def _supportsSymlinks(self) -> bool:
  117. """
  118. Check for symlink support usable for Twisted's purposes.
  119. @return: C{True} if symlinks are supported on the current platform,
  120. otherwise C{False}.
  121. """
  122. if self.isWindows():
  123. # We do the isWindows() check as newer Pythons support the symlink
  124. # support in Vista+, but only if you have some obscure permission
  125. # (SeCreateSymbolicLinkPrivilege), which can only be given on
  126. # platforms with msc.exe (so, Business/Enterprise editions).
  127. # This uncommon requirement makes the Twisted test suite test fail
  128. # in 99.99% of cases as general users don't have permission to do
  129. # it, even if there is "symlink support".
  130. return False
  131. else:
  132. # If we're not on Windows, check for existence of os.symlink.
  133. try:
  134. os.symlink
  135. except AttributeError:
  136. return False
  137. else:
  138. return True
  139. def supportsThreads(self) -> bool:
  140. """
  141. Can threads be created?
  142. @return: C{True} if the threads are supported on the current platform.
  143. """
  144. try:
  145. import threading
  146. return threading is not None # shh pyflakes
  147. except ImportError:
  148. return False
  149. def supportsINotify(self) -> bool:
  150. """
  151. Return C{True} if we can use the inotify API on this platform.
  152. @since: 10.1
  153. """
  154. try:
  155. from twisted.python._inotify import INotifyError, init
  156. except ImportError:
  157. return False
  158. try:
  159. os.close(init())
  160. except INotifyError:
  161. return False
  162. return True
  163. platform = Platform()
  164. platformType = platform.getType()