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

from brutha.tree import Tree
from brutha.util import detect_cores, default_output
from brutha.output import OUTPUTS


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=OUTPUTS.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:
        stream = StringIO()
    else:
        stream = sys.stdout
    jobs = args.jobs if args.output in ('parallel', 'make') else 1
    out = OUTPUTS[args.output](args.echo, jobs)
    out.write(tree.commands(), stream)

    if args.execute:
        print >>sys.stderr, 'Synchronizing %s to %s, using %s concurrent jobs.' % (args.src, args.dest, jobs)
        sys.exit(out.run(stream))


if __name__ == '__main__':
    main()