Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 1 | # uu.py |
| 2 | # Copyright 1994 by Lance Ellinghouse |
| 3 | # Cathedral City, California Republic, United States of America. |
| 4 | # All Rights Reserved |
| 5 | # Permission to use, copy, modify, and distribute this software and its |
| 6 | # documentation for any purpose and without fee is hereby granted, |
| 7 | # provided that the above copyright notice appear in all copies and that |
| 8 | # both that copyright notice and this permission notice appear in |
| 9 | # supporting documentation, and that the name of Lance Ellinghouse |
| 10 | # not be used in advertising or publicity pertaining to distribution |
| 11 | # of the software without specific, written prior permission. |
| 12 | # LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 13 | # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 14 | # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE |
| 15 | # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 18 | # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 19 | # |
| 20 | # Modified by Jack Jansen, CWI, July 1995: |
| 21 | # - Use binascii module to do the actual line-by-line conversion |
| 22 | # between ascii and binary. This results in a 1000-fold speedup. The C |
| 23 | # version is still 5 times faster, though. |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 24 | # - Arguments more compliant with python standard |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 25 | # |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 26 | # This file implements the UUencode and UUdecode functions. |
| 27 | |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 28 | # encode(in_file, out_file [,name, mode]) |
| 29 | # decode(in_file [, out_file, mode]) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 30 | |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 31 | import binascii |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 32 | import os |
| 33 | import string |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 34 | |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 35 | Error = 'uu.Error' |
| 36 | |
| 37 | def encode(in_file, out_file, name=None, mode=None): |
| 38 | """Uuencode file""" |
| 39 | # |
| 40 | # If in_file is a pathname open it and change defaults |
| 41 | # |
| 42 | if in_file == '-': |
| 43 | in_file = sys.stdin |
| 44 | elif type(in_file) == type(''): |
| 45 | if name == None: |
Jack Jansen | 5a49ffc | 1995-10-04 16:36:53 +0000 | [diff] [blame] | 46 | name = os.path.basename(in_file) |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 47 | if mode == None: |
| 48 | try: |
Jack Jansen | 5a49ffc | 1995-10-04 16:36:53 +0000 | [diff] [blame] | 49 | mode = os.stat(in_file)[0] |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 50 | except AttributeError: |
| 51 | pass |
| 52 | in_file = open(in_file, 'rb') |
| 53 | # |
| 54 | # Open out_file if it is a pathname |
| 55 | # |
| 56 | if out_file == '-': |
| 57 | out_file = sys.stdout |
| 58 | elif type(out_file) == type(''): |
| 59 | out_file = open(out_file, 'w') |
| 60 | # |
| 61 | # Set defaults for name and mode |
| 62 | # |
| 63 | if name == None: |
| 64 | name = '-' |
| 65 | if mode == None: |
| 66 | mode = 0666 |
| 67 | # |
| 68 | # Write the data |
| 69 | # |
| 70 | out_file.write('begin %o %s\n' % ((mode&0777),name)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 71 | str = in_file.read(45) |
| 72 | while len(str) > 0: |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 73 | out_file.write(binascii.b2a_uu(str)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 74 | str = in_file.read(45) |
| 75 | out_file.write(' \nend\n') |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 77 | |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 78 | def decode(in_file, out_file=None, mode=None): |
| 79 | """Decode uuencoded file""" |
| 80 | # |
| 81 | # Open the input file, if needed. |
| 82 | # |
| 83 | if in_file == '-': |
| 84 | in_file = sys.stdin |
| 85 | elif type(in_file) == type(''): |
| 86 | in_file = open(in_file) |
| 87 | # |
Guido van Rossum | 2ebaa17 | 1997-04-08 19:46:02 +0000 | [diff] [blame] | 88 | # Read until a begin is encountered or we've exhausted the file |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 89 | # |
Guido van Rossum | 3ccd2f1 | 1997-04-08 19:46:53 +0000 | [diff] [blame] | 90 | while 1: |
Guido van Rossum | 2ebaa17 | 1997-04-08 19:46:02 +0000 | [diff] [blame] | 91 | hdr = in_file.readline() |
| 92 | if not hdr: |
| 93 | raise Error, 'No valid begin line found in input file' |
| 94 | if hdr[:5] != 'begin': |
| 95 | continue |
| 96 | hdrfields = string.split(hdr) |
| 97 | if len(hdrfields) == 3 and hdrfields[0] == 'begin': |
| 98 | try: |
| 99 | string.atoi(hdrfields[1], 8) |
| 100 | break |
| 101 | except ValueError: |
| 102 | pass |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 103 | if out_file == None: |
| 104 | out_file = hdrfields[2] |
| 105 | if mode == None: |
Guido van Rossum | 2ebaa17 | 1997-04-08 19:46:02 +0000 | [diff] [blame] | 106 | mode = string.atoi(hdrfields[1], 8) |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 107 | # |
| 108 | # Open the output file |
| 109 | # |
| 110 | if out_file == '-': |
| 111 | out_file = sys.stdout |
| 112 | elif type(out_file) == type(''): |
| 113 | fp = open(out_file, 'wb') |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 114 | try: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 115 | os.path.chmod(out_file, mode) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 116 | except AttributeError: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 117 | pass |
| 118 | out_file = fp |
| 119 | # |
| 120 | # Main decoding loop |
| 121 | # |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 122 | str = in_file.readline() |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 123 | while str and str != 'end\n': |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 124 | out_file.write(binascii.a2b_uu(str)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 125 | str = in_file.readline() |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 126 | if not str: |
| 127 | raise Error, 'Truncated input file' |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 128 | |
| 129 | def test(): |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 130 | """uuencode/uudecode main program""" |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 131 | import sys |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 132 | import getopt |
| 133 | |
| 134 | dopt = 0 |
| 135 | topt = 0 |
| 136 | input = sys.stdin |
| 137 | output = sys.stdout |
| 138 | ok = 1 |
| 139 | try: |
| 140 | optlist, args = getopt.getopt(sys.argv[1:], 'dt') |
| 141 | except getopt.error: |
| 142 | ok = 0 |
| 143 | if not ok or len(args) > 2: |
| 144 | print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]' |
| 145 | print ' -d: Decode (in stead of encode)' |
| 146 | print ' -t: data is text, encoded format unix-compatible text' |
| 147 | sys.exit(1) |
| 148 | |
| 149 | for o, a in optlist: |
| 150 | if o == '-d': dopt = 1 |
| 151 | if o == '-t': topt = 1 |
| 152 | |
| 153 | if len(args) > 0: |
| 154 | input = args[0] |
| 155 | if len(args) > 1: |
| 156 | output = args[1] |
| 157 | |
| 158 | if dopt: |
| 159 | if topt: |
| 160 | if type(output) == type(''): |
| 161 | output = open(output, 'w') |
| 162 | else: |
| 163 | print sys.argv[0], ': cannot do -t to stdout' |
| 164 | sys.exit(1) |
| 165 | decode(input, output) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 166 | else: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 167 | if topt: |
| 168 | if type(input) == type(''): |
| 169 | input = open(input, 'r') |
| 170 | else: |
| 171 | print sys.argv[0], ': cannot do -t from stdin' |
| 172 | sys.exit(1) |
| 173 | encode(input, output) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 174 | |
| 175 | if __name__ == '__main__': |
| 176 | test() |