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 | # |
| 88 | # Read the header line, and fill in optional args if needed |
| 89 | # |
| 90 | hdr = in_file.readline() |
| 91 | if not hdr: |
| 92 | raise Error, 'Empty input file' |
| 93 | hdrfields = string.split(hdr) |
| 94 | if len(hdrfields) <> 3 or hdrfields[0] <> 'begin': |
| 95 | raise Error, ('Incorrect uu header line', hdr) |
| 96 | if out_file == None: |
| 97 | out_file = hdrfields[2] |
| 98 | if mode == None: |
| 99 | mode = string.atoi(hdrfields[1]) |
| 100 | # |
| 101 | # Open the output file |
| 102 | # |
| 103 | if out_file == '-': |
| 104 | out_file = sys.stdout |
| 105 | elif type(out_file) == type(''): |
| 106 | fp = open(out_file, 'wb') |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 107 | try: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 108 | os.path.chmod(out_file, mode) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 109 | except AttributeError: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 110 | pass |
| 111 | out_file = fp |
| 112 | # |
| 113 | # Main decoding loop |
| 114 | # |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 115 | str = in_file.readline() |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 116 | while str and str != 'end\n': |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 117 | out_file.write(binascii.a2b_uu(str)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 118 | str = in_file.readline() |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 119 | if not str: |
| 120 | raise Error, 'Truncated input file' |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 121 | |
| 122 | def test(): |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 123 | """uuencode/uudecode main program""" |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 124 | import sys |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 125 | import getopt |
| 126 | |
| 127 | dopt = 0 |
| 128 | topt = 0 |
| 129 | input = sys.stdin |
| 130 | output = sys.stdout |
| 131 | ok = 1 |
| 132 | try: |
| 133 | optlist, args = getopt.getopt(sys.argv[1:], 'dt') |
| 134 | except getopt.error: |
| 135 | ok = 0 |
| 136 | if not ok or len(args) > 2: |
| 137 | print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]' |
| 138 | print ' -d: Decode (in stead of encode)' |
| 139 | print ' -t: data is text, encoded format unix-compatible text' |
| 140 | sys.exit(1) |
| 141 | |
| 142 | for o, a in optlist: |
| 143 | if o == '-d': dopt = 1 |
| 144 | if o == '-t': topt = 1 |
| 145 | |
| 146 | if len(args) > 0: |
| 147 | input = args[0] |
| 148 | if len(args) > 1: |
| 149 | output = args[1] |
| 150 | |
| 151 | if dopt: |
| 152 | if topt: |
| 153 | if type(output) == type(''): |
| 154 | output = open(output, 'w') |
| 155 | else: |
| 156 | print sys.argv[0], ': cannot do -t to stdout' |
| 157 | sys.exit(1) |
| 158 | decode(input, output) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 159 | else: |
Jack Jansen | 8b74512 | 1995-08-30 12:19:30 +0000 | [diff] [blame] | 160 | if topt: |
| 161 | if type(input) == type(''): |
| 162 | input = open(input, 'r') |
| 163 | else: |
| 164 | print sys.argv[0], ': cannot do -t from stdin' |
| 165 | sys.exit(1) |
| 166 | encode(input, output) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 167 | |
| 168 | if __name__ == '__main__': |
| 169 | test() |