]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/zfec/cmdline_zunfec.py
zfec: fix bug in in-line doc
[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 from util import argparse
9 import filefec
10
11 from zfec import __version__ as libversion
12 from util.version import Version
13 __version__ = Version("1.0.0a1-0-STABLE")
14
15 def main():
16     if '-V' in sys.argv or '--version' in sys.argv:
17         print "zfec library version: ", libversion
18         print "zunfec command-line tool version: ", __version__
19         return 0
20
21     parser = argparse.ArgumentParser(description="Decode data from share files.")
22
23     parser.add_argument('-o', '--outputfile', required=True, help='file to write the resulting data to, or "-" for stdout', type=str, metavar='OUTF')
24     parser.add_argument('sharefiles', nargs='*', help='shares file to read the encoded data from', type=unicode, metavar='SHAREFILE')
25     parser.add_argument('-v', '--verbose', help='print out messages about progress', action='store_true')
26     parser.add_argument('-f', '--force', help='overwrite any file which already in place of the output file', action='store_true')
27     parser.add_argument('-V', '--version', help='print out version number and exit', action='store_true')
28     args = parser.parse_args()
29
30     if len(args.sharefiles) < 2:
31         print "At least two sharefiles are required."
32         return 1
33
34     if args.force:
35         outf = open(args.outputfile, 'wb')
36     else:
37         try:
38             flags = os.O_WRONLY|os.O_CREAT|os.O_EXCL | (hasattr(os, 'O_BINARY') and os.O_BINARY)
39             outfd = os.open(args.outputfile, flags)
40         except OSError:
41             print "There is already a file named %r -- aborting.  Use --force to overwrite." % (args.outputfile,)
42             return 2
43         outf = os.fdopen(outfd, "wb")
44
45     sharefs = []
46     # This sort() actually matters for performance (shares with numbers < k
47     # are much faster to use than the others), as well as being important for
48     # reproducibility.
49     args.sharefiles.sort()
50     for fn in args.sharefiles:
51         sharefs.append(open(fn, 'rb'))
52     try:
53         ret = filefec.decode_from_files(outf, sharefs, args.verbose)
54     except filefec.InsufficientShareFilesError, e:
55         print str(e)
56         return 3
57
58     return 0
59
60 # zfec -- fast forward error correction library with Python interface
61
62 # Copyright (C) 2007 Allmydata, Inc.
63 # Author: Zooko Wilcox-O'Hearn
64
65 # This file is part of zfec.
66
67 # This program is free software; you can redistribute it and/or modify it
68 # under the terms of the GNU General Public License as published by the Free
69 # Software Foundation; either version 2 of the License, or (at your option)
70 # any later version, with the added permission that, if you become obligated
71 # to release a derived work under this licence (as per section 2.b), you may
72 # delay the fulfillment of this obligation for up to 12 months.  See the file
73 # COPYING for details.
74 #
75 # If you would like to inquire about a commercial relationship with Allmydata,
76 # Inc., please contact partnerships@allmydata.com and visit
77 # http://allmydata.com/.