Extract the CLI binary from a .tar.gz archive.
(data: bytes, binary_name: str, dest_dir: Path)
| 155 | |
| 156 | |
| 157 | def _extract_tar_gz(data: bytes, binary_name: str, dest_dir: Path) -> Path: |
| 158 | """Extract the CLI binary from a .tar.gz archive.""" |
| 159 | with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tf: |
| 160 | # Find the binary in the archive (may be at top level or in a subdirectory) |
| 161 | members = tf.getnames() |
| 162 | target_member = None |
| 163 | for name in members: |
| 164 | if name == binary_name or name.endswith(f"/{binary_name}"): |
| 165 | target_member = name |
| 166 | break |
| 167 | |
| 168 | if target_member is None: |
| 169 | raise RuntimeError( |
| 170 | f"Binary '{binary_name}' not found in archive. Archive contains: {members}" |
| 171 | ) |
| 172 | |
| 173 | member = tf.getmember(target_member) |
| 174 | f = tf.extractfile(member) |
| 175 | if f is None: |
| 176 | raise RuntimeError(f"Could not extract '{target_member}' from archive") |
| 177 | |
| 178 | dest_path = dest_dir / binary_name |
| 179 | with open(dest_path, "wb") as out: |
| 180 | out.write(f.read()) |
| 181 | |
| 182 | return dest_path |
| 183 | |
| 184 | |
| 185 | def _extract_zip(data: bytes, binary_name: str, dest_dir: Path) -> Path: |
no test coverage detected
searching dependent graphs…