|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright The SCons Foundation |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +# a copy of this software and associated documentation files (the |
| 7 | +# "Software"), to deal in the Software without restriction, including |
| 8 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +# permit persons to whom the Software is furnished to do so, subject to |
| 11 | +# the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included |
| 14 | +# in all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 17 | +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +""" |
| 25 | +A testing framework for the SCons software construction tool. |
| 26 | +
|
| 27 | +A TestSConsTar environment object is created via the usual invocation: |
| 28 | +
|
| 29 | + test = TestSConsTar() |
| 30 | +
|
| 31 | +TestSConsTar is a subsclass of TestSCons, which is in turn a subclass |
| 32 | +of TestCommon, which is in turn is a subclass of TestCmd), and hence |
| 33 | +has available all of the methods and attributes from those classes, |
| 34 | +as well as any overridden or additional methods or attributes defined |
| 35 | +in this subclass. |
| 36 | +""" |
| 37 | + |
| 38 | +import os |
| 39 | +import os.path |
| 40 | +import subprocess |
| 41 | +import sys |
| 42 | + |
| 43 | +from TestSCons import * |
| 44 | +from TestSCons import __all__ |
| 45 | + |
| 46 | +__all__.extend([ |
| 47 | + 'TestSConsTar', |
| 48 | + 'windows_system_tar_gz', |
| 49 | + 'windows_system_tar_bz2', |
| 50 | + 'windows_system_tar_xz', |
| 51 | + 'windows_system_tar_lzma', |
| 52 | +]) |
| 53 | + |
| 54 | +if sys.platform == 'win32': |
| 55 | + |
| 56 | + # Windows 10 and later supplies windows/system32/tar.exe (bsdtar). |
| 57 | + # Not all versions support all compression types. Check the version |
| 58 | + # string for library support. |
| 59 | + |
| 60 | + # windows tar.exe: |
| 61 | + # %systemroot%/system32/tar.exe |
| 62 | + |
| 63 | + # tar.exe --version (Windows 10, GH windows-2022): |
| 64 | + # bsdtar 3.5.2 - libarchive 3.5.2 zlib/1.2.5.f-ipp |
| 65 | + |
| 66 | + # tar.exe --version (Windows 11): |
| 67 | + # bsdtar 3.7.7 - libarchive 3.7.7 zlib/1.2.5.f-ipp liblzma/5.4.3 bz2lib/1.0.8 libzstd/1.5.4 |
| 68 | + |
| 69 | + # tar.exe --version (GH windows-2025): |
| 70 | + # bsdtar 3.7.7 - libarchive 3.7.7 zlib/1.2.13.1-motley liblzma/5.4.3 bz2lib/1.0.8 libzstd/1.5.5 |
| 71 | + |
| 72 | + def _is_windows_system_tar(tar): |
| 73 | + |
| 74 | + if not tar: |
| 75 | + return False |
| 76 | + |
| 77 | + tar = os.path.normcase(os.path.abspath(tar)) |
| 78 | + if not os.path.exists(tar): |
| 79 | + return False |
| 80 | + |
| 81 | + if not tar.endswith('tar.exe'): |
| 82 | + return False |
| 83 | + |
| 84 | + windir = os.environ.get("SystemRoot") |
| 85 | + if not windir: |
| 86 | + windir = os.environ.get("windir") |
| 87 | + |
| 88 | + if not windir: |
| 89 | + windir = os.path.join(os.environ.get("SystemDrive", "C:") + os.path.sep, "Windows") |
| 90 | + |
| 91 | + windows_tar = os.path.normcase(os.path.abspath(os.path.join(windir, "System32", "tar.exe"))) |
| 92 | + rval = bool(tar == windows_tar) |
| 93 | + |
| 94 | + return rval |
| 95 | + |
| 96 | + def _windows_system_tar_have_library(tar, libname): |
| 97 | + |
| 98 | + is_wintar = _is_windows_system_tar(tar) |
| 99 | + if not is_wintar: |
| 100 | + return is_wintar, None |
| 101 | + |
| 102 | + try: |
| 103 | + result = subprocess.run([f"{tar}", "--version"], capture_output=True, text=True, check=True) |
| 104 | + version_str = result.stdout.strip() |
| 105 | + except: |
| 106 | + version_str = None |
| 107 | + |
| 108 | + if not version_str: |
| 109 | + return is_wintar, None |
| 110 | + |
| 111 | + # print(f"{tar} --version => {version_str!r}") |
| 112 | + |
| 113 | + version_comps = version_str.split() |
| 114 | + if len(version_comps) < 5: |
| 115 | + return is_wintar, None |
| 116 | + |
| 117 | + for indx, expected in [ |
| 118 | + (0, "bsdtar"), (2, "-"), (3, "libarchive"), |
| 119 | + ]: |
| 120 | + if version_comps[indx].lower() != expected: |
| 121 | + return is_wintar, None |
| 122 | + |
| 123 | + # bsdtar_version = version_comps[1] |
| 124 | + # libarchive_version = version_comps[4] |
| 125 | + |
| 126 | + kind = libname + "/" |
| 127 | + |
| 128 | + have_library = False |
| 129 | + for component in version_comps[5:]: |
| 130 | + if component.lower().startswith(kind): |
| 131 | + have_library = True |
| 132 | + break |
| 133 | + |
| 134 | + return is_wintar, have_library |
| 135 | + |
| 136 | + def windows_system_tar_gz(tar): |
| 137 | + is_wintar, have_library = _windows_system_tar_have_library(tar, 'zlib') |
| 138 | + return is_wintar, have_library |
| 139 | + |
| 140 | + def windows_system_tar_bz2(tar): |
| 141 | + is_wintar, have_library = _windows_system_tar_have_library(tar, 'bz2lib') |
| 142 | + return is_wintar, have_library |
| 143 | + |
| 144 | + def windows_system_tar_xz(tar): |
| 145 | + is_wintar, have_library = _windows_system_tar_have_library(tar, 'liblzma') |
| 146 | + return is_wintar, have_library |
| 147 | + |
| 148 | + def windows_system_tar_lzma(tar): |
| 149 | + is_wintar, have_library = _windows_system_tar_have_library(tar, 'liblzma') |
| 150 | + return is_wintar, have_library |
| 151 | + |
| 152 | +else: |
| 153 | + |
| 154 | + def windows_system_tar_gz(tar): |
| 155 | + return False, None |
| 156 | + |
| 157 | + def windows_system_tar_bz2(tar): |
| 158 | + return False, None |
| 159 | + |
| 160 | + def windows_system_tar_xz(tar): |
| 161 | + return False, None |
| 162 | + |
| 163 | + def windows_system_tar_lzma(tar): |
| 164 | + return False, None |
| 165 | + |
| 166 | +class TestSConsTar(TestSCons): |
| 167 | + pass |
| 168 | + |
| 169 | +# Local Variables: |
| 170 | +# tab-width:4 |
| 171 | +# indent-tabs-mode:nil |
| 172 | +# End: |
| 173 | +# vim: set expandtab tabstop=4 shiftwidth=4: |
0 commit comments