Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: uu.py
#! /usr/bin/env python
[0] Fix | Delete
[1] Fix | Delete
# Copyright 1994 by Lance Ellinghouse
[2] Fix | Delete
# Cathedral City, California Republic, United States of America.
[3] Fix | Delete
# All Rights Reserved
[4] Fix | Delete
# Permission to use, copy, modify, and distribute this software and its
[5] Fix | Delete
# documentation for any purpose and without fee is hereby granted,
[6] Fix | Delete
# provided that the above copyright notice appear in all copies and that
[7] Fix | Delete
# both that copyright notice and this permission notice appear in
[8] Fix | Delete
# supporting documentation, and that the name of Lance Ellinghouse
[9] Fix | Delete
# not be used in advertising or publicity pertaining to distribution
[10] Fix | Delete
# of the software without specific, written prior permission.
[11] Fix | Delete
# LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
[12] Fix | Delete
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
[13] Fix | Delete
# FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
[14] Fix | Delete
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
[15] Fix | Delete
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
[16] Fix | Delete
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
[17] Fix | Delete
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
[18] Fix | Delete
#
[19] Fix | Delete
# Modified by Jack Jansen, CWI, July 1995:
[20] Fix | Delete
# - Use binascii module to do the actual line-by-line conversion
[21] Fix | Delete
# between ascii and binary. This results in a 1000-fold speedup. The C
[22] Fix | Delete
# version is still 5 times faster, though.
[23] Fix | Delete
# - Arguments more compliant with python standard
[24] Fix | Delete
[25] Fix | Delete
"""Implementation of the UUencode and UUdecode functions.
[26] Fix | Delete
[27] Fix | Delete
encode(in_file, out_file [,name, mode])
[28] Fix | Delete
decode(in_file [, out_file, mode])
[29] Fix | Delete
"""
[30] Fix | Delete
[31] Fix | Delete
import binascii
[32] Fix | Delete
import os
[33] Fix | Delete
import sys
[34] Fix | Delete
[35] Fix | Delete
__all__ = ["Error", "encode", "decode"]
[36] Fix | Delete
[37] Fix | Delete
class Error(Exception):
[38] Fix | Delete
pass
[39] Fix | Delete
[40] Fix | Delete
def encode(in_file, out_file, name=None, mode=None):
[41] Fix | Delete
"""Uuencode file"""
[42] Fix | Delete
#
[43] Fix | Delete
# If in_file is a pathname open it and change defaults
[44] Fix | Delete
#
[45] Fix | Delete
opened_files = []
[46] Fix | Delete
try:
[47] Fix | Delete
if in_file == '-':
[48] Fix | Delete
in_file = sys.stdin
[49] Fix | Delete
elif isinstance(in_file, basestring):
[50] Fix | Delete
if name is None:
[51] Fix | Delete
name = os.path.basename(in_file)
[52] Fix | Delete
if mode is None:
[53] Fix | Delete
try:
[54] Fix | Delete
mode = os.stat(in_file).st_mode
[55] Fix | Delete
except AttributeError:
[56] Fix | Delete
pass
[57] Fix | Delete
in_file = open(in_file, 'rb')
[58] Fix | Delete
opened_files.append(in_file)
[59] Fix | Delete
#
[60] Fix | Delete
# Open out_file if it is a pathname
[61] Fix | Delete
#
[62] Fix | Delete
if out_file == '-':
[63] Fix | Delete
out_file = sys.stdout
[64] Fix | Delete
elif isinstance(out_file, basestring):
[65] Fix | Delete
out_file = open(out_file, 'wb')
[66] Fix | Delete
opened_files.append(out_file)
[67] Fix | Delete
#
[68] Fix | Delete
# Set defaults for name and mode
[69] Fix | Delete
#
[70] Fix | Delete
if name is None:
[71] Fix | Delete
name = '-'
[72] Fix | Delete
if mode is None:
[73] Fix | Delete
mode = 0666
[74] Fix | Delete
#
[75] Fix | Delete
# Write the data
[76] Fix | Delete
#
[77] Fix | Delete
out_file.write('begin %o %s\n' % ((mode&0777),name))
[78] Fix | Delete
data = in_file.read(45)
[79] Fix | Delete
while len(data) > 0:
[80] Fix | Delete
out_file.write(binascii.b2a_uu(data))
[81] Fix | Delete
data = in_file.read(45)
[82] Fix | Delete
out_file.write(' \nend\n')
[83] Fix | Delete
finally:
[84] Fix | Delete
for f in opened_files:
[85] Fix | Delete
f.close()
[86] Fix | Delete
[87] Fix | Delete
[88] Fix | Delete
def decode(in_file, out_file=None, mode=None, quiet=0):
[89] Fix | Delete
"""Decode uuencoded file"""
[90] Fix | Delete
#
[91] Fix | Delete
# Open the input file, if needed.
[92] Fix | Delete
#
[93] Fix | Delete
opened_files = []
[94] Fix | Delete
if in_file == '-':
[95] Fix | Delete
in_file = sys.stdin
[96] Fix | Delete
elif isinstance(in_file, basestring):
[97] Fix | Delete
in_file = open(in_file)
[98] Fix | Delete
opened_files.append(in_file)
[99] Fix | Delete
try:
[100] Fix | Delete
#
[101] Fix | Delete
# Read until a begin is encountered or we've exhausted the file
[102] Fix | Delete
#
[103] Fix | Delete
while True:
[104] Fix | Delete
hdr = in_file.readline()
[105] Fix | Delete
if not hdr:
[106] Fix | Delete
raise Error('No valid begin line found in input file')
[107] Fix | Delete
if not hdr.startswith('begin'):
[108] Fix | Delete
continue
[109] Fix | Delete
hdrfields = hdr.split(' ', 2)
[110] Fix | Delete
if len(hdrfields) == 3 and hdrfields[0] == 'begin':
[111] Fix | Delete
try:
[112] Fix | Delete
int(hdrfields[1], 8)
[113] Fix | Delete
break
[114] Fix | Delete
except ValueError:
[115] Fix | Delete
pass
[116] Fix | Delete
if out_file is None:
[117] Fix | Delete
out_file = hdrfields[2].rstrip()
[118] Fix | Delete
if os.path.exists(out_file):
[119] Fix | Delete
raise Error('Cannot overwrite existing file: %s' % out_file)
[120] Fix | Delete
if mode is None:
[121] Fix | Delete
mode = int(hdrfields[1], 8)
[122] Fix | Delete
#
[123] Fix | Delete
# Open the output file
[124] Fix | Delete
#
[125] Fix | Delete
if out_file == '-':
[126] Fix | Delete
out_file = sys.stdout
[127] Fix | Delete
elif isinstance(out_file, basestring):
[128] Fix | Delete
fp = open(out_file, 'wb')
[129] Fix | Delete
try:
[130] Fix | Delete
os.path.chmod(out_file, mode)
[131] Fix | Delete
except AttributeError:
[132] Fix | Delete
pass
[133] Fix | Delete
out_file = fp
[134] Fix | Delete
opened_files.append(out_file)
[135] Fix | Delete
#
[136] Fix | Delete
# Main decoding loop
[137] Fix | Delete
#
[138] Fix | Delete
s = in_file.readline()
[139] Fix | Delete
while s and s.strip() != 'end':
[140] Fix | Delete
try:
[141] Fix | Delete
data = binascii.a2b_uu(s)
[142] Fix | Delete
except binascii.Error, v:
[143] Fix | Delete
# Workaround for broken uuencoders by /Fredrik Lundh
[144] Fix | Delete
nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3
[145] Fix | Delete
data = binascii.a2b_uu(s[:nbytes])
[146] Fix | Delete
if not quiet:
[147] Fix | Delete
sys.stderr.write("Warning: %s\n" % v)
[148] Fix | Delete
out_file.write(data)
[149] Fix | Delete
s = in_file.readline()
[150] Fix | Delete
if not s:
[151] Fix | Delete
raise Error('Truncated input file')
[152] Fix | Delete
finally:
[153] Fix | Delete
for f in opened_files:
[154] Fix | Delete
f.close()
[155] Fix | Delete
[156] Fix | Delete
def test():
[157] Fix | Delete
"""uuencode/uudecode main program"""
[158] Fix | Delete
[159] Fix | Delete
import optparse
[160] Fix | Delete
parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
[161] Fix | Delete
parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
[162] Fix | Delete
parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
[163] Fix | Delete
[164] Fix | Delete
(options, args) = parser.parse_args()
[165] Fix | Delete
if len(args) > 2:
[166] Fix | Delete
parser.error('incorrect number of arguments')
[167] Fix | Delete
sys.exit(1)
[168] Fix | Delete
[169] Fix | Delete
input = sys.stdin
[170] Fix | Delete
output = sys.stdout
[171] Fix | Delete
if len(args) > 0:
[172] Fix | Delete
input = args[0]
[173] Fix | Delete
if len(args) > 1:
[174] Fix | Delete
output = args[1]
[175] Fix | Delete
[176] Fix | Delete
if options.decode:
[177] Fix | Delete
if options.text:
[178] Fix | Delete
if isinstance(output, basestring):
[179] Fix | Delete
output = open(output, 'w')
[180] Fix | Delete
else:
[181] Fix | Delete
print sys.argv[0], ': cannot do -t to stdout'
[182] Fix | Delete
sys.exit(1)
[183] Fix | Delete
decode(input, output)
[184] Fix | Delete
else:
[185] Fix | Delete
if options.text:
[186] Fix | Delete
if isinstance(input, basestring):
[187] Fix | Delete
input = open(input, 'r')
[188] Fix | Delete
else:
[189] Fix | Delete
print sys.argv[0], ': cannot do -t from stdin'
[190] Fix | Delete
sys.exit(1)
[191] Fix | Delete
encode(input, output)
[192] Fix | Delete
[193] Fix | Delete
if __name__ == '__main__':
[194] Fix | Delete
test()
[195] Fix | Delete
[196] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function