File: cmdline_zunfec.py

package info (click to toggle)
zfec 1.5.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 524 kB
  • sloc: python: 2,022; ansic: 892; haskell: 229; sh: 22; makefile: 4
file content (71 lines) | stat: -rwxr-xr-x 2,542 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

# zfec -- a fast C implementation of Reed-Solomon erasure coding with
# command-line, C, and Python interfaces

from __future__ import print_function

import os, sys, argparse

from zfec import filefec

from zfec import __version__ as libversion
__version__ = libversion

def main():
    if '-V' in sys.argv or '--version' in sys.argv:
        print("zfec library version: ", libversion)
        print("zunfec command-line tool version: ", __version__)
        return 0

    parser = argparse.ArgumentParser(description="Decode data from share files.")

    parser.add_argument('-o', '--outputfile', required=True, help='file to write the resulting data to, or "-" for stdout', type=str, metavar='OUTF')
    parser.add_argument('sharefiles', nargs='*', help='shares file to read the encoded data from', type=str, metavar='SHAREFILE')
    parser.add_argument('-v', '--verbose', help='print out messages about progress', action='store_true')
    parser.add_argument('-f', '--force', help='overwrite any file which already in place of the output file', action='store_true')
    parser.add_argument('-V', '--version', help='print out version number and exit', action='store_true')
    args = parser.parse_args()

    if len(args.sharefiles) < 2:
        print("At least two sharefiles are required.")
        return 1

    if args.force:
        outf = open(args.outputfile, 'wb')
    else:
        try:
            flags = os.O_WRONLY|os.O_CREAT|os.O_EXCL | (hasattr(os, 'O_BINARY') and os.O_BINARY)
            outfd = os.open(args.outputfile, flags)
        except OSError:
            print("There is already a file named %r -- aborting.  Use --force to overwrite." % (args.outputfile,))
            return 2
        outf = os.fdopen(outfd, "wb")

    sharefs = []
    # This sort() actually matters for performance (shares with numbers < k
    # are much faster to use than the others), as well as being important for
    # reproducibility.
    args.sharefiles.sort()
    for fn in args.sharefiles:
        sharefs.append(open(fn, 'rb'))
    try:
        filefec.decode_from_files(outf, sharefs, args.verbose)
    except filefec.InsufficientShareFilesError as e:
        print(str(e))
        return 3
    finally:
        outf.close()
        for f in sharefs:
            f.close()

    return 0

# zfec -- fast forward error correction library with Python interface
# 
# Copyright (C) 2007 Allmydata, Inc.
# Author: Zooko Wilcox-O'Hearn
# 
# This file is part of zfec.
#
# See README.rst for licensing information.