|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 1994 by Lance Ellinghouse |
| 4 | +# Cathedral City, California Republic, United States of America. |
| 5 | +# All Rights Reserved |
| 6 | +# Permission to use, copy, modify, and distribute this software and its |
| 7 | +# documentation for any purpose and without fee is hereby granted, |
| 8 | +# provided that the above copyright notice appear in all copies and that |
| 9 | +# both that copyright notice and this permission notice appear in |
| 10 | +# supporting documentation, and that the name of Lance Ellinghouse |
| 11 | +# not be used in advertising or publicity pertaining to distribution |
| 12 | +# of the software without specific, written prior permission. |
| 13 | +# LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 14 | +# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 15 | +# FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE |
| 16 | +# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 17 | +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 19 | +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | +# |
| 21 | +# Modified by Jack Jansen, CWI, July 1995: |
| 22 | +# - Use binascii module to do the actual line-by-line conversion |
| 23 | +# between ascii and binary. This results in a 1000-fold speedup. The C |
| 24 | +# version is still 5 times faster, though. |
| 25 | +# - Arguments more compliant with python standard |
| 26 | + |
| 27 | +"""Implementation of the UUencode and UUdecode functions. |
| 28 | +
|
| 29 | +encode(in_file, out_file [,name, mode]) |
| 30 | +decode(in_file [, out_file, mode]) |
| 31 | +""" |
| 32 | + |
| 33 | +import binascii |
| 34 | +import os |
| 35 | +import sys |
| 36 | + |
| 37 | +__all__ = ["Error", "encode", "decode"] |
| 38 | + |
| 39 | + |
| 40 | +class Error(Exception): |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +def encode(in_file, out_file, name=None, mode=None): |
| 45 | + """Uuencode file""" |
| 46 | + # |
| 47 | + # If in_file is a pathname open it and change defaults |
| 48 | + # |
| 49 | + opened_files = [] |
| 50 | + try: |
| 51 | + if in_file == "-": |
| 52 | + in_file = sys.stdin.buffer |
| 53 | + elif isinstance(in_file, str): |
| 54 | + if name is None: |
| 55 | + name = os.path.basename(in_file) |
| 56 | + if mode is None: |
| 57 | + try: |
| 58 | + mode = os.stat(in_file).st_mode |
| 59 | + except AttributeError: |
| 60 | + pass |
| 61 | + in_file = open(in_file, "rb") |
| 62 | + opened_files.append(in_file) |
| 63 | + # |
| 64 | + # Open out_file if it is a pathname |
| 65 | + # |
| 66 | + if out_file == "-": |
| 67 | + out_file = sys.stdout.buffer |
| 68 | + elif isinstance(out_file, str): |
| 69 | + out_file = open(out_file, "wb") |
| 70 | + opened_files.append(out_file) |
| 71 | + # |
| 72 | + # Set defaults for name and mode |
| 73 | + # |
| 74 | + if name is None: |
| 75 | + name = "-" |
| 76 | + if mode is None: |
| 77 | + mode = 0o666 |
| 78 | + # |
| 79 | + # Write the data |
| 80 | + # |
| 81 | + out_file.write(("begin %o %s\n" % ((mode & 0o777), name)).encode("ascii")) |
| 82 | + data = in_file.read(45) |
| 83 | + while len(data) > 0: |
| 84 | + out_file.write(binascii.b2a_uu(data)) |
| 85 | + data = in_file.read(45) |
| 86 | + out_file.write(b" \nend\n") |
| 87 | + finally: |
| 88 | + for f in opened_files: |
| 89 | + f.close() |
| 90 | + |
| 91 | + |
| 92 | +def decode(in_file, out_file=None, mode=None, quiet=False): |
| 93 | + """Decode uuencoded file""" |
| 94 | + # |
| 95 | + # Open the input file, if needed. |
| 96 | + # |
| 97 | + opened_files = [] |
| 98 | + if in_file == "-": |
| 99 | + in_file = sys.stdin.buffer |
| 100 | + elif isinstance(in_file, str): |
| 101 | + in_file = open(in_file, "rb") |
| 102 | + opened_files.append(in_file) |
| 103 | + |
| 104 | + try: |
| 105 | + # |
| 106 | + # Read until a begin is encountered or we've exhausted the file |
| 107 | + # |
| 108 | + while True: |
| 109 | + hdr = in_file.readline() |
| 110 | + if not hdr: |
| 111 | + raise Error("No valid begin line found in input file") |
| 112 | + if not hdr.startswith(b"begin"): |
| 113 | + continue |
| 114 | + hdrfields = hdr.split(b" ", 2) |
| 115 | + if len(hdrfields) == 3 and hdrfields[0] == b"begin": |
| 116 | + try: |
| 117 | + int(hdrfields[1], 8) |
| 118 | + break |
| 119 | + except ValueError: |
| 120 | + pass |
| 121 | + if out_file is None: |
| 122 | + # If the filename isn't ASCII, what's up with that?!? |
| 123 | + out_file = hdrfields[2].rstrip(b" \t\r\n\f").decode("ascii") |
| 124 | + if os.path.exists(out_file): |
| 125 | + raise Error("Cannot overwrite existing file: %s" % out_file) |
| 126 | + if mode is None: |
| 127 | + mode = int(hdrfields[1], 8) |
| 128 | + # |
| 129 | + # Open the output file |
| 130 | + # |
| 131 | + if out_file == "-": |
| 132 | + out_file = sys.stdout.buffer |
| 133 | + elif isinstance(out_file, str): |
| 134 | + fp = open(out_file, "wb") |
| 135 | + try: |
| 136 | + os.path.chmod(out_file, mode) |
| 137 | + except AttributeError: |
| 138 | + pass |
| 139 | + out_file = fp |
| 140 | + opened_files.append(out_file) |
| 141 | + # |
| 142 | + # Main decoding loop |
| 143 | + # |
| 144 | + s = in_file.readline() |
| 145 | + while s and s.strip(b" \t\r\n\f") != b"end": |
| 146 | + try: |
| 147 | + data = binascii.a2b_uu(s) |
| 148 | + except binascii.Error as v: |
| 149 | + # Workaround for broken uuencoders by /Fredrik Lundh |
| 150 | + nbytes = (((s[0] - 32) & 63) * 4 + 5) // 3 |
| 151 | + data = binascii.a2b_uu(s[:nbytes]) |
| 152 | + if not quiet: |
| 153 | + sys.stderr.write("Warning: %s\n" % v) |
| 154 | + out_file.write(data) |
| 155 | + s = in_file.readline() |
| 156 | + if not s: |
| 157 | + raise Error("Truncated input file") |
| 158 | + finally: |
| 159 | + for f in opened_files: |
| 160 | + f.close() |
| 161 | + |
| 162 | + |
| 163 | +def test(): |
| 164 | + """uuencode/uudecode main program""" |
| 165 | + |
| 166 | + import optparse |
| 167 | + |
| 168 | + parser = optparse.OptionParser(usage="usage: %prog [-d] [-t] [input [output]]") |
| 169 | + parser.add_option( |
| 170 | + "-d", |
| 171 | + "--decode", |
| 172 | + dest="decode", |
| 173 | + help="Decode (instead of encode)?", |
| 174 | + default=False, |
| 175 | + action="store_true", |
| 176 | + ) |
| 177 | + parser.add_option( |
| 178 | + "-t", |
| 179 | + "--text", |
| 180 | + dest="text", |
| 181 | + help="data is text, encoded format unix-compatible text?", |
| 182 | + default=False, |
| 183 | + action="store_true", |
| 184 | + ) |
| 185 | + |
| 186 | + (options, args) = parser.parse_args() |
| 187 | + if len(args) > 2: |
| 188 | + parser.error("incorrect number of arguments") |
| 189 | + sys.exit(1) |
| 190 | + |
| 191 | + # Use the binary streams underlying stdin/stdout |
| 192 | + input = sys.stdin.buffer |
| 193 | + output = sys.stdout.buffer |
| 194 | + if len(args) > 0: |
| 195 | + input = args[0] |
| 196 | + if len(args) > 1: |
| 197 | + output = args[1] |
| 198 | + |
| 199 | + if options.decode: |
| 200 | + if options.text: |
| 201 | + if isinstance(output, str): |
| 202 | + output = open(output, "wb") |
| 203 | + else: |
| 204 | + print(sys.argv[0], ": cannot do -t to stdout") |
| 205 | + sys.exit(1) |
| 206 | + decode(input, output) |
| 207 | + else: |
| 208 | + if options.text: |
| 209 | + if isinstance(input, str): |
| 210 | + input = open(input, "rb") |
| 211 | + else: |
| 212 | + print(sys.argv[0], ": cannot do -t from stdin") |
| 213 | + sys.exit(1) |
| 214 | + encode(input, output) |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + test() |
| 219 | + |
| 220 | + |
| 221 | +__version__ = '0.5.1' |
0 commit comments