aboutsummaryrefslogtreecommitdiffstats
path: root/brutha/__main__.py
blob: b839a591391f5f857442ad0eb131deddd49c94ef (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import argparse
import sys
from multiprocessing import cpu_count
from StringIO import StringIO

from .output import OUTPUTS
from .tree import Tree
from .util import default_output


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

    parser = argparse.ArgumentParser(
        description="Sync FLAC music files to Ogg Vorbis (or keep lossy as-is).",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('-q', '--quality', default=8, type=int,
                        help="Ogg Vorbis quality, -1 to 10")
    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")
    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")
    parser.add_argument('-O', '--outside', action='store_true',
                        help="Follow directory symbolic links outside of the source directory")
    parser.add_argument('-I', '--inside', action='store_true',
                        help="Follow directory symbolic links inside of the source directory")
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-l', '--hardlink', action='store_true', help="Use hardlinks instead of a copy")
    group.add_argument('-r', '--reflink', action='store_true', help="Use reflinks instead of a copy")
    parser.add_argument('src', help="Source directory", metavar='SOURCE')
    parser.add_argument('dest', help="Destination directory", metavar='DESTINATION')
    args = parser.parse_args()

    log = sys.stderr
    tree = Tree(args.src, args.dest,
                {'quality': args.quality, 'gain': args.gain, 'delete': args.delete,
                 'maxrate': args.maxrate, 'maxbits': args.maxbits,
                 'lossycheck': args.lossycheck,
                 'hardlink': args.hardlink, 'reflink': args.reflink,
                 'inside': args.inside, 'outside': args.outside},
                log)
    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 >>log, "Synchronizing %s to %s, using %s concurrent jobs." % (args.src, args.dest, jobs)
        sys.exit(out.run(stream))


if __name__ == '__main__':
    main()