• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Shared AIX support functions."""
2
3import sys
4import sysconfig
5
6try:
7    import subprocess
8except ImportError:  # pragma: no cover
9    # _aix_support is used in distutils by setup.py to build C extensions,
10    # before subprocess dependencies like _posixsubprocess are available.
11    import _bootsubprocess as subprocess
12
13
14def _aix_tag(vrtl, bd):
15    # type: (List[int], int) -> str
16    # Infer the ABI bitwidth from maxsize (assuming 64 bit as the default)
17    _sz = 32 if sys.maxsize == (2**31-1) else 64
18    # vrtl[version, release, technology_level]
19    return "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(vrtl[0], vrtl[1], vrtl[2], bd, _sz)
20
21
22# extract version, release and technology level from a VRMF string
23def _aix_vrtl(vrmf):
24    # type: (str) -> List[int]
25    v, r, tl = vrmf.split(".")[:3]
26    return [int(v[-1]), int(r), int(tl)]
27
28
29def _aix_bosmp64():
30    # type: () -> Tuple[str, int]
31    """
32    Return a Tuple[str, int] e.g., ['7.1.4.34', 1806]
33    The fileset bos.mp64 is the AIX kernel. It's VRMF and builddate
34    reflect the current ABI levels of the runtime environment.
35    """
36    # We expect all AIX systems to have lslpp installed in this location
37    out = subprocess.check_output(["/usr/bin/lslpp", "-Lqc", "bos.mp64"])
38    out = out.decode("utf-8")
39    out = out.strip().split(":")  # type: ignore
40    # Use str() and int() to help mypy see types
41    return (str(out[2]), int(out[-1]))
42
43
44def aix_platform():
45    # type: () -> str
46    """
47    AIX filesets are identified by four decimal values: V.R.M.F.
48    V (version) and R (release) can be retreived using ``uname``
49    Since 2007, starting with AIX 5.3 TL7, the M value has been
50    included with the fileset bos.mp64 and represents the Technology
51    Level (TL) of AIX. The F (Fix) value also increases, but is not
52    relevant for comparing releases and binary compatibility.
53    For binary compatibility the so-called builddate is needed.
54    Again, the builddate of an AIX release is associated with bos.mp64.
55    AIX ABI compatibility is described  as guaranteed at: https://www.ibm.com/\
56    support/knowledgecenter/en/ssw_aix_72/install/binary_compatability.html
57
58    For pep425 purposes the AIX platform tag becomes:
59    "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(v, r, tl, builddate, bitsize)
60    e.g., "aix-6107-1415-32" for AIX 6.1 TL7 bd 1415, 32-bit
61    and, "aix-6107-1415-64" for AIX 6.1 TL7 bd 1415, 64-bit
62    """
63    vrmf, bd = _aix_bosmp64()
64    return _aix_tag(_aix_vrtl(vrmf), bd)
65
66
67# extract vrtl from the BUILD_GNU_TYPE as an int
68def _aix_bgt():
69    # type: () -> List[int]
70    gnu_type = sysconfig.get_config_var("BUILD_GNU_TYPE")
71    if not gnu_type:
72        raise ValueError("BUILD_GNU_TYPE is not defined")
73    return _aix_vrtl(vrmf=gnu_type)
74
75
76def aix_buildtag():
77    # type: () -> str
78    """
79    Return the platform_tag of the system Python was built on.
80    """
81    # AIX_BUILDDATE is defined by configure with:
82    # lslpp -Lcq bos.mp64 | awk -F:  '{ print $NF }'
83    build_date = sysconfig.get_config_var("AIX_BUILDDATE")
84    try:
85        build_date = int(build_date)
86    except (ValueError, TypeError):
87        raise ValueError(f"AIX_BUILDDATE is not defined or invalid: "
88                         f"{build_date!r}")
89    return _aix_tag(_aix_bgt(), build_date)
90