blob: 23331aa2d0cc793b003fa0c6b54e642626130efe [file] [log] [blame]
Guido van Rossum85347411994-09-09 11:10:15 +00001# 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 Jansen0a2eaac1995-08-07 14:37:38 +000019#
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 Jansen8b745121995-08-30 12:19:30 +000024# - Arguments more compliant with python standard
Jack Jansen0a2eaac1995-08-07 14:37:38 +000025#
Guido van Rossum85347411994-09-09 11:10:15 +000026# This file implements the UUencode and UUdecode functions.
27
Jack Jansen8b745121995-08-30 12:19:30 +000028# encode(in_file, out_file [,name, mode])
29# decode(in_file [, out_file, mode])
Guido van Rossum85347411994-09-09 11:10:15 +000030
Jack Jansen0a2eaac1995-08-07 14:37:38 +000031import binascii
Jack Jansen8b745121995-08-30 12:19:30 +000032import os
33import string
Guido van Rossum85347411994-09-09 11:10:15 +000034
Jack Jansen8b745121995-08-30 12:19:30 +000035Error = 'uu.Error'
36
37def 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 Jansen5a49ffc1995-10-04 16:36:53 +000046 name = os.path.basename(in_file)
Jack Jansen8b745121995-08-30 12:19:30 +000047 if mode == None:
48 try:
Jack Jansen5a49ffc1995-10-04 16:36:53 +000049 mode = os.stat(in_file)[0]
Jack Jansen8b745121995-08-30 12:19:30 +000050 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 Rossum85347411994-09-09 11:10:15 +000071 str = in_file.read(45)
72 while len(str) > 0:
Jack Jansen0a2eaac1995-08-07 14:37:38 +000073 out_file.write(binascii.b2a_uu(str))
Guido van Rossum85347411994-09-09 11:10:15 +000074 str = in_file.read(45)
75 out_file.write(' \nend\n')
Guido van Rossum85347411994-09-09 11:10:15 +000076
Guido van Rossum85347411994-09-09 11:10:15 +000077
Jack Jansen8b745121995-08-30 12:19:30 +000078def 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 Rossum2ebaa171997-04-08 19:46:02 +000088 # Read until a begin is encountered or we've exhausted the file
Jack Jansen8b745121995-08-30 12:19:30 +000089 #
Guido van Rossum3ccd2f11997-04-08 19:46:53 +000090 while 1:
Guido van Rossum2ebaa171997-04-08 19:46:02 +000091 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 Jansen8b745121995-08-30 12:19:30 +0000103 if out_file == None:
104 out_file = hdrfields[2]
105 if mode == None:
Guido van Rossum2ebaa171997-04-08 19:46:02 +0000106 mode = string.atoi(hdrfields[1], 8)
Jack Jansen8b745121995-08-30 12:19:30 +0000107 #
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 Rossum85347411994-09-09 11:10:15 +0000114 try:
Jack Jansen8b745121995-08-30 12:19:30 +0000115 os.path.chmod(out_file, mode)
Guido van Rossum85347411994-09-09 11:10:15 +0000116 except AttributeError:
Jack Jansen8b745121995-08-30 12:19:30 +0000117 pass
118 out_file = fp
119 #
120 # Main decoding loop
121 #
Guido van Rossum85347411994-09-09 11:10:15 +0000122 str = in_file.readline()
Jack Jansen8b745121995-08-30 12:19:30 +0000123 while str and str != 'end\n':
Jack Jansen0a2eaac1995-08-07 14:37:38 +0000124 out_file.write(binascii.a2b_uu(str))
Guido van Rossum85347411994-09-09 11:10:15 +0000125 str = in_file.readline()
Jack Jansen8b745121995-08-30 12:19:30 +0000126 if not str:
127 raise Error, 'Truncated input file'
Guido van Rossum85347411994-09-09 11:10:15 +0000128
129def test():
Jack Jansen8b745121995-08-30 12:19:30 +0000130 """uuencode/uudecode main program"""
Guido van Rossum85347411994-09-09 11:10:15 +0000131 import sys
Jack Jansen8b745121995-08-30 12:19:30 +0000132 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 Rossum85347411994-09-09 11:10:15 +0000166 else:
Jack Jansen8b745121995-08-30 12:19:30 +0000167 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 Rossum85347411994-09-09 11:10:15 +0000174
175if __name__ == '__main__':
176 test()