_aix_support.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Shared AIX support functions."""
  2. import sys
  3. import sysconfig
  4. # Taken from _osx_support _read_output function
  5. def _read_cmd_output(commandstring, capture_stderr=False):
  6. """Output from successful command execution or None"""
  7. # Similar to os.popen(commandstring, "r").read(),
  8. # but without actually using os.popen because that
  9. # function is not usable during python bootstrap.
  10. import os
  11. import contextlib
  12. fp = open("/tmp/_aix_support.%s"%(
  13. os.getpid(),), "w+b")
  14. with contextlib.closing(fp) as fp:
  15. if capture_stderr:
  16. cmd = "%s >'%s' 2>&1" % (commandstring, fp.name)
  17. else:
  18. cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
  19. return fp.read() if not os.system(cmd) else None
  20. def _aix_tag(vrtl, bd):
  21. # type: (List[int], int) -> str
  22. # Infer the ABI bitwidth from maxsize (assuming 64 bit as the default)
  23. _sz = 32 if sys.maxsize == (2**31-1) else 64
  24. _bd = bd if bd != 0 else 9988
  25. # vrtl[version, release, technology_level]
  26. return "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(vrtl[0], vrtl[1], vrtl[2], _bd, _sz)
  27. # extract version, release and technology level from a VRMF string
  28. def _aix_vrtl(vrmf):
  29. # type: (str) -> List[int]
  30. v, r, tl = vrmf.split(".")[:3]
  31. return [int(v[-1]), int(r), int(tl)]
  32. def _aix_bos_rte():
  33. # type: () -> Tuple[str, int]
  34. """
  35. Return a Tuple[str, int] e.g., ['7.1.4.34', 1806]
  36. The fileset bos.rte represents the current AIX run-time level. It's VRMF and
  37. builddate reflect the current ABI levels of the runtime environment.
  38. If no builddate is found give a value that will satisfy pep425 related queries
  39. """
  40. # All AIX systems to have lslpp installed in this location
  41. # subprocess may not be available during python bootstrap
  42. try:
  43. import subprocess
  44. out = subprocess.check_output(["/usr/bin/lslpp", "-Lqc", "bos.rte"])
  45. except ImportError:
  46. out = _read_cmd_output("/usr/bin/lslpp -Lqc bos.rte")
  47. out = out.decode("utf-8")
  48. out = out.strip().split(":") # type: ignore
  49. _bd = int(out[-1]) if out[-1] != '' else 9988
  50. return (str(out[2]), _bd)
  51. def aix_platform():
  52. # type: () -> str
  53. """
  54. AIX filesets are identified by four decimal values: V.R.M.F.
  55. V (version) and R (release) can be retrieved using ``uname``
  56. Since 2007, starting with AIX 5.3 TL7, the M value has been
  57. included with the fileset bos.rte and represents the Technology
  58. Level (TL) of AIX. The F (Fix) value also increases, but is not
  59. relevant for comparing releases and binary compatibility.
  60. For binary compatibility the so-called builddate is needed.
  61. Again, the builddate of an AIX release is associated with bos.rte.
  62. AIX ABI compatibility is described as guaranteed at: https://www.ibm.com/\
  63. support/knowledgecenter/en/ssw_aix_72/install/binary_compatability.html
  64. For pep425 purposes the AIX platform tag becomes:
  65. "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(v, r, tl, builddate, bitsize)
  66. e.g., "aix-6107-1415-32" for AIX 6.1 TL7 bd 1415, 32-bit
  67. and, "aix-6107-1415-64" for AIX 6.1 TL7 bd 1415, 64-bit
  68. """
  69. vrmf, bd = _aix_bos_rte()
  70. return _aix_tag(_aix_vrtl(vrmf), bd)
  71. # extract vrtl from the BUILD_GNU_TYPE as an int
  72. def _aix_bgt():
  73. # type: () -> List[int]
  74. gnu_type = sysconfig.get_config_var("BUILD_GNU_TYPE")
  75. if not gnu_type:
  76. raise ValueError("BUILD_GNU_TYPE is not defined")
  77. return _aix_vrtl(vrmf=gnu_type)
  78. def aix_buildtag():
  79. # type: () -> str
  80. """
  81. Return the platform_tag of the system Python was built on.
  82. """
  83. # AIX_BUILDDATE is defined by configure with:
  84. # lslpp -Lcq bos.rte | awk -F: '{ print $NF }'
  85. build_date = sysconfig.get_config_var("AIX_BUILDDATE")
  86. try:
  87. build_date = int(build_date)
  88. except (ValueError, TypeError):
  89. raise ValueError(f"AIX_BUILDDATE is not defined or invalid: "
  90. f"{build_date!r}")
  91. return _aix_tag(_aix_bgt(), build_date)