|
| 1 | +import pathlib |
| 2 | +import platform |
| 3 | +import sys |
| 4 | +import tarfile |
| 5 | +import tempfile |
| 6 | +import urllib.request |
| 7 | +import zipfile |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +def is_linux_x86() -> bool: |
| 12 | + """ |
| 13 | + Check if the current system is Linux running on an x86 architecture. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + bool: True if the system is Linux and the architecture is one of |
| 17 | + x86_64, i386, or i686. False otherwise. |
| 18 | + """ |
| 19 | + return sys.platform.startswith("linux") and platform.machine() in { |
| 20 | + "x86_64", |
| 21 | + "i386", |
| 22 | + "i686", |
| 23 | + } |
| 24 | + |
| 25 | +SDK_DIR = pathlib.Path(__file__).parent / "sdk" |
| 26 | + |
| 27 | +def _download_qnn_sdk() -> pathlib.Path: |
| 28 | + """ |
| 29 | + Download and extract the Qualcomm SDK from the given URL into target_dir. |
| 30 | +
|
| 31 | + Args: |
| 32 | + url (str): The URL of the archive (.zip, .tar.gz, or .tgz). |
| 33 | + prefix_to_strip (str): Top-level directory inside the archive to strip |
| 34 | + from extracted file paths. |
| 35 | + target_dir (pathlib.Path): Directory to extract the SDK into. |
| 36 | +
|
| 37 | + Notes: |
| 38 | + - Only runs on Linux x86 platforms. Skips otherwise. |
| 39 | + - Creates the target_dir if it does not exist. |
| 40 | + """ |
| 41 | + # Default path where the Qualcomm SDK will be installed (under the script directory). |
| 42 | + |
| 43 | + # URL to download the Qualcomm AI Runtime SDK archive. |
| 44 | + qairt_url = ( |
| 45 | + "https://softwarecenter.qualcomm.com/api/download/software/sdks/" |
| 46 | + "Qualcomm_AI_Runtime_Community/All/2.34.0.250424/v2.34.0.250424.zip" |
| 47 | + ) |
| 48 | + |
| 49 | + # Top-level directory inside the SDK archive to extract. |
| 50 | + qairt_content_dir = "qairt/2.34.0.250424" |
| 51 | + |
| 52 | + if not is_linux_x86(): |
| 53 | + print("Skipping Qualcomm SDK (only supported on Linux x86).") |
| 54 | + return |
| 55 | + |
| 56 | + SDK_DIR.mkdir(parents=True, exist_ok=True) |
| 57 | + |
| 58 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 59 | + archive_path = pathlib.Path(tmpdir) / pathlib.Path(qairt_url).name |
| 60 | + |
| 61 | + print(f"Downloading Qualcomm SDK from {qairt_url}...") |
| 62 | + urllib.request.urlretrieve(qairt_url, archive_path) |
| 63 | + |
| 64 | + if qairt_url.endswith(".zip"): |
| 65 | + _extract_zip(archive_path, qairt_content_dir, SDK_DIR) |
| 66 | + elif qairt_url.endswith((".tar.gz", ".tgz")): |
| 67 | + _extract_tar(archive_path, qairt_content_dir, SDK_DIR) |
| 68 | + else: |
| 69 | + raise ValueError(f"Unsupported archive format: {qairt_url}") |
| 70 | + |
| 71 | + print(f"Qualcomm SDK extracted to {SDK_DIR}") |
| 72 | + |
| 73 | + return SDK_DIR |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +def _extract_zip(archive_path: pathlib.Path, prefix: str, target_dir: pathlib.Path): |
| 79 | + """ |
| 80 | + Extract files from a zip archive into target_dir, stripping a prefix. |
| 81 | +
|
| 82 | + Args: |
| 83 | + archive_path (pathlib.Path): Path to the .zip archive. |
| 84 | + prefix (str): Prefix folder inside the archive to strip. |
| 85 | + target_dir (pathlib.Path): Destination directory. |
| 86 | + """ |
| 87 | + with zipfile.ZipFile(archive_path, "r") as zf: |
| 88 | + for member in zf.infolist(): |
| 89 | + if not member.filename.startswith(prefix + "/"): |
| 90 | + continue |
| 91 | + relpath = pathlib.Path(member.filename).relative_to(prefix) |
| 92 | + if not relpath.parts or relpath.parts[0] == "..": |
| 93 | + continue |
| 94 | + out_path = target_dir / relpath |
| 95 | + if member.is_dir(): |
| 96 | + out_path.mkdir(parents=True, exist_ok=True) |
| 97 | + else: |
| 98 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 99 | + with zf.open(member) as src, open(out_path, "wb") as dst: |
| 100 | + dst.write(src.read()) |
| 101 | + |
| 102 | + |
| 103 | +def _extract_tar(archive_path: pathlib.Path, prefix: str, target_dir: pathlib.Path): |
| 104 | + """ |
| 105 | + Extract files from a tar.gz archive into target_dir, stripping a prefix. |
| 106 | +
|
| 107 | + Args: |
| 108 | + archive_path (pathlib.Path): Path to the .tar.gz or .tgz archive. |
| 109 | + prefix (str): Prefix folder inside the archive to strip. |
| 110 | + target_dir (pathlib.Path): Destination directory. |
| 111 | + """ |
| 112 | + with tarfile.open(archive_path, "r:gz") as tf: |
| 113 | + for m in tf.getmembers(): |
| 114 | + if not m.name.startswith(prefix + "/"): |
| 115 | + continue |
| 116 | + relpath = pathlib.Path(m.name).relative_to(prefix) |
| 117 | + if not relpath.parts or relpath.parts[0] == "..": |
| 118 | + continue |
| 119 | + |
| 120 | + out_path = target_dir / relpath |
| 121 | + if m.isdir(): |
| 122 | + out_path.mkdir(parents=True, exist_ok=True) |
| 123 | + else: |
| 124 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 125 | + src = tf.extractfile(m) |
| 126 | + if src is None: |
| 127 | + # Skip non-regular files (links, devices, etc.) |
| 128 | + continue |
| 129 | + with src, open(out_path, "wb") as dst: |
| 130 | + dst.write(src.read()) |
0 commit comments