]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/zfec/cmdline_zunfec.py
docs: update docs and metadata
[tahoe-lafs/zfec.git] / zfec / zfec / cmdline_zunfec.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 os, sys
7
8 import argparse
9 import filefec
10
11 from zfec import __version__ as libversion
12 __version__ = libversion
13
14 def main():
15     if '-V' in sys.argv or '--version' in sys.argv:
16         print "zfec library version: ", libversion
17         print "zunfec command-line tool version: ", __version__
18         return 0
19
20     parser = argparse.ArgumentParser(description="Decode data from share files.")
21
22     parser.add_argument('-o', '--outputfile', required=True, help='file to write the resulting data to, or "-" for stdout', type=str, metavar='OUTF')
23     parser.add_argument('sharefiles', nargs='*', help='shares file to read the encoded data from', type=unicode, metavar='SHAREFILE')
24     parser.add_argument('-v', '--verbose', help='print out messages about progress', action='store_true')
25     parser.add_argument('-f', '--force', help='overwrite any file which already in place of the output file', action='store_true')
26     parser.add_argument('-V', '--version', help='print out version number and exit', action='store_true')
27     args = parser.parse_args()
28
29     if len(args.sharefiles) < 2:
30         print "At least two sharefiles are required."
31         return 1
32
33     if args.force:
34         outf = open(args.outputfile, 'wb')
35     else:
36         try:
37             flags = os.O_WRONLY|os.O_CREAT|os.O_EXCL | (hasattr(os, 'O_BINARY') and os.O_BINARY)
38             outfd = os.open(args.outputfile, flags)
39         except OSError:
40             print "There is already a file named %r -- aborting.  Use --force to overwrite." % (args.outputfile,)
41             return 2
42         outf = os.fdopen(outfd, "wb")
43
44     sharefs = []
45     # This sort() actually matters for performance (shares with numbers < k
46     # are much faster to use than the others), as well as being important for
47     # reproducibility.
48     args.sharefiles.sort()
49     for fn in args.sharefiles:
50         sharefs.append(open(fn, 'rb'))
51     try:
52         ret = filefec.decode_from_files(outf, sharefs, args.verbose)
53     except filefec.InsufficientShareFilesError, e:
54         print str(e)
55         return 3
56
57     return 0
58
59 # zfec -- fast forward error correction library with Python interface
60
61 # Copyright (C) 2007 Allmydata, Inc.
62 # Author: Zooko Wilcox-O'Hearn
63
64 # This file is part of zfec.
65 #
66 # See README.rst for licensing information.