]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/zfec/cmdline_zfec.py
zfec: setup: use argparse and pyutil as separate packages, not by copying their sourc...
[tahoe-lafs/zfec.git] / zfec / zfec / cmdline_zfec.py
1 #!/usr/bin/env python
2
3 # zfec -- a fast C implementation of Reed-Solomon erasure coding with
4 # command-line, C, and Python interfaces
5
6 import sys
7
8 import argparse
9 import filefec
10
11 from zfec import __version__ as libversion
12 __version__ = libversion
13
14 def main():
15
16     if '-V' in sys.argv or '--version' in sys.argv:
17         print "zfec library version: ", libversion
18         print "zfec command-line tool version: ", __version__
19         sys.exit(0)
20
21     parser = argparse.ArgumentParser(description="Encode a file into a set of share files, a subset of which can later be used to recover the original file.")
22
23     parser.add_argument('inputfile', help='file to encode or "-" for stdin', type=argparse.FileType('rb'), metavar='INF')
24     parser.add_argument('-d', '--output-dir', help='directory in which share file names will be created (default ".")', default='.', metavar='D')
25     parser.add_argument('-p', '--prefix', help='prefix for share file names; If omitted, the name of the input file will be used.', metavar='P')
26     parser.add_argument('-s', '--suffix', help='suffix for share file names (default ".fec")', default='.fec', metavar='S')
27     parser.add_argument('-m', '--totalshares', help='the total number of share files created (default 8)', default=8, type=int, metavar='M')
28     parser.add_argument('-k', '--requiredshares', help='the number of share files required to reconstruct (default 3)', default=3, type=int, metavar='K')
29     parser.add_argument('-f', '--force', help='overwrite any file which already in place an output file (share file)', action='store_true')
30     parser.add_argument('-v', '--verbose', help='print out messages about progress', action='store_true')
31     parser.add_argument('-q', '--quiet', help='quiet progress indications and warnings about silly choices of K and M', action='store_true')
32     parser.add_argument('-V', '--version', help='print out version number and exit', action='store_true')
33     args = parser.parse_args()
34
35     if args.prefix is None:
36         args.prefix = args.inputfile.name
37         if args.prefix == "<stdin>":
38             args.prefix = ""
39
40     if args.verbose and args.quiet:
41         print "Please choose only one of --verbose and --quiet."
42         sys.exit(1)
43         
44     if args.totalshares > 256 or args.totalshares < 1:
45         print "Invalid parameters, totalshares is required to be <= 256 and >= 1\nPlease see the accompanying documentation."
46         sys.exit(1)
47     if args.requiredshares > args.totalshares or args.requiredshares < 1:
48         print "Invalid parameters, requiredshares is required to be <= totalshares and >= 1\nPlease see the accompanying documentation."
49         sys.exit(1)
50
51     if not args.quiet:
52         if args.requiredshares == 1:
53             print "warning: silly parameters: requiredshares == 1, which means that every share will be a complete copy of the file.  You could use \"cp\" for the same effect.  But proceeding to do it anyway..."
54         if args.requiredshares == args.totalshares:
55             print "warning: silly parameters: requiredshares == totalshares, which means that all shares will be required in order to reconstruct the file.  You could use \"split\" for the same effect.  But proceeding to do it anyway..."
56
57     args.inputfile.seek(0, 2)
58     fsize = args.inputfile.tell()
59     args.inputfile.seek(0, 0)
60     return filefec.encode_to_files(args.inputfile, fsize, args.output_dir, args.prefix, args.requiredshares, args.totalshares, args.suffix, args.force, args.verbose)
61
62 # zfec -- fast forward error correction library with Python interface
63
64 # Copyright (C) 2007 Allmydata, Inc.
65 # Author: Zooko Wilcox-O'Hearn
66
67 # This file is part of zfec.
68
69 # This program is free software; you can redistribute it and/or modify it
70 # under the terms of the GNU General Public License as published by the Free
71 # Software Foundation; either version 2 of the License, or (at your option)
72 # any later version, with the added permission that, if you become obligated
73 # to release a derived work under this licence (as per section 2.b), you may
74 # delay the fulfillment of this obligation for up to 12 months.  See the file
75 # COPYING for details.
76 #
77 # If you would like to inquire about a commercial relationship with Allmydata,
78 # Inc., please contact partnerships@allmydata.com and visit
79 # http://allmydata.com/.