blob: 2341bbd7e3d30ae8201fae9cba1a4129bfbe7656 [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:
46 name = basename(in_file)
47 if mode == None:
48 try:
49 mode = os.path.stat(in_file)[0]
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 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 #
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 Rossum85347411994-09-09 11:10:15 +0000107 try:
Jack Jansen8b745121995-08-30 12:19:30 +0000108 os.path.chmod(out_file, mode)
Guido van Rossum85347411994-09-09 11:10:15 +0000109 except AttributeError:
Jack Jansen8b745121995-08-30 12:19:30 +0000110 pass
111 out_file = fp
112 #
113 # Main decoding loop
114 #
Guido van Rossum85347411994-09-09 11:10:15 +0000115 str = in_file.readline()
Jack Jansen8b745121995-08-30 12:19:30 +0000116 while str and str != 'end\n':
Jack Jansen0a2eaac1995-08-07 14:37:38 +0000117 out_file.write(binascii.a2b_uu(str))
Guido van Rossum85347411994-09-09 11:10:15 +0000118 str = in_file.readline()
Jack Jansen8b745121995-08-30 12:19:30 +0000119 if not str:
120 raise Error, 'Truncated input file'
Guido van Rossum85347411994-09-09 11:10:15 +0000121
122def test():
Jack Jansen8b745121995-08-30 12:19:30 +0000123 """uuencode/uudecode main program"""
Guido van Rossum85347411994-09-09 11:10:15 +0000124 import sys
Jack Jansen8b745121995-08-30 12:19:30 +0000125 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 Rossum85347411994-09-09 11:10:15 +0000159 else:
Jack Jansen8b745121995-08-30 12:19:30 +0000160 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 Rossum85347411994-09-09 11:10:15 +0000167
168if __name__ == '__main__':
169 test()