rekfix 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rekfix/cli.py ADDED
@@ -0,0 +1,382 @@
1
+ """rekfix - diagnose (and carefully repair) rekordbox USB exports.
2
+
3
+ rekfix check E:\\ health of the stick's export.pdb (+ its files)
4
+ rekfix check export.pdb --scan recovery read: rows by page, ignoring a damaged header/chains
5
+ rekfix diff OLD.pdb NEW.pdb what changed between two copies
6
+ rekfix menu E:\\ the players' root menu
7
+ rekfix menu E:\\ --hide time,bitrate,"file name","hot cue bank" --apply
8
+ rekfix prune E:\\ tracks whose audio is gone: what removing them would change
9
+ rekfix prune E:\\ --restore-from D:\\Music --apply
10
+ rekfix rebuild E:\\ library.xml --music D:\\stick-copy recover the library for rekordbox
11
+ rekfix clone E:\\ F:\\ --audio-from D:\\Music --apply an exact copy, read back file by file
12
+ rekfix history E:\\ play histories the decks saved
13
+ rekfix snapshot E:\\ D:\\backups copy the databases + list every file, before anything else
14
+ rekfix readtest E:\\ how fast and evenly the stick reads
15
+
16
+ Commands that write (menu and prune, with --apply) save a copy of the database first, refuse
17
+ while rekordbox is running, and only edit fixed-size fields in place.
18
+ """
19
+ import argparse
20
+ import datetime as dt
21
+ import os
22
+ import shutil
23
+ import subprocess
24
+ import sys
25
+ from collections import Counter, defaultdict
26
+
27
+ from . import __version__, check, clone, menu, prune, readtest, rebuild, snapshot
28
+ from .pdb import TABLES, Pdb
29
+
30
+
31
+ def pdb_path(target):
32
+ if os.path.isdir(target):
33
+ p = os.path.join(target, "PIONEER", "rekordbox", "export.pdb")
34
+ if not os.path.exists(p):
35
+ raise SystemExit(f"no PIONEER\\rekordbox\\export.pdb under {target}")
36
+ return p, target
37
+ if not os.path.exists(target):
38
+ raise SystemExit(f"not found: {target}")
39
+ return target, None
40
+
41
+
42
+ def rekordbox_running():
43
+ try:
44
+ if os.name == "nt":
45
+ r = subprocess.run(["tasklist", "/FI", "IMAGENAME eq rekordbox.exe", "/NH"], capture_output=True,
46
+ text=True, errors="replace", creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
47
+ return "rekordbox.exe" in r.stdout.lower()
48
+ return subprocess.run(["pgrep", "-x", "rekordbox"], stdout=subprocess.DEVNULL,
49
+ stderr=subprocess.DEVNULL).returncode == 0
50
+ except FileNotFoundError:
51
+ return False
52
+
53
+
54
+ def backup(path, dest=None):
55
+ stamp = dt.datetime.now().strftime("%Y%m%d-%H%M%S")
56
+ target = os.path.join(dest, f"export.pdb.{stamp}") if dest else f"{path}.rekfix-{stamp}.bak"
57
+ os.makedirs(os.path.dirname(os.path.abspath(target)), exist_ok=True)
58
+ shutil.copy2(path, target)
59
+ return target
60
+
61
+
62
+ # ------------------------------------------------------------ commands
63
+ def cmd_check(a):
64
+ path, root = pdb_path(a.target)
65
+ pdb = Pdb.open(path, scan=a.scan, second_word=a.second_word)
66
+ counts, found = check.walk(pdb)
67
+ by_id, more = check.content(pdb)
68
+ found += more
69
+ if root and not a.no_files:
70
+ found += check.files(pdb, root, by_id)
71
+ print(f"{path}: {pdb.num_pages:,} pages of {pdb.page_size}, sequence {pdb.sequence}"
72
+ + (" (recovery read)" if a.scan else ""))
73
+ for level in ("error", "warn", "info"):
74
+ for f in found:
75
+ if f.level == level and (a.verbose or f.level != "info" or f.code in ("summary", "format.legacy-flags")):
76
+ print(" ", f)
77
+ if a.tables:
78
+ for typ, (pages, rows) in sorted(counts.items()):
79
+ print(f" {typ:2} {TABLES.get(typ, '?'):18} {pages:6} pages {rows:8,} rows")
80
+ errors = sum(f.level == "error" for f in found)
81
+ print(f"{errors} error(s), {sum(f.level == 'warn' for f in found)} warning(s)")
82
+ return 1 if errors else 0
83
+
84
+
85
+ def cmd_diff(a):
86
+ old, new = Pdb.open(pdb_path(a.old)[0]), Pdb.open(pdb_path(a.new)[0])
87
+ print(f"sequence {old.sequence} -> {new.sequence}; pages {old.num_pages:,} -> {new.num_pages:,}")
88
+ for typ in sorted(set(old.tables) | set(new.tables)):
89
+ ro, rn = len(old.rows(typ)), len(new.rows(typ))
90
+ if ro != rn:
91
+ print(f" {TABLES.get(typ, typ):18} rows {ro:,} -> {rn:,}")
92
+ changed = Counter()
93
+ for i in range(1, min(old.num_pages, new.num_pages)):
94
+ a_, b_ = old.page_size * i, old.page_size * (i + 1)
95
+ if old.data[a_:b_] != new.data[a_:b_]:
96
+ changed[TABLES.get(new.page(i).type, new.page(i).type)] += 1
97
+ if changed:
98
+ print(" pages changed by table:", dict(changed.most_common()))
99
+ to, tn = old.tracks(), new.tracks()
100
+ print(f" tracks: +{len(set(tn) - set(to)):,} new, -{len(set(to) - set(tn)):,} gone")
101
+ po, pn = old.playlist_tree(), new.playlist_tree()
102
+ for pid in sorted(set(po) | set(pn)):
103
+ if pid not in po:
104
+ print(f" + playlist {pn[pid][3]!r}")
105
+ elif pid not in pn:
106
+ print(f" - playlist {po[pid][3]!r}")
107
+ elif po[pid][3] != pn[pid][3] or po[pid][0] != pn[pid][0]:
108
+ print(f" ~ playlist {po[pid][3]!r} -> {pn[pid][3]!r} (parent {po[pid][0]} -> {pn[pid][0]})")
109
+ mo = [n for s, c, n, m in menu.items(old) if s]
110
+ mn = [n for s, c, n, m in menu.items(new) if s]
111
+ if mo != mn:
112
+ print(" root menu:", ", ".join(mo), "\n ->", ", ".join(mn))
113
+ ho, hn = old.history_playlists(), new.history_playlists()
114
+ if ho != hn:
115
+ print(f" play histories: {len(ho)} -> {len(hn)}")
116
+ return 0
117
+
118
+
119
+ def cmd_menu(a):
120
+ path, root = pdb_path(a.target)
121
+ pdb = Pdb.open(path)
122
+ change = a.hide or a.show
123
+ errors = sorted({f.code for f in check.walk(pdb)[1] if f.level == "error"}) if change else []
124
+ if errors:
125
+ print(f"nothing changed: the database has structural errors ({', '.join(errors)}), and menu only "
126
+ f"edits a healthy one; see `rekfix check`")
127
+ return 1
128
+ if change:
129
+ wanted = [w for part in change for w in part.split(",") if w.strip()]
130
+ ids = menu.resolve(pdb, wanted)
131
+ done = menu.hide(pdb, ids) if a.hide else menu.show(pdb, ids)
132
+ verb = "hidden" if a.hide else "shown"
133
+ if not done:
134
+ print(f"nothing to change - already {verb}")
135
+ return 0
136
+ print(f"{verb}: {', '.join(done)}")
137
+ for pos, cat, name, m in menu.items(pdb):
138
+ print(f" {pos if pos else '-':>3} {name}")
139
+ if not change:
140
+ return 0
141
+ if not a.apply:
142
+ print("dry run: add --apply to write it (a copy of export.pdb is saved first)")
143
+ return 0
144
+ if rekordbox_running():
145
+ raise SystemExit("rekordbox is running - close it first (it may rewrite the stick)")
146
+ saved = backup(path, a.backup_dir)
147
+ pdb.commit()
148
+ pdb.save(path)
149
+ print(f"written. The original is saved as {saved}")
150
+ return 0
151
+
152
+
153
+ def cmd_prune(a):
154
+ path, root = pdb_path(a.target)
155
+ if root is None:
156
+ raise SystemExit("prune needs the stick (its root folder), not only export.pdb: it looks there for "
157
+ "each track's audio")
158
+ pdb = Pdb.open(path)
159
+ try:
160
+ p = prune.plan(pdb, root, a.restore_from or (), a.restore_all, a.force)
161
+ except prune.PruneError as e:
162
+ print(f"nothing changed: {e}")
163
+ return 1
164
+ print(f"{path}:")
165
+ for line in prune.describe(pdb, p, a.verbose):
166
+ print(" ", line)
167
+ if not p.edits_database and not p.restore:
168
+ print("nothing to do")
169
+ return 0
170
+ changed = []
171
+ if p.edits_database:
172
+ before = Pdb(bytes(pdb.data))
173
+ changed = prune.apply(pdb, p)
174
+ problems = prune.verify(before, pdb.data, p, changed)
175
+ if problems:
176
+ print("the edited database didn't check out, so nothing was written:")
177
+ for problem in problems:
178
+ print(" !", problem)
179
+ return 1
180
+ if not a.apply:
181
+ what = f"export.pdb ({len(changed):,} pages)" if changed else "nothing in export.pdb"
182
+ print(f"dry run, checked: this would change {what}"
183
+ + (f" and copy {len(p.restore):,} files back" if p.restore else "")
184
+ + ". Add --apply to do it (export.pdb is copied first)")
185
+ return 0
186
+ if rekordbox_running():
187
+ raise SystemExit("rekordbox is running - close it first (it may rewrite the stick)")
188
+ saved = backup(path, a.backup_dir) if p.edits_database else None
189
+ try:
190
+ copied = prune.restore_files(p, root)
191
+ except (prune.PruneError, OSError) as e:
192
+ print(f"stopped while copying audio back: {e}")
193
+ return 1
194
+ if p.edits_database:
195
+ pdb.save(path)
196
+ print(f"done: {len(p.remove):,} tracks removed, {len(copied):,} files copied back"
197
+ + (f". The original export.pdb is saved as {saved}" if saved else ""))
198
+ print("Test the stick on your decks before relying on it.")
199
+ return 0
200
+
201
+
202
+ def cmd_rebuild(a):
203
+ if not os.path.isdir(a.source):
204
+ raise SystemExit("rebuild reads a stick, or a full copy of one: a folder with PIONEER (and Contents) in it")
205
+ out = os.path.abspath(a.out)
206
+ rb = rebuild.collect(a.source, a.music or (), a.pdb, a.scan, a.second_word, a.all_audio)
207
+ rebuild.write_xml(rb, out)
208
+ report = os.path.splitext(out)[0] + ".report.txt"
209
+ rebuild.write_report(rb, report)
210
+ for line in rebuild.report_lines(rb):
211
+ print(" ", line)
212
+ problems = rebuild.verify_xml(out, rb)
213
+ for p in problems:
214
+ print(" !", p)
215
+ print(f"written: {out} ({len(rb.items):,} tracks); the details are in {report}")
216
+ if not a.music:
217
+ print("note: the XML points at the audio where it is now. If that's the stick itself, copy its Contents "
218
+ "folder to your computer first and pass --music, so your rekordbox library doesn't depend on the stick.")
219
+ print("to import: rekordbox > Preferences > Advanced > Database > rekordbox xml: choose this file; show the "
220
+ "\"rekordbox xml\" tree (Preferences > View > Layout), then right-click a playlist > Import Playlist.")
221
+ return 1 if problems else 0
222
+
223
+
224
+ def cmd_clone(a):
225
+ p = clone.plan(a.source, a.target, a.audio_from or (), a.skip or (), a.allow_missing)
226
+ for line in clone.describe(p):
227
+ print(line)
228
+ if a.verify_only:
229
+ problems = clone.verify_only(p)
230
+ for problem in problems[:50]:
231
+ print(" !", problem)
232
+ print(f"{len(problems):,} difference(s)" if problems else "the target matches the source, byte for byte")
233
+ return 1 if problems else 0
234
+ if p.refusals:
235
+ return 1
236
+ if not a.apply:
237
+ print("dry run: add --apply to copy (the target's files are replaced; nothing on the source changes)")
238
+ return 0
239
+ if rekordbox_running():
240
+ raise SystemExit("rekordbox is running - close it first (it writes to sticks it sees)")
241
+ try:
242
+ done, nbytes, failures = clone.run(p, verify=not a.no_verify, remove_extra=a.remove_extra)
243
+ except clone.CloneError as e:
244
+ print(f"stopped: {e}")
245
+ return 1
246
+ for f in failures[:50]:
247
+ print(" !", f)
248
+ if failures:
249
+ return 1
250
+ print(f"done: {done:,} files, {nbytes / 1e9:.1f} GB copied" + ("" if a.no_verify else " and read back")
251
+ + ". Eject the target properly, then test it on your decks.")
252
+ return 0
253
+
254
+
255
+ def cmd_history(a):
256
+ pdb = Pdb.open(pdb_path(a.target)[0], scan=a.scan, second_word=a.second_word)
257
+ lists = pdb.history_playlists()
258
+ entries = defaultdict(list)
259
+ for pid, idx, tid, r in pdb.history_entries():
260
+ entries[pid].append((idx, tid))
261
+ tracks = pdb.tracks()
262
+ artists = pdb.artists()
263
+ for pid, name in sorted(lists.items()):
264
+ print(f" {name:20} {len(entries[pid]):4} plays")
265
+ if a.tracks:
266
+ for idx, tid in sorted(entries[pid]):
267
+ t = tracks.get(tid, [None])[0]
268
+ who = artists.get(t.artist_id, "") if t else ""
269
+ print(f" {idx:3}. {who + ' - ' if who else ''}{t.title if t else f'(missing track {tid})'}")
270
+ orphans = [pid for pid in entries if pid not in lists]
271
+ if orphans:
272
+ print(f" ({sum(len(entries[p]) for p in orphans)} plays belong to history lists that are missing)")
273
+ return 0
274
+
275
+
276
+ def cmd_snapshot(a):
277
+ snapshot.take(a.stick, a.dest, a.label, a.full)
278
+ return 0
279
+
280
+
281
+ def cmd_readtest(a):
282
+ return 0 if readtest.run(a.stick, a.music, a.analysis) else 1
283
+
284
+
285
+ def main(argv=None):
286
+ ap = argparse.ArgumentParser(prog="rekfix", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
287
+ ap.add_argument("--version", action="version", version=f"rekfix {__version__}")
288
+ sub = ap.add_subparsers(dest="cmd", required=True)
289
+
290
+ p = sub.add_parser("check", help="health check of export.pdb (a stick or a file)")
291
+ p.add_argument("target", help="the stick (e.g. E:\\) or an export.pdb file")
292
+ p.add_argument("--scan", action="store_true", help="recovery read: rows by page type, ignoring header and chains")
293
+ p.add_argument("--second-word", action="store_true", help="also count rows present only in the second footer "
294
+ "word (deleted rows, or older exports' live rows)")
295
+ p.add_argument("--no-files", action="store_true", help="skip checking audio/analysis files on the stick")
296
+ p.add_argument("--tables", action="store_true", help="list pages and rows per table")
297
+ p.add_argument("-v", "--verbose", action="store_true", help="show every finding")
298
+ p.set_defaults(fn=cmd_check)
299
+
300
+ p = sub.add_parser("diff", help="what changed between two export.pdb copies")
301
+ p.add_argument("old")
302
+ p.add_argument("new")
303
+ p.set_defaults(fn=cmd_diff)
304
+
305
+ p = sub.add_parser("menu", help="the players' root menu; --hide / --show categories")
306
+ p.add_argument("target")
307
+ g = p.add_mutually_exclusive_group()
308
+ g.add_argument("--hide", action="append", help="categories to hide (comma-separated names)")
309
+ g.add_argument("--show", action="append", help="categories to bring back")
310
+ p.add_argument("--apply", action="store_true", help="write the change (otherwise a dry run)")
311
+ p.add_argument("--backup-dir", help="where the copy of the original goes (default: next to it)")
312
+ p.set_defaults(fn=cmd_menu)
313
+
314
+ p = sub.add_parser("prune", help="remove tracks whose audio is gone, the way rekordbox removes tracks")
315
+ p.add_argument("target", help="the stick (e.g. E:\\)")
316
+ p.add_argument("--restore-from", action="append", metavar="DIR",
317
+ help="folder to find missing audio in (a stick snapshot or your music library); "
318
+ "tracks a play history lists get their audio copied back from it")
319
+ p.add_argument("--restore-all", action="store_true",
320
+ help="copy back every missing track found in --restore-from, not only those in a history")
321
+ p.add_argument("--apply", action="store_true", help="write the change (otherwise a dry run)")
322
+ p.add_argument("--force", action="store_true", help="go ahead even if most tracks look missing")
323
+ p.add_argument("--backup-dir", help="where the copy of export.pdb goes (default: next to it)")
324
+ p.add_argument("-v", "--verbose", action="store_true", help="list every track affected")
325
+ p.set_defaults(fn=cmd_prune)
326
+
327
+ p = sub.add_parser("rebuild", help="recover the library as rekordbox XML: tracks, key, BPM, beat grids, cues, "
328
+ "playlists, play histories")
329
+ p.add_argument("source", help="the stick, or a full copy of it (a folder with PIONEER and Contents)")
330
+ p.add_argument("out", help="the XML file to write")
331
+ p.add_argument("--music", action="append", metavar="DIR",
332
+ help="where the audio is for rekordbox to import from: a copy of the stick or of its Contents "
333
+ "folder, or your music library (default: the source)")
334
+ p.add_argument("--pdb", help="use this export.pdb (e.g. a saved copy) instead of the source's")
335
+ p.add_argument("--scan", action="store_true", help="recovery read of a damaged export.pdb (see check --scan)")
336
+ p.add_argument("--second-word", action="store_true", help="see check --second-word")
337
+ p.add_argument("--all-audio", action="store_true",
338
+ help="also add audio files the database doesn't list, with their analysis")
339
+ p.set_defaults(fn=cmd_rebuild)
340
+
341
+ p = sub.add_parser("clone", help="copy a stick exactly onto another stick (no rekordbox export), then read "
342
+ "every file back")
343
+ p.add_argument("source", help="the stick, or a full snapshot of it (rekfix snapshot --full)")
344
+ p.add_argument("target", help="the stick to copy onto: freshly formatted (FAT32), or an earlier clone")
345
+ p.add_argument("--audio-from", action="append", metavar="DIR",
346
+ help="your music library: audio is taken from here when the same file (name and size) is "
347
+ "found, so the source is read less; needed when the source is a snapshot")
348
+ p.add_argument("--skip", action="append", metavar="FOLDER", help='a top-level folder to leave out, e.g. "PIONEER REC"')
349
+ p.add_argument("--apply", action="store_true", help="copy (otherwise a dry run)")
350
+ p.add_argument("--allow-missing", action="store_true", help="clone even if some files can't be found")
351
+ p.add_argument("--remove-extra", action="store_true",
352
+ help="when refreshing a clone, delete target files the source no longer has")
353
+ p.add_argument("--verify-only", action="store_true",
354
+ help="compare an existing copy with the source, byte for byte; copies nothing")
355
+ p.add_argument("--no-verify", action="store_true", help="skip reading each file back (faster, not advised)")
356
+ p.set_defaults(fn=cmd_clone)
357
+
358
+ p = sub.add_parser("history", help="play histories the decks saved")
359
+ p.add_argument("target")
360
+ p.add_argument("--tracks", action="store_true", help="list each history's tracks")
361
+ p.add_argument("--scan", action="store_true", help="recovery read (see check --scan)")
362
+ p.add_argument("--second-word", action="store_true", help="see check --second-word")
363
+ p.set_defaults(fn=cmd_history)
364
+
365
+ p = sub.add_parser("snapshot", help="copy the stick's databases + list every file, read-only on the stick")
366
+ p.add_argument("stick")
367
+ p.add_argument("dest", help="folder to put the snapshot in")
368
+ p.add_argument("--label", help="snapshot folder name (default: date and time)")
369
+ p.add_argument("--full", action="store_true", help="copy the whole PIONEER folder (analysis files too)")
370
+ p.set_defaults(fn=cmd_snapshot)
371
+
372
+ p = sub.add_parser("readtest", help="read speed and slow spots of the stick")
373
+ p.add_argument("stick")
374
+ p.add_argument("--music", type=int, default=60, help="music files to sample")
375
+ p.add_argument("--analysis", type=int, default=1500, help="analysis files to sample")
376
+ p.set_defaults(fn=cmd_readtest)
377
+
378
+ for stream in (sys.stdout, sys.stderr): # track names in any script, on any console
379
+ if hasattr(stream, "reconfigure"):
380
+ stream.reconfigure(encoding="utf-8", errors="replace")
381
+ a = ap.parse_args(argv)
382
+ return a.fn(a)