Changeset 388 for python/vendor/current/Lib/uu.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/uu.py
r2 r388 45 45 # If in_file is a pathname open it and change defaults 46 46 # 47 if in_file == '-': 48 in_file = sys.stdin 49 elif isinstance(in_file, basestring): 47 opened_files = [] 48 try: 49 if in_file == '-': 50 in_file = sys.stdin 51 elif isinstance(in_file, basestring): 52 if name is None: 53 name = os.path.basename(in_file) 54 if mode is None: 55 try: 56 mode = os.stat(in_file).st_mode 57 except AttributeError: 58 pass 59 in_file = open(in_file, 'rb') 60 opened_files.append(in_file) 61 # 62 # Open out_file if it is a pathname 63 # 64 if out_file == '-': 65 out_file = sys.stdout 66 elif isinstance(out_file, basestring): 67 out_file = open(out_file, 'wb') 68 opened_files.append(out_file) 69 # 70 # Set defaults for name and mode 71 # 50 72 if name is None: 51 name = os.path.basename(in_file)73 name = '-' 52 74 if mode is None: 53 try: 54 mode = os.stat(in_file).st_mode 55 except AttributeError: 56 pass 57 in_file = open(in_file, 'rb') 58 # 59 # Open out_file if it is a pathname 60 # 61 if out_file == '-': 62 out_file = sys.stdout 63 elif isinstance(out_file, basestring): 64 out_file = open(out_file, 'w') 65 # 66 # Set defaults for name and mode 67 # 68 if name is None: 69 name = '-' 70 if mode is None: 71 mode = 0666 72 # 73 # Write the data 74 # 75 out_file.write('begin %o %s\n' % ((mode&0777),name)) 76 data = in_file.read(45) 77 while len(data) > 0: 78 out_file.write(binascii.b2a_uu(data)) 75 mode = 0666 76 # 77 # Write the data 78 # 79 out_file.write('begin %o %s\n' % ((mode&0777),name)) 79 80 data = in_file.read(45) 80 out_file.write(' \nend\n') 81 while len(data) > 0: 82 out_file.write(binascii.b2a_uu(data)) 83 data = in_file.read(45) 84 out_file.write(' \nend\n') 85 finally: 86 for f in opened_files: 87 f.close() 81 88 82 89 … … 86 93 # Open the input file, if needed. 87 94 # 95 opened_files = [] 88 96 if in_file == '-': 89 97 in_file = sys.stdin 90 98 elif isinstance(in_file, basestring): 91 99 in_file = open(in_file) 92 # 93 # Read until a begin is encountered or we've exhausted the file 94 # 95 while True: 96 hdr = in_file.readline() 97 if not hdr: 98 raise Error('No valid begin line found in input file') 99 if not hdr.startswith('begin'): 100 continue 101 hdrfields = hdr.split(' ', 2) 102 if len(hdrfields) == 3 and hdrfields[0] == 'begin': 100 opened_files.append(in_file) 101 try: 102 # 103 # Read until a begin is encountered or we've exhausted the file 104 # 105 while True: 106 hdr = in_file.readline() 107 if not hdr: 108 raise Error('No valid begin line found in input file') 109 if not hdr.startswith('begin'): 110 continue 111 hdrfields = hdr.split(' ', 2) 112 if len(hdrfields) == 3 and hdrfields[0] == 'begin': 113 try: 114 int(hdrfields[1], 8) 115 break 116 except ValueError: 117 pass 118 if out_file is None: 119 out_file = hdrfields[2].rstrip() 120 if os.path.exists(out_file): 121 raise Error('Cannot overwrite existing file: %s' % out_file) 122 if mode is None: 123 mode = int(hdrfields[1], 8) 124 # 125 # Open the output file 126 # 127 if out_file == '-': 128 out_file = sys.stdout 129 elif isinstance(out_file, basestring): 130 fp = open(out_file, 'wb') 103 131 try: 104 int(hdrfields[1], 8) 105 break 106 except ValueError: 132 os.path.chmod(out_file, mode) 133 except AttributeError: 107 134 pass 108 if out_file is None: 109 out_file = hdrfields[2].rstrip() 110 if os.path.exists(out_file): 111 raise Error('Cannot overwrite existing file: %s' % out_file) 112 if mode is None: 113 mode = int(hdrfields[1], 8) 114 # 115 # Open the output file 116 # 117 opened = False 118 if out_file == '-': 119 out_file = sys.stdout 120 elif isinstance(out_file, basestring): 121 fp = open(out_file, 'wb') 122 try: 123 os.path.chmod(out_file, mode) 124 except AttributeError: 125 pass 126 out_file = fp 127 opened = True 128 # 129 # Main decoding loop 130 # 131 s = in_file.readline() 132 while s and s.strip() != 'end': 133 try: 134 data = binascii.a2b_uu(s) 135 except binascii.Error, v: 136 # Workaround for broken uuencoders by /Fredrik Lundh 137 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3 138 data = binascii.a2b_uu(s[:nbytes]) 139 if not quiet: 140 sys.stderr.write("Warning: %s\n" % v) 141 out_file.write(data) 135 out_file = fp 136 opened_files.append(out_file) 137 # 138 # Main decoding loop 139 # 142 140 s = in_file.readline() 143 if not s: 144 raise Error('Truncated input file') 145 if opened: 146 out_file.close() 141 while s and s.strip() != 'end': 142 try: 143 data = binascii.a2b_uu(s) 144 except binascii.Error, v: 145 # Workaround for broken uuencoders by /Fredrik Lundh 146 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3 147 data = binascii.a2b_uu(s[:nbytes]) 148 if not quiet: 149 sys.stderr.write("Warning: %s\n" % v) 150 out_file.write(data) 151 s = in_file.readline() 152 if not s: 153 raise Error('Truncated input file') 154 finally: 155 for f in opened_files: 156 f.close() 147 157 148 158 def test():
Note:
See TracChangeset
for help on using the changeset viewer.