aboutsummaryrefslogtreecommitdiffstats
path: root/brutha/__main__.py
blob: f575090cccaf5175d3deb58cb6721ecbff95b7d3 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import sys
from StringIO import StringIO

from brutha.tree import Tree
from brutha.util import uprint, detect_cores, default_output
from brutha.output import PRINTERS, EXECUTORS


def main():
    cores = detect_cores()
    output = default_output(cores)

    parser = argparse.ArgumentParser(
        description='Sync FLAC music files to Ogg Vorbis (or keep lossy as-is).')
    parser.add_argument('-q', '--quality', default=8, type=int,
                        help='Ogg Vorbis quality, -1 to 10, default 8')
    parser.add_argument('-g', '--gain', action='store_true',
                        help='Compute ReplayGain if missing')
    parser.add_argument('-d', '--delete', action='store_true',
                        help='Delete extraneous files in destination')
    parser.add_argument('-o', '--output', default=output, choices=PRINTERS.keys(),
                        help='Command list type, default %s' % output)
    parser.add_argument('-R', '--maxrate', type=int,
                        help='Maximum sample rate allowed (e.g. 44100)', metavar='RATE')
    parser.add_argument('-B', '--maxbits', type=int,
                        help='Maximum bit depth allowed (e.g. 16)', metavar='BITS')
    parser.add_argument('-L', '--lossycheck', action='store_true',
                        help='Ignore lossy files with too high sample rate or bit depth')
    parser.add_argument('-e', '--echo', action='store_true',
                        help='Show started commands')
    parser.add_argument('-x', '--execute', action='store_true',
                        help='Execute the script instead of printing it')
    parser.add_argument('-j', '--jobs', type=int, default=cores,
                        help='Number of concurrent jobs, default %s' % cores)
    parser.add_argument('src', help='Source directory', metavar='SOURCE')
    parser.add_argument('dest', help='Destination directory', metavar='DESTINATION')
    args = parser.parse_args()

    tree = Tree(args.src, args.dest,
                {'quality': args.quality, 'gain': args.gain, 'delete': args.delete,
                 'maxrate': args.maxrate, 'maxbits': args.maxbits, 'lossycheck': args.lossycheck})
    if args.execute:
        s = StringIO()
        p = uprint(s)
    else:
        p = uprint(sys.stdout)
    outf = PRINTERS[args.output]
    outf(p, tree.commands(), args.echo, args.jobs)

    if args.execute:
        jobs = args.jobs if args.output in ('parallel', 'make') else 1
        print >>sys.stderr, 'Synchronizing %s to %s, using %s concurrent jobs.' % (args.src, args.dest, jobs)
        exef = EXECUTORS[args.output]
        sys.exit(exef(s, args.jobs))


if __name__ == '__main__':
    main()