This file is indexed.

/usr/bin/trackcmp is in audiotools 3.1.1-1+b1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/python3

# Audio Tools, a module and set of tools for manipulating audio data
# Copyright (C) 2007-2015  Brian Langenberger

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


import sys
import os
import os.path
import operator
import audiotools
import audiotools.text as _


def cmp_files(progress, audiofile1, audiofile2):
    """Returns (path1, path2, mismatch) tuple

    where mismatch is the int of the first PCM mismatch,
    None if the files match exactly or
    a negative value if some error occurs."""

    try:
        if os.path.samefile(audiofile1.filename, audiofile2.filename):
            return (audiofile1.filename,
                    audiofile2.filename,
                    None)
        elif ((audiofile1.sample_rate() != audiofile2.sample_rate()) or
              (audiofile1.bits_per_sample() != audiofile2.bits_per_sample()) or
              (audiofile1.channels() != audiofile2.channels())):
            return (audiofile1.filename,
                    audiofile2.filename,
                    -1)
        else:
            return (audiofile1.filename,
                    audiofile2.filename,
                    audiotools.pcm_frame_cmp(
                        audiotools.to_pcm_progress(audiofile1, progress),
                        audiofile2.to_pcm()))
    except (IOError, ValueError, KeyboardInterrupt, audiotools.DecodingError):
        return (audiofile1.filename,
                audiofile2.filename,
                -2)


def cmp_result(result, is_tty=False):
    (path1, path2, mismatch) = result

    assert(isinstance(path1, str))
    assert(isinstance(path2, str))
    assert((mismatch is None) or isinstance(mismatch, int))

    if mismatch is None:
        return ((_.LAB_TRACKCMP_CMP %
                 {"file1": audiotools.Filename(path1),
                  "file2": audiotools.Filename(path2)}) +
                u" : " + audiotools.output_text(
                    _.LAB_TRACKCMP_OK,
                    fg_color="green").format(is_tty))
    elif mismatch >= 0:
        return ((_.LAB_TRACKCMP_CMP %
                 {"file1": audiotools.Filename(path1),
                  "file2": audiotools.Filename(path2)}) +
                u" : " + audiotools.output_text(
                    _.LAB_TRACKCMP_MISMATCH %
                    {"frame_number": mismatch + 1},
                    fg_color="red").format(is_tty))
    elif mismatch == -1:
        return ((_.LAB_TRACKCMP_CMP %
                 {"file1": audiotools.Filename(path1),
                  "file2": audiotools.Filename(path2)}) +
                u" : " + audiotools.output_text(
                    _.LAB_TRACKCMP_PARAM_MISMATCH,
                    fg_color="red").format(is_tty))
    else:
        return ((_.LAB_TRACKCMP_CMP %
                 {"file1": audiotools.Filename(path1),
                  "file2": audiotools.Filename(path2)}) +
                u" : " + audiotools.output_text(
                    _.LAB_TRACKCMP_ERROR,
                    fg_color="red").format(is_tty))


def cmp_result_tty(result):
    return cmp_result(result, is_tty=True)


def image_compare(progress, image_audiofile, track_audiofile,
                  image_filename, track_filename,
                  pcm_frames_offset, total_pcm_frames):
    image_pcmreader = image_audiofile.to_pcm()

    # if image_pcmreader has seek(),
    # use it to reduce the amount of frames to skip
    if hasattr(image_pcmreader, "seek") and callable(image_pcmreader.seek):
        pcm_frames_offset -= image_pcmreader.seek(pcm_frames_offset)

    try:
        return (
            audiotools.pcm_frame_cmp(
                audiotools.PCMReaderWindow(image_pcmreader,
                                           pcm_frames_offset,
                                           total_pcm_frames),
                audiotools.PCMReaderProgress(track_audiofile.to_pcm(),
                                             total_pcm_frames,
                                             progress)),
            image_filename,
            track_filename)
    except (IOError, ValueError, KeyboardInterrupt, audiotools.DecodingError):
        return (-2, image_filename, track_filename)


def image_compare_raw(progress, source_filename,
                      sample_rate, channels, channel_mask, bits_per_sample,
                      track_audiofile, image_filename, track_filename,
                      pcm_frames_offset, total_pcm_frames):
    with open(source_filename, "rb") as f:
        # skip initial offset
        f.seek(pcm_frames_offset * channels * (bits_per_sample // 8))

        return (
            audiotools.pcm_frame_cmp(
                audiotools.PCMReaderHead(
                    audiotools.PCMFileReader(file=f,
                                             sample_rate=sample_rate,
                                             channels=channels,
                                             channel_mask=channel_mask,
                                             bits_per_sample=bits_per_sample),
                    total_pcm_frames),
                audiotools.PCMReaderProgress(track_audiofile.to_pcm(),
                                             total_pcm_frames,
                                             progress)),
            image_filename,
            track_filename)


def image_compare_results(result, is_tty=False):
    (mismatch, image_name, track_name) = result
    return u"%s : %s" % (_.LAB_TRACKCMP_CMP %
                         {"file1": audiotools.Filename(image_name),
                          "file2": audiotools.Filename(track_name)},
                         (audiotools.output_text(
                             _.LAB_TRACKCMP_OK,
                             fg_color="green").format(is_tty) if
                          mismatch is None else
                          audiotools.output_text(
                              _.LAB_TRACKCMP_MISMATCH %
                              {"frame_number": mismatch + 1},
                              fg_color="red").format(is_tty)))


def image_compare_results_tty(result):
    return image_compare_results(result, is_tty=True)


if (__name__ == '__main__'):
    import argparse

    parser = argparse.ArgumentParser(description=_.DESCRIPTION_TRACKCMP)

    parser.add_argument("--version",
                        action="version",
                        version="Python Audio Tools %s" % (audiotools.VERSION))

    parser.add_argument("-V", "--verbose",
                        action="store",
                        dest="verbosity",
                        choices=audiotools.VERBOSITY_LEVELS,
                        default=audiotools.DEFAULT_VERBOSITY,
                        help=_.OPT_VERBOSE)

    parser.add_argument("-j", "--joint",
                        type=int,
                        default=audiotools.MAX_JOBS,
                        dest="max_processes",
                        help=_.OPT_JOINT)

    parser.add_argument("-S", "--no-summary",
                        action="store_true",
                        dest="no_summary",
                        help=_.OPT_NO_SUMMARY)

    parser.add_argument("filename",
                        metavar="PATH",
                        help=_.OPT_INPUT_FILENAME_OR_IMAGE)

    parser.add_argument("filenames",
                        metavar="PATH",
                        nargs="+",
                        help=_.OPT_INPUT_FILENAME_OR_DIR)

    options = parser.parse_args()

    msg = audiotools.Messenger(options.verbosity == "quiet")

    args = [options.filename] + options.filenames

    if options.max_processes < 1:
        msg.error(_.ERR_INVALID_JOINT)
        sys.exit(1)

    check_function = audiotools.pcm_frame_cmp

    if len(args) == 2:
        if os.path.isfile(args[0]) and os.path.isfile(args[1]):
            # comparing two files

            filename_a = audiotools.Filename(args[0])
            filename_b = audiotools.Filename(args[1])

            try:
                audiofile_a = audiotools.open(args[0])
            except audiotools.UnsupportedFile:
                msg.error(_.ERR_UNSUPPORTED_FILE % (filename_a,))
                sys.exit(1)

            if not audiofile_a.__class__.supports_to_pcm():
                msg.error(_.ERR_UNSUPPORTED_TO_PCM %
                          {"filename": filename_a, "type": audiofile_a.NAME})
                sys.exit(1)

            try:
                audiofile_b = audiotools.open(args[1])
            except audiotools.UnsupportedFile:
                msg.error(_.ERR_UNSUPPORTED_FILE % (filename_b,))
                sys.exit(1)

            if not audiofile_b.__class__.supports_to_pcm():
                msg.error(_.ERR_UNSUPPORTED_TO_PCM %
                          {"filename": filename_b, "type": audiofile_b.NAME})
                sys.exit(1)

            (path1, path2, mismatch) = cmp_files(None,
                                                 audiofile_a,
                                                 audiofile_b)
            if mismatch is not None:
                msg.output(cmp_result((path1, path2, mismatch),
                                      msg.output_isatty()))
                sys.exit(1)
        elif os.path.isdir(args[0]) and os.path.isdir(args[1]):
            # comparing two directories

            to_compare = []
            results = []

            dir1 = args[0]
            files1 = {}
            dir2 = args[1]
            files2 = {}

            for (files, dir) in [(files1, dir1), (files2, dir2)]:
                for path in [os.path.join(dir, f) for f in os.listdir(dir)]:
                    if os.path.isfile(path):
                        try:
                            audiofile = audiotools.open(path)
                            if audiofile.__class__.supports_to_pcm():
                                files[audiofile.filename] = audiofile
                            else:
                                msg.warning(_.ERR_UNSUPPORTED_TO_PCM %
                                            {"filename": audiofile.filename,
                                             "type": audiofile.NAME})
                        except audiotools.UnsupportedFile:
                            pass
                        except audiotools.InvalidFile as err:
                            msg.warning(str(err))
                        except IOError:
                            msg.warning(_.ERR_OPEN_IOERROR % (path,))

            # first, attempt to match files by their stream characteristics
            streams1 = {}
            streams2 = {}

            for (files, streams) in [(files1, streams1),
                                     (files2, streams2)]:
                for f in files.values():
                    streams.setdefault((f.bits_per_sample(),
                                        f.channels(),
                                        f.sample_rate(),
                                        f.total_frames()), []).append(f)

            # anything with matching specs
            # and only a single possible match per directory
            # is queued for comparison
            for specs in set(streams1.keys()) & set(streams2.keys()):
                if (((len(streams1[specs]) == 1) and
                     (len(streams2[specs]) == 1))):
                    file1 = streams1[specs][0]
                    file2 = streams2[specs][0]

                    # remove matched files from lists
                    del(files1[file1.filename])
                    del(files2[file2.filename])

                    # queue up comparison job
                    to_compare.append((file1, file2))

            # then, attempt to match leftover files by metadata
            # such as album_number and track_number
            metadatas1 = {}
            metadatas2 = {}

            for (files, metadatas) in [(files1, metadatas1),
                                       (files2, metadatas2)]:
                for f in files.values():
                    m = f.get_metadata()
                    if m is not None:
                        metadatas.setdefault((m.track_number,
                                              m.album_number), []).append(f)
                    else:
                        metadatas.setdefault((None,
                                              None), []).append(f)

            for metadata in set(metadatas1.keys()) & set(metadatas2.keys()):
                if (((len(metadatas1[metadata]) == 1) and
                     (len(metadatas2[metadata]) == 1))):
                    file1 = metadatas1[metadata][0]
                    file2 = metadatas2[metadata][0]

                    # remove matched files from lists
                    del(files1[file1.filename])
                    del(files2[file2.filename])

                    # queue up comparison job
                    to_compare.append((file1, file2))

            # anything left over is marked as a missing file
            for (files, directory) in [(files1, args[1]), (files2, args[0])]:
                for filename in files.keys():
                    msg.info(
                        audiotools.output_text(
                            _.LAB_TRACKCMP_MISSING %
                            {"filename": audiotools.Filename(
                             os.path.basename(filename)),
                             "directory": audiotools.Filename(directory)},
                            fg_color="red").format(msg.info_isatty()))
                    sys.stdout.flush()
                    results.append((filename, None, 0))

            queue = audiotools.ExecProgressQueue(msg)

            for (track1, track2) in sorted(to_compare,
                                           key=lambda f: f[0].filename):
                queue.execute(
                    function=cmp_files,
                    progress_text=_.LAB_TRACKCMP_CMP %
                    {"file1": audiotools.Filename(track1.filename),
                     "file2": audiotools.Filename(track2.filename)},
                    completion_output=(cmp_result_tty
                                       if msg.output_isatty() else
                                       cmp_result),
                    audiofile1=track1,
                    audiofile2=track2)

            try:
                results.extend(queue.run(options.max_processes))
            except KeyboardInterrupt:
                msg.error(_.ERR_CANCELLED)
                sys.exit(1)
            successes = len([r for r in results if r[2] is None])
            failures = len(results) - successes

            if not options.no_summary:
                msg.output(_.LAB_TRACKCMP_RESULTS)
                msg.output(u"")

                table = audiotools.output_table()
                row = table.row()
                row.add_column(_.LAB_TRACKCMP_HEADER_SUCCESS, "right")
                row.add_column(u" ")
                row.add_column(_.LAB_TRACKCMP_HEADER_FAILURE, "right")
                row.add_column(u" ")
                row.add_column(_.LAB_TRACKCMP_HEADER_TOTAL, "right")

                table.divider_row([_.DIV, u" ", _.DIV, u" ", _.DIV])

                row = table.row()
                row.add_column(u"%d" % (successes), "right")
                row.add_column(u" ")
                row.add_column(u"%d" % (failures), "right")
                row.add_column(u" ")
                row.add_column(u"%d" % (successes + failures), "right")

                for row in table.format(msg.output_isatty()):
                    msg.output(row)

            if failures > 0:
                sys.exit(1)
        else:
            # comparison mismatch
            msg.error((_.LAB_TRACKCMP_CMP %
                       {"file1": audiotools.Filename(args[0]),
                        "file2": audiotools.Filename(args[1])}) +
                      u" : " +
                      audiotools.output_text(
                          _.LAB_TRACKCMP_TYPE_MISMATCH,
                          fg_color="red").format(msg.error_isatty()))
            sys.exit(1)
    elif len(args) > 2:
        # possibly comparing disk image against tracks
        audiofiles = []
        for arg in args:
            if os.path.isfile(arg):
                try:
                    audiofiles.append(audiotools.open(arg))
                except audiotools.UnsupportedFile:
                    pass
                except audiotools.InvalidFile as err:
                    msg.warning(str(err))
                except IOError:
                    msg.warning(_.ERR_OPEN_IOERROR % (path,))
        audiofiles.sort(key=lambda t: t.total_frames())

        if (sum(t.total_frames() for t in audiofiles[0:-1]) !=
            audiofiles[-1].total_frames()):
            msg.output(_.USAGE_TRACKCMP_CDIMAGE)
            sys.exit(1)

        cd_image = audiofiles[-1]
        tracks = audiofiles[0:-1]

        image_name = audiotools.Filename(cd_image.filename)

        # all tracks should have the same album number and track total
        tracks = audiotools.sorted_tracks(tracks)

        cd_data = audiotools.BufferedPCMReader(cd_image.to_pcm())

        queue = audiotools.ExecProgressQueue(msg)

        pcm_offset = 0

        if cd_image.seekable():
            for (i, track) in enumerate(tracks):
                track_name = audiotools.Filename(track.filename)
                queue.execute(
                    function=image_compare,
                    progress_text=_.LAB_TRACKCMP_CMP %
                    {"file1": image_name, "file2": track_name},
                    completion_output=(image_compare_results_tty
                                       if msg.output_isatty()
                                       else image_compare_results),
                    image_audiofile=cd_image,
                    track_audiofile=track,
                    image_filename=str(image_name),
                    track_filename=str(track_name),
                    pcm_frames_offset=pcm_offset,
                    total_pcm_frames=track.total_frames())

                pcm_offset += track.total_frames()

            try:
                if ({r[0] for r in
                     queue.run(options.max_processes)} != {None}):
                    sys.exit(1)
            except KeyboardInterrupt:
                msg.error(_.ERR_CANCELLED)
                sys.exit(1)
        else:
            import tempfile

            # if file isn't seekable

            # decode it to a single PCM blob of binary data
            temp_blob = tempfile.NamedTemporaryFile()
            cache_progress = audiotools.SingleProgressDisplay(
                msg, _.LAB_CACHING_FILE)
            try:
                audiotools.transfer_framelist_data(
                    audiotools.PCMReaderProgress(
                        cd_image.to_pcm(),
                        cd_image.total_frames(),
                        cache_progress.update),
                    temp_blob.write)
            except audiotools.DecodingError as err:
                cache_progress.clear_rows()
                msg.error(err)
                temp_blob.close()
                sys.exit(1)
            except KeyboardInterrupt:
                cache_progress.clear_rows()
                msg.error(_.ERR_CANCELLED)
                temp_blob.close()
                sys.exit(1)

            cache_progress.clear_rows()
            temp_blob.flush()

            # compare the blob using multiple jobs
            for (i, track) in enumerate(tracks):
                track_name = audiotools.Filename(track.filename)
                queue.execute(
                    function=image_compare_raw,
                    progress_text=_.LAB_TRACKCMP_CMP %
                    {"file1": image_name, "file2": track_name},
                    completion_output=(image_compare_results_tty
                                       if msg.output_isatty()
                                       else image_compare_results),
                    source_filename=temp_blob.name,
                    sample_rate=cd_image.sample_rate(),
                    channels=cd_image.channels(),
                    channel_mask=int(cd_image.channel_mask()),
                    bits_per_sample=cd_image.bits_per_sample(),
                    track_audiofile=track,
                    image_filename=str(image_name),
                    track_filename=str(track_name),
                    pcm_frames_offset=pcm_offset,
                    total_pcm_frames=track.total_frames())

                pcm_offset += track.total_frames()

            try:
                if ({r[0] for r in
                     queue.run(options.max_processes)} != {None}):
                    temp_blob.close()
                    sys.exit(1)
            except KeyboardInterrupt:
                msg.error(_.ERR_CANCELLED)
                temp_blob.close()
                sys.exit(1)

            # then delete the blob when finished
            temp_blob.close()
    else:
        msg.output(_.USAGE_TRACKCMP_FILES)
        sys.exit(1)