aboutsummaryrefslogtreecommitdiffstats
path: root/optical.py
blob: 1e3a2687c8810ae8343f2502cadccd35d5cb0544 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import json
import os
import platform
import shutil
import subprocess
import tempfile
import time
import urllib.request
import zipfile
from collections.abc import Callable
from pathlib import Path


CUETOOLS_VERSION = "2.2.6"
CUETOOLS_DOWNLOAD_URL = (
    "https://github.com/gchudov/cuetools.net/releases/download/"
    "v2.2.6/CUETools_2.2.6.zip"
)


def _get_powershell_executable() -> str | None:
    return shutil.which("powershell") or shutil.which("pwsh")


def _get_windows_tools_root() -> Path:
    local_app_data = os.environ.get("LOCALAPPDATA")
    if local_app_data:
        return Path(local_app_data) / "fiio-snowsky-echo-flac-tool" / "tools"
    return Path.home() / ".fiio-snowsky-echo-flac-tool" / "tools"


def _get_windows_cuetools_dir() -> Path:
    return _get_windows_tools_root() / f"cuetools-{CUETOOLS_VERSION}"


def _find_cueripper_in_dir(base_dir: Path) -> str | None:
    if not base_dir.exists():
        return None

    for name in ("CUERipper.Console.exe", "CUERipper.exe"):
        matches = sorted(base_dir.rglob(name))
        if matches:
            return str(matches[0])

    return None


def _get_cueripper_executable() -> str | None:
    explicit = os.environ.get("CUERIPPER_EXE")
    if explicit:
        return explicit

    for name in ["CUERipper.Console.exe", "CUERipper.exe"]:
        found = shutil.which(name)
        if found:
            return found

    return _find_cueripper_in_dir(_get_windows_cuetools_dir())


def needs_cuetools_install() -> bool:
    return platform.system() == "Windows" and _get_cueripper_executable() is None


def ensure_windows_cuetools(
    on_output: Callable[[str], None] | None = None,
) -> str:
    if platform.system() != "Windows":
        raise RuntimeError("CUETools bootstrap is only supported on Windows")

    existing = _get_cueripper_executable()
    if existing:
        return existing

    install_dir = _get_windows_cuetools_dir()
    install_dir.mkdir(parents=True, exist_ok=True)

    zip_path = install_dir / "CUETools_2.2.6.zip"

    if on_output:
        on_output(f"Downloading CUETools from {CUETOOLS_DOWNLOAD_URL}")

    with urllib.request.urlopen(CUETOOLS_DOWNLOAD_URL) as resp, zip_path.open("wb") as dst:
        shutil.copyfileobj(resp, dst)

    if on_output:
        on_output(f"Extracting {zip_path} to {install_dir}")

    with zipfile.ZipFile(zip_path, "r") as zf:
        zf.extractall(install_dir)

    cueripper = _find_cueripper_in_dir(install_dir)
    if not cueripper:
        raise RuntimeError(
            "Downloaded CUETools archive but could not find CUERipper executable"
        )

    if on_output:
        on_output(f"CUERipper installed: {cueripper}")

    return cueripper


def _iter_existing_dirs(paths: list[Path]) -> list[Path]:
    seen: set[str] = set()
    out: list[Path] = []
    for p in paths:
        key = str(p).lower()
        if key in seen:
            continue
        seen.add(key)
        if p.exists() and p.is_dir():
            out.append(p)
    return out


def _find_recent_flacs(search_roots: list[Path], since_epoch: float) -> list[str]:
    found: list[str] = []
    for root in _iter_existing_dirs(search_roots):
        for path in root.rglob("*.flac"):
            try:
                if path.stat().st_mtime >= since_epoch:
                    found.append(str(path.resolve()))
            except OSError:
                continue
    return sorted(set(found))


def _run_streaming_command(
    cmd: list[str],
    cwd: Path,
    on_output: Callable[[str], None] | None = None,
) -> list[str]:
    proc = subprocess.Popen(
        cmd,
        cwd=cwd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1,
        env=os.environ.copy(),
    )

    lines: list[str] = []
    if proc.stdout:
        for raw in proc.stdout:
            line = raw.rstrip("\n")
            lines.append(line)
            if on_output and line:
                on_output(line)

    rc = proc.wait()
    if rc != 0:
        raise subprocess.CalledProcessError(rc, cmd, output="\n".join(lines))

    return lines


def check_dependencies() -> tuple[bool, str]:
    if platform.system() == "Windows":
        missing: list[str] = []
        if _get_powershell_executable() is None:
            missing.append("powershell")
        if _get_cueripper_executable() is None:
            missing.append("CUERipper.Console.exe")

        if missing:
            return (
                False,
                "Ripping CDs on Windows requires: "
                + ", ".join(missing)
                + ". CUERipper can be auto-downloaded.",
            )
        return True, "OK"

    if platform.system() == "Linux":
        try:
            subprocess.run(["abcde", "-h"], check=True, capture_output=True)
            return True, "OK"
        except (subprocess.CalledProcessError, FileNotFoundError):
            return (
                False,
                "Ripping CDs requires the 'abcde' package to be installed, please refer to the README for more information",
            )

    return True, "You are on an unsupported platform, CD features may not be fully functional"


def get_optical_drives() -> list[tuple[str, str]]:
    import re

    if platform.system() == "Windows":
        powershell = _get_powershell_executable()
        if not powershell:
            return []

        try:
            res = subprocess.run(
                [
                    powershell,
                    "-NoProfile",
                    "-Command",
                    "Get-CimInstance Win32_CDROMDrive | "
                    "Select-Object Drive, Caption, Name, MediaLoaded | "
                    "ConvertTo-Json -Compress",
                ],
                capture_output=True,
                text=True,
                check=True,
            )

            payload = res.stdout.strip()
            if not payload:
                return []

            parsed = json.loads(payload)
            entries = parsed if isinstance(parsed, list) else [parsed]

            drives: list[tuple[str, str]] = []
            for entry in entries:
                drive = entry.get("Drive")
                if not drive:
                    continue
                caption = entry.get("Caption") or entry.get("Name") or drive
                media_loaded = entry.get("MediaLoaded")
                if media_loaded is False:
                    caption = f"{caption} (no media)"
                drives.append((drive, caption))

            return drives
        except (subprocess.CalledProcessError, json.JSONDecodeError):
            return []

    if platform.system() == "Linux":
        unix_path_regex = re.compile(r"(/dev/[^\s]+)")
        try:
            res = subprocess.run(
                ["lsblk", "-d", "-o", "PATH,TYPE,MODEL"],
                capture_output=True,
                text=True,
                check=True,
            )
            drives = [
                (m.group(1), line)
                for line in res.stdout.splitlines()
                if "rom" in line and (m := unix_path_regex.search(line))
            ]
            return drives
        except subprocess.CalledProcessError:
            return []

    return []


def run_abcde_rip(
    drive: str,
    on_output: Callable[[str], None] | None = None,
) -> list[str]:
    workspace = Path(tempfile.gettempdir()) / "expergo-rip-workspace"
    if workspace.exists():
        shutil.rmtree(workspace)
    workspace.mkdir(parents=True, exist_ok=True)

    if platform.system() == "Windows":
        cueripper = _get_cueripper_executable()
        if not cueripper:
            raise RuntimeError(
                "CUERipper executable not found. Install CUETools and ensure "
                "CUERipper.Console.exe is in PATH, set CUERIPPER_EXE, "
                "or use auto-download from the Rip CD prompt."
            )

        rip_start = time.time()
        if on_output:
            on_output("CUERipper GUI opened. Start rip there and close it when done.")

        _run_streaming_command([cueripper], workspace, on_output)

        flacs_in_workspace = sorted(str(p.resolve()) for p in workspace.rglob("*.flac"))
        if flacs_in_workspace:
            return flacs_in_workspace

        configured_output = os.environ.get("CUERIPPER_OUTPUT_DIR")
        search_roots = [
            workspace,
            Path.home() / "Music",
        ]
        if configured_output:
            search_roots.insert(0, Path(configured_output))

        recent_flacs = _find_recent_flacs(search_roots, rip_start - 2)
        if recent_flacs:
            return recent_flacs

        searched = "\n".join(f"- {p}" for p in _iter_existing_dirs(search_roots))
        raise RuntimeError(
            "CUERipper completed but no FLAC files were detected.\n\n"
            "Searched these locations for newly created .flac files:\n"
            f"{searched}\n\n"
            "If CUERipper saves elsewhere, set CUERIPPER_OUTPUT_DIR to that folder."
        )

    cmd = ["abcde", "-d", drive, "-L", "-n", "-N", "-o", "flac"]
    _run_streaming_command(cmd, workspace, on_output)

    return sorted(str(p.resolve()) for p in workspace.rglob("*.flac"))
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage