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. |
| 24 | # |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 25 | # This file implements the UUencode and UUdecode functions. |
| 26 | |
| 27 | # encode(filename, mode, in_file, out_file) |
| 28 | # decode(filename, mode, in_file) |
| 29 | # decode(in_file, out_file) |
| 30 | # decode(in_file) |
| 31 | |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 32 | import binascii |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 34 | # encode a fileobject and write out to a file object |
| 35 | def encode(filename, mode, in_file, out_file): |
| 36 | out_file.write('begin %o %s\n' % ((mode&0777),filename)) |
| 37 | str = in_file.read(45) |
| 38 | while len(str) > 0: |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 39 | out_file.write(binascii.b2a_uu(str)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 40 | str = in_file.read(45) |
| 41 | out_file.write(' \nend\n') |
| 42 | return None |
| 43 | |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 44 | |
| 45 | # decode(filename, mode, in_file) |
| 46 | # decode(in_file, out_file) |
| 47 | # decode(in_file) |
| 48 | def decode(*args): |
| 49 | ok = 1 |
| 50 | _setup = None |
| 51 | out_file = None |
| 52 | if len(args) == 3: |
| 53 | filename, mode, in_file = args |
| 54 | if type(filename) != type(''): |
| 55 | ok = 0 |
| 56 | if type(mode) != type(0): |
| 57 | ok = 0 |
| 58 | try: |
| 59 | _ = getattr(in_file,'readline') |
| 60 | except AttributeError: |
| 61 | ok = 0 |
| 62 | def _setup(out_file,args): |
| 63 | filename, mode, in_file = args |
| 64 | # open file as specified and assign out_file for later use |
| 65 | out_file = open(filename,'w',mode) |
| 66 | _out_file_orig = 0 |
| 67 | _ = in_file.readline() |
| 68 | return (out_file,_out_file_orig) |
| 69 | elif len(args) == 2: |
| 70 | in_file, out_file = args |
| 71 | try: |
| 72 | _ = getattr(in_file,'readline') |
| 73 | _ = getattr(out_file,'write') |
| 74 | except AttributeError: |
| 75 | ok = 0 |
| 76 | def _setup(out_file, args): |
| 77 | in_file, out_file = args |
| 78 | # Toss the 'begin mode filename' part.. not needed |
| 79 | _ = in_file.readline() |
| 80 | _out_file_orig = 1 |
| 81 | return (out_file,_out_file_orig) |
| 82 | elif len(args) == 1: |
| 83 | in_file = args[0] |
| 84 | try: |
| 85 | _ = getattr(in_file,'readline') |
| 86 | except AttributeError: |
| 87 | ok = 0 |
| 88 | def _setup(out_file, args): |
| 89 | import strop |
| 90 | in_file = args[0] |
| 91 | # open file as specified in uu file and |
| 92 | # assign out_file for later use |
| 93 | i = in_file.readline() |
| 94 | i = strop.strip(i) |
| 95 | if 'begin' != i[:5]: |
| 96 | raise IOError, 'input file not in UUencoded format' |
| 97 | [dummy, mode, filename] = strop.split(i) |
| 98 | mode = strop.atoi(mode, 8) |
| 99 | out_file = open(filename,'w',mode) |
| 100 | _out_file_orig = 0 |
| 101 | return (out_file,_out_file_orig) |
| 102 | if ok != 1: |
| 103 | raise SyntaxError, 'must be (filename, mode, in_file) or (in_file,out_file) or (in_file)' |
| 104 | out_file, _out_file_orig = _setup(out_file, args) |
| 105 | str = in_file.readline() |
| 106 | while len(str) > 0 and str != ' \n' and str != 'end\n': |
Jack Jansen | 0a2eaac | 1995-08-07 14:37:38 +0000 | [diff] [blame] | 107 | out_file.write(binascii.a2b_uu(str)) |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 108 | str = in_file.readline() |
| 109 | if _out_file_orig == 0: |
| 110 | out_file.close() |
| 111 | del out_file |
| 112 | return None |
| 113 | |
| 114 | def test(): |
| 115 | import sys |
| 116 | if sys.argv[1:2] == ['-d']: |
| 117 | if sys.argv[2:]: |
| 118 | decode(open(sys.argv[2]), sys.stdout) |
| 119 | else: |
| 120 | decode(sys.stdin, sys.stdout) |
| 121 | elif sys.argv[1:2] == ['-e']: |
| 122 | if sys.argv[2:]: |
| 123 | file = sys.argv[2] |
| 124 | fp = open(file) |
| 125 | else: |
| 126 | file = '-' |
| 127 | fp = sys.stdin |
| 128 | encode(file, 0644, fp, sys.stdout) |
| 129 | else: |
| 130 | print 'usage: uu -d [file]; (to decode)' |
| 131 | print 'or: uu -e [file]; (to encode)' |
| 132 | sys.exit(2) |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | test() |