📄 archive.py
字号:
for directory in directory_list: try: path_list = os.listdir(directory) except: continue for path in path_list: path = os.path.join(directory, path) if os.path.isfile(path) or os.path.islink(path): file_list.append(path) # decompose origional paths so the archive system # can make directory records for them that will set the # correct access/modification dates and permissions for directory in origional_directory_list: while 1: base, junk = os.path.split(directory) if base == "" or \ base == os.curdir or \ base == os.pardir or \ base == os.sep: break directory_list.append(base) directory = base return file_list, directory_list, delete_listdef get_directories(directory): directory_list = [directory] try: path_list = os.listdir(directory) except: return directory_list for path in path_list: path = os.path.join(directory, path) if os.path.isdir(path) and not os.path.islink(path): directory_list = directory_list + get_directories(path) return directory_listdef file_type(path): if os.path.islink(path): return "L" basename = os.path.basename(path) try: return _file_table[basename].file_t except KeyError: pass ## try to determine file type by extention (base, ext) = os.path.splitext(basename) ext = ext[1:] if len(ext): try: return _ext_table[ext].file_t except KeyError: pass ## the extention/file is not in the table ## so use algrithim to try to figure it out return check_file_type(path)def lookup_creator_type(path, file_t): basename = os.path.basename(path) try: return (_file_table[basename].creator, _file_table[basename].type) except KeyError: pass ## try to determine file type by extention (base, ext) = os.path.splitext(basename) ext = ext[1:] if len(ext): try: return (_ext_table[ext].creator, _ext_table[ext].type) except KeyError: pass ## the extention/file is not in the table so use defaults if file_t == "A": return _ascii_default else: return _binary_default# "A" = ascii# "B" = binary# "L" = linkdef check_file_type(path): ## Check what CVS thinks.. entries=os.path.join(os.path.dirname(path),"CVS","Entries") if os.path.exists(entries): base=os.path.basename(path) for l in open(entries,"r").readlines(): fields=string.split(l,"/") if len(fields) < 5: continue if fields[1] == base: if fields[4] == "-kb": return "B" if fields[4] == "": return "A" break ## Heuristics, does the file contain funny characters?? non_text = 0# print path fil = open(path, "rb") buff = fil.read(512) fil.close() ten_percent = len(buff) / 10 + 1 for i in range(len(buff)): a = ord(buff[i]) if (a < 8) or (a > 13 and a < 32) or (a > 126): non_text = non_text + 1 if non_text >= ten_percent: return "B" return "A"def zread(out_fil, in_fil): zdata_len = 0 zobj = zlib.compressobj(_zlib_level) data = in_fil.read(_max_read) while data: zdata = zobj.compress(data) zdata_len = zdata_len + len(zdata) out_fil.write(zdata) data = in_fil.read(_max_read) zdata = zobj.flush() zdata_len = zdata_len + len(zdata) out_fil.write(zdata) return zdata_lendef zwrite(in_fil, zlen, out_fil): bytes_to_read = zlen zobj = zlib.decompressobj() while bytes_to_read > 0: zbuff = in_fil.read(min(_max_read, bytes_to_read)) bytes_to_read = bytes_to_read - len(zbuff) buff = zobj.decompress(zbuff) out_fil.write(buff) out_fil.write(zobj.flush())def openrf(path, mode): if have_macos: try: return MacOS.openrf(mac_path(path), "*"+mode) except MacOS.Error: pass try: return open(os.path.join(path,"..namedfork","rsrc"),mode) except IOError: pass return Nonedef read_file(arch, path): rec = Record() rec.path = path rec.file_t = file_type(rec.path) rec.data_len = 0 rec.resc_len = 0 rec.creator = " " rec.type = " " rec.mode = 0 rec.atime = 0 rec.mtime = 0 (rec.creator, rec.type) = lookup_creator_type(rec.path, rec.file_t) if rec.file_t == "L": str=os.readlink(rec.path) arch.write(str) rec.data_len=len(str) else: if rec.file_t == "B": fil = open(rec.path, "rb") else: fil = open(rec.path, "r") print "%s %s" % (rec.file_t, rec.path) ## compress-read the file, writing it to the ## archive and returning the compressed length rec.data_len = zread(arch, fil) fil.close() ## MACINTOSH SPECIFIC if have_macfs: ## get creator/type on the macintosh try: fsp = macfs.FSSpec(mac_path(rec.path)) (rec.creator, rec.type) = fsp.GetCreatorType() except MacOS.Error: pass fork_fil = openrf(rec.path, "rb"); if fork_fil: ## compress read the data fork, returning ## the compressed length rec.resc_len = zread(arch, fork_fil) fork_fil.close() ## stat the file st = os.stat(rec.path) ## get file mode, atime, and mtime rec.mode = st[stat.ST_MODE] rec.atime = st[stat.ST_ATIME] rec.mtime = st[stat.ST_MTIME] ## Silly macs! if type(rec.atime) == types.FloatType: rec.atime = int(rec.atime - _mac_second_diff) rec.mtime = int(rec.mtime - _mac_second_diff) return recdef recursive_rm(path): if os.path.isdir(path) and not os.path.islink(path): for p in os.listdir(path): recursive_rm(os.path.join(path,p)) os.unlink(path)def delete_file(rec): path = rec.path if os.path.exists(path): recursive_rm(path)## truncate filename to 31 chars on the Macintoshdef mac_name_mangler(fname): if len(fname) <= 31: return fname base, ext = os.path.splitext(fname) base=string.replace(base,"-","") base=string.replace(base,"_","") fname=base+ext if len(fname) <= 31: return fname base=string.replace(base,"e","") base=string.replace(base,"i","") base=string.replace(base,"a","") base=string.replace(base,"o","") base=string.replace(base,"u","") fname=base+ext if len(fname) <= 31: return fname ## Crop return base[:31 - len(ext)] + extdef write_file(arch, rec): ## make sure the directory specified exists, ## if it doesn't then create it properly #print "x %s" % rec.path make_dirs = [] filename=rec.path (path, basename) = os.path.split(filename) ## Mac paths if os.sep == ":": filename=os.path.join(path, mac_name_mangler(basename)) ## create directory path while path != "" and not os.path.exists(path): make_dirs.insert(0, path) (path, basename) = os.path.split(path) for path in make_dirs: os.mkdir(path) ## check if file exists if os.path.exists(filename): if not os.path.isfile(filename) and not os.path.islink(filename): return 0 else: try: os.unlink(filename) except os.error: print "ERROR: could not remove %s" % (filename) ## write the file -- attempt to find another filename if ## it cannot be opened try: if rec.file_t == "L": to=arch.read(rec.data_len) try: os.unlink(filename) except OSError: pass os.symlink(to, filename) return 1 if rec.file_t == "B": fil = open(filename, "wb") else: fil = open(filename, "w") except IOError: print "ERROR: could not write to %s" % (filename) fil = FSink() zwrite(arch, rec.data_len, fil) return 1 ## read compressed file zwrite(arch, rec.data_len, fil) fil.close() ## set file mode, atime, mtime if have_os_chmod: os.chmod(filename, rec.mode) ## resource fork if rec.resc_len: fil = openrf(filename, "wb") if not fil: fil = FSink() zwrite(arch, rec.resc_len, fil) fil.close() ## MACINTOSH SPECIFIC if have_macfs: ## get creator/type on the macintosh try: fsp = macfs.FSSpec(mac_path(filename)) fsp.SetCreatorType(rec.creator, rec.type) mtime = float(rec.mtime + _mac_second_diff) fsp.SetDates(mtime, mtime, mtime) except MacOS.Error: pass if have_os_utime: try: os.utime(filename, (rec.atime, rec.mtime)) except OSError: pass return 1def read_directory_info(path): rec = Record() rec.path = path rec.file_t = "D" rec.creator = " " rec.type = " " rec.data_len = 0 rec.resc_len = 0 ## stat the directory st = os.stat(path) ## get directory mode, atime, and mtime rec.mode = st[stat.ST_MODE] rec.atime = st[stat.ST_ATIME] rec.mtime = st[stat.ST_MTIME] ## Silly macs! if type(rec.atime) == types.FloatType: rec.atime = int(rec.atime - _mac_second_diff) rec.mtime = int(rec.mtime - _mac_second_diff) return recdef delete_record(path): rec = Record() rec.path = path rec.file_t = "R" rec.creator = " " rec.type = " " rec.data_len = 0 rec.resc_len = 0 rec.mode = 0 rec.atime = 0 rec.mtime = 0 return recdef write_directory_info(rec): ## create directory path make_dirs = [] path = rec.path while path != "" and not os.path.exists(path): make_dirs.insert(0, path) (path, basename) = os.path.split(path) for path in make_dirs: try: os.mkdir(path) except: print "ERROR: cannot make directory %s" % (directory) return 0 ## set mtime on Macintosh Folders if have_macfs: mtime = float(rec.mtime + _mac_second_diff) try: fsp = macfs.FSSpec(mac_path(rec.path)) fsp.SetDates(mtime, mtime, mtime) except MacOS.Error: pass ## set directory mode/mtime/atime on posix/UNIX if have_os_utime: try: os.utime(rec.path, (rec.atime, rec.mtime)) except OSError: pass try: os.chmod(rec.path, rec.mode) except OSError: pass return 1## FOR INTERACTIVE USAGEDEFAULT_ARCHIVE_NAME = "archive.rna"def usage(pname): pname = os.path.basename(pname) print "%s: invalid usage" % (pname) print "python %s -c archfile path1 path2 ... create archive" % (pname) print "python %s -i archfile print index" % (pname) print "python %s -e archfile extract archive" % (pname) sys.exit(1)def mac_main(): if len(sys.argv) < 2: usage(sys.argv[0]) file_list = sys.argv[1:] # if only one file with .rna extentio, then extract if len(file_list) == 1: (path, basename) = os.path.split(file_list[0]) # try to determine file type by extention i = string.rfind(basename, ".") if i != -1: if basename[i+1:] == "rna": os.chdir(path) Extract(basename) sys.exit(0) arch_file_list = [] # else, create a archive with the default name for file in file_list: (path, basename) = os.path.split(file) arch_file_list.append(basename) os.chdir(path) Archive(DEFAULT_ARCHIVE_NAME, arch_file_list)def command_line_main(): program_name = sys.argv[0] try: (optlist, arglist) = getopt.getopt(sys.argv[1:], "c:e:i:") except: usage(program_name) if len(optlist) != 1: usage(program_name) ## create archive if optlist[0][0] == "-c": if len(arglist) < 1: usage(program_name) Archive(optlist[0][1], arglist) ## print index of archive elif optlist[0][0] == "-i": Index(optlist[0][1]) ## extract archive elif optlist[0][0] == "-e": ## if there's an argument in the arglist, then use that as ## the filter dir if not len(arglist): Extract(optlist[0][1]) else: ExtractDir(optlist[0][1], arglist[0])def main(): if not have_os_utime: mac_main() else: if sys.platform == 'darwin': import string if string.find(os.environ.get("SYSTEM_ID",""),"-cw") != -1: import macemu command_line_main()if __name__ == "__main__": try: main() except KeyboardInterrupt: print "**BREAK**" sys.exit(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -