⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 archive.py

📁 linux下的一款播放器
💻 PY
📖 第 1 页 / 共 2 页
字号:
# # ***** BEGIN LICENSE BLOCK *****# Source last modified: $Id: archive.py,v 1.21 2004/12/02 20:36:28 hubbe Exp $# # Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.# # The contents of this file, and the files included with this file,# are subject to the current version of the RealNetworks Public# Source License (the "RPSL") available at# http://www.helixcommunity.org/content/rpsl unless you have licensed# the file under the current version of the RealNetworks Community# Source License (the "RCSL") available at# http://www.helixcommunity.org/content/rcsl, in which case the RCSL# will apply. You may also obtain the license terms directly from# RealNetworks.  You may not use this file except in compliance with# the RPSL or, if you have a valid RCSL with RealNetworks applicable# to this file, the RCSL.  Please see the applicable RPSL or RCSL for# the rights, obligations and limitations governing use of the# contents of the file.# # Alternatively, the contents of this file may be used under the# terms of the GNU General Public License Version 2 or later (the# "GPL") in which case the provisions of the GPL are applicable# instead of those above. If you wish to allow use of your version of# this file only under the terms of the GPL, and not to allow others# to use your version of this file under the terms of either the RPSL# or RCSL, indicate your decision by deleting the provisions above# and replace them with the notice and other provisions required by# the GPL. If you do not delete the provisions above, a recipient may# use your version of this file under the terms of any one of the# RPSL, the RCSL or the GPL.# # This file is part of the Helix DNA Technology. RealNetworks is the# developer of the Original Code and owns the copyrights in the# portions it created.# # This file, and the files included with this file, is distributed# and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY# KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS# ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET# ENJOYMENT OR NON-INFRINGEMENT.# # Technology Compatibility Kit Test Suite(s) Location:#    http://www.helixcommunity.org/content/tck# # Contributor(s):# # ***** END LICENSE BLOCK *****# """Multi-platform compressed (using zlib) archive format.  None of thepublicly available archive formats worked well between Mac, Windows, andUNIX, so this was written to fill that gap for the purposes of the buildsystem.  It supports data and resource forks for the Macintosh.  It has ainternal MIME table for distinguishing ASCII files from binary files, as wellas a binary-checking algroithm it falls back on if the file extention is notin the MIME table.  It stores line ending internally as UNIX "\n" endings,but writes out the correct line endings for ASCII files when decompressingon a given platform.  For Windows, "\r\n", and for Macintosh, "\r"."""import osimport sysimport stringimport getoptimport statimport typestry:    import zlibexcept:    passtry:    import macfs    import MacOS    have_macfs=1    have_macos=1except:    have_macfs=0    have_macos=0try:    os.utime    have_os_utime=1except:    have_os_utime=0try:    os.chmod    have_os_chmod=1except:    have_os_chmod=0## these constants effect .rna file compatibility!!!_rna_version = "1.1"_num_length = 32_mac_second_diff = 2082816000.0 #### these constants effect preformance/debugging_debug = 0_max_read = 8192_zlib_level = 6#### extention table for mapping extiontions to automagic## ASCII or binary file, and creator/type for the Macintoshclass FType:    def __init__(self, file_t, creator, ftype):        self.file_t = file_t        self.creator = creator        self.type = ftype_ext_table = {    "xml" : FType("A", "CWIE", "TEXT"),    "bif" : FType("A", "CWIE", "TEXT"),    "bat" : FType("A", "CWIE", "TEXT"),    "s"   : FType("A", "CWIE", "TEXT"),    "S"   : FType("A", "CWIE", "TEXT"),    "exp" : FType("A", "CWIE", "TEXT"),    "cpp" : FType("A", "CWIE", "TEXT"),    "CPP" : FType("A", "CWIE", "TEXT"),    "cp"  : FType("A", "CWIE", "TEXT"),    "r"   : FType("A", "CWIE", "TEXT"),    "c"   : FType("A", "CWIE", "TEXT"),    "C"   : FType("A", "CWIE", "TEXT"),    "h"   : FType("A", "CWIE", "TEXT"),    "H"   : FType("A", "CWIE", "TEXT"),    "res" : FType("A", "CWIE", "TEXT"),    "py"  : FType("A", "CWIE", "TEXT"),    "pcf" : FType("A", "CWIE", "TEXT"),    "cf"  : FType("A", "CWIE", "TEXT"),    "pm"  : FType("A", "CWIE", "TEXT"),    "pl"  : FType("A", "CWIE", "TEXT"),    "cfg" : FType("A", "CWIE", "TEXT"),    "spc" : FType("A", "CWIE", "TEXT"),    "txt" : FType("A", "CWIE", "TEXT"),    "cc"  : FType("A", "CWIE", "TEXT"),    "asm" : FType("A", "CWIE", "TEXT"),    "cp"  : FType("A", "CWIE", "TEXT"),    "htm" : FType("A", "CWIE", "TEXT"),    "html": FType("A", "CWIE", "TEXT"),    "ver" : FType("A", "CWIE", "TEXT"),    "ini" : FType("A", "CWIE", "TEXT"),    "lib" : FType("B", "CWIE", "MPLF"),    "o"   : FType("B", "????", "BINA"),    "obj" : FType("B", "????", "BINA"),    "OBJ" : FType("B", "????", "BINA"),    "exe" : FType("B", "????", "BINA"),    "dll" : FType("B", "????", "shlb"),    "DLL" : FType("B", "????", "shlb"),    "a"   : FType("B", "????", "BINA"),    "bmp" : FType("B", "????", "BINA"),    "bmp" : FType("B", "????", "BINA"),    "class":FType("B", "????", "BINA"),    "rna" : FType("B", "????", "BINA"),    "dat" : FType("B", "????", "BINA"),    "jpg" : FType("B", "????", "BINA"),    "JPG" : FType("B", "????", "BINA"),    "gif" : FType("B", "????", "BINA"),    "GIF" : FType("B", "????", "BINA"),    "png" : FType("B", "????", "BINA"),    "PNG" : FType("B", "????", "BINA"),    "rm"  : FType("B", "????", "BINA"),    "mp3" : FType("B", "????", "BINA"),    "zip" : FType("B", "????", "BINA"),    "jar" : FType("B", "????", "BINA"),    "hqx" : FType("B", "????", "BINA"),    "sit" : FType("B", "????", "BINA"),    "pdf" : FType("B", "????", "BINA"),    "PDF" : FType("B", "????", "BINA"),    }## file table, like above but for complete file names_file_table = {    "Umakefil"    : FType("A", "CWIE", "TEXT"),    "umakefil"    : FType("A", "CWIE", "TEXT"),    "Tag"         : FType("A", "CWIE", "TEXT"),    "Entries"     : FType("A", "CWIE", "TEXT"),    "Repository"  : FType("A", "CWIE", "TEXT"),    "Root"        : FType("A", "CWIE", "TEXT"),    }    ## default creator/types for misc binary and ASCII files_binary_default = ("????", "BINA")_ascii_default  = ("CWIE", "TEXT")## entrypointsdef Archive(archive_path, path_list):    RNA_Archive(archive_path, path_list)def Index(archive_path):    RNA_Index(archive_path)def Extract(archive_path):    RNA_Extract(archive_path)def ExtractDir(archive_path, dir):    def filter_func(x, dir = dir):        return x[:len(dir)] == dir    RNA_Extract(archive_path, filter_func)## private## fake file class which is a infinite sinkclass FSink:    def __init__(self):        self.__tell = 0        def write(self, data):        self.__tell = self.__tell + len(data)    def tell(self):        return self.__tell    def close(self):        self.__tell = 0def RNA_Archive_stream(arch, path_list):    archive_file_list, archive_directory_list, delete_list = get_target_list(path_list)    ## separate record lists of all files and directories    record_list = []    for path in archive_file_list:        rec=read_file(arch, path)        record_list.append(rec)        rec.debug()    for path in archive_directory_list:        rec=read_directory_info(path)        record_list.append(rec)        rec.debug()    for path in delete_list:        rec=delete_record(path)        record_list.append(rec)        rec.debug()    ## write file index table    index_location = arch.tell()    for rec in record_list:        rec.write(arch)    ## write the beginning of the index location at the end of the file    arch.write(string.zfill(str(index_location), _num_length))def RNA_Archive(archive_path, path_list):    ## open the archive file for writing    arch = open(archive_path, "wb")    RNA_Archive_stream(arch, path_list)    arch.close()    ## set the file type of the archive to binary on Macintosh    if have_macfs:        try:            fsp = macfs.FSSpec(mac_path(archive_path))            fsp.SetCreatorType("????", "BINA")        except MacOS.Error:            passdef RNA_Index(archive_path):    if not os.path.isfile(archive_path):        print "archive.py: file not found %s" % (archive_path)        return    ## debugging output is used to show the index    global _debug    _debug = 1    arch = open(archive_path, "rb")    ## find the location of the index in the file    index_begin, index_end = index_begin_end(arch)    ## seek to the beginning of the index and print    arch.seek(index_begin)    while arch.tell() < index_end:        rec = Record()        rec.read(arch)        rec.debug()    arch.close()def RNA_Extract(archive_path, filter_func = None, posix_filter = None):    if not os.path.isfile(archive_path):        print "archive.py: file not found %s" % (archive_path)        return    arch = open(archive_path, "rb")    archidx = open(archive_path, "rb")    ## find the location of the index in the file    index_begin, index_end = index_begin_end(archidx)    ## seek to the beginning of the index    archidx.seek(index_begin)    directory_record_list = []    while archidx.tell() < index_end:        rec = Record()        rec.read(archidx)        rec.debug()        ## filter out according to the filter function        if filter_func:            if posix_filter:                yn=filter_func(rec.posixpath)            else:                yn=filter_func(rec.path)            if not yn:                arch.seek(rec.data_len + rec.resc_len, 1)                continue        print "%s %s" % (rec.file_t, rec.path)                if rec.file_t == "R":            delete_file(rec)        elif rec.file_t == "D":            directory_record_list.append(rec)        else:            write_file(arch, rec)    ## write directory information in decending directory order    record_hash = {}    for rec in directory_record_list:        record_hash[rec.path] = rec    key_list = record_hash.keys()    key_list.sort()    key_list.reverse()    for key in key_list:        write_directory_info(record_hash[key])            class Record:    def debug(self):        if not _debug:            return        print "path = %s" % (self.path)        print "        file_t = %s   creator = %s  type = %s " % (self.file_t, self.creator, self.type)        print "        data_len = %d  resc_len = %d" % (self.data_len, self.resc_len)        print "        mode = %d  atime = %d  mtime = %d" % (self.mode, self.atime, self.mtime)        def read(self, fil):        ## first part of the record is the length of the path, the rest of        ## the record is fixed-length        try:            path_len = int(fil.read(_num_length))        except ValueError:            raise error, "archive corrupt, invalid path_len in record"        rec_data_len = path_len + 9 + (5 * _num_length)        rec_data = fil.read(rec_data_len)        ## grab the path, and then chop it off the rest of        ## the buffer        self.posixpath=rec_data[:path_len]        self.path = native_path(self.posixpath)        index = path_len        ## read file type: binary=B, ascii=A, directory=D        self.file_t = rec_data[index]        index = index + 1        ## file createor        index2 = index + 4        self.creator = rec_data[index:index2]        index = index2        ## file type        index2 = index + 4        self.type = rec_data[index:index2]        index = index2        ## read data fork length        index2 = index + _num_length        try:            self.data_len = int(rec_data[index:index2])        except ValueError:            raise error, "archive corrupt, invalid data_len in record"        index = index2        ## read resource fork length        index2 = index + _num_length        try:            self.resc_len  = int(rec_data[index:index2])        except ValueError:            raise error, "archive corrupt, invalid resc_len in record"        index = index2        ## read mode bits        index2 = index + _num_length        try:            self.mode = int(rec_data[index:index2])        except ValueError:            raise error, "archive corrupt, invalid mode in record"        index = index2        ## read atime        index2 = index + _num_length        try:            self.atime = int(rec_data[index:index2])        except ValueError:            raise error, "archive corrupt, invalid atime in record"        index = index2                ## read mtime        index2 = index + _num_length        try:            self.mtime = int(rec_data[index:index2])        except ValueError:            raise error, "archive corrupt, invalid mtime in record"    def write(self, fil):        ## store paths as relative file paths        path = self.path        while path[:1] == os.sep:            path = path[1:]        ## internal format for paths is the UNIX format        path = self.__posix_path(path)        ## file creator/type much be 4 charactors        creator = self.creator        if len(creator) != 4:            creator = "    "        ftype = self.type        if len(ftype) != 4:            ftype = "    "        rec_data = string.zfill(len(path), _num_length) +\                   path + self.file_t + creator + ftype +\                   string.zfill(self.data_len, _num_length) +\                   string.zfill(self.resc_len, _num_length) +\                   string.zfill(self.mode, _num_length) +\                   string.zfill(self.atime, _num_length) +\                   string.zfill(self.mtime, _num_length)                fil.write(rec_data)    def __posix_path(self, path):        return string.translate(path, string.maketrans(os.sep, "/"))def native_path(path, sep=os.sep):    if not path or path == "":        return ""    path = string.translate(path, string.maketrans("/", sep))    ## Mac paths    if sep == ':':        mac_path = ""        last_backdir = 0        last_curdir = 0        ## no directory info defaults to current dir        if not path[0] in ".:":            mac_path = ":"         i = 0        while i < len(path):            ## translate current directory            if path[i:i+2] == ".:":                if not last_curdir and not last_backdir:                    mac_path = mac_path + ":"                last_curdir = 1                i = i + 2                continue            ## translate stepping back a directory            if path[i:i+3] == "..:":                if last_backdir or last_curdir:                    mac_path = mac_path + ":"                else:                    mac_path = mac_path + "::"                    last_backdir = 1                i = i + 3                continue            ## append to mac_path            mac_path = mac_path + path[i]            i = i + 1            last_curdir = 0            last_backdir = 0        path = mac_path    return pathdef mac_path(path):    ## Convert native path to mac path    if os.sep == ":":        return path    #print "mac_path(%s) => %s" % (repr(path), native_path(path, ":"))    return native_path(path, ":")def index_begin_end(fil):    fil.seek(-_num_length, 2)    try:        begin = int(fil.read(_num_length))    except ValueError:        raise error, "archive corrupt, cannot read index location"    end = fil.tell() - _num_length        return begin, enddef get_target_list(path_list):    origional_directory_list = []    directory_list = []    file_list = []    delete_list = []    # get all directories in the path list    for path in path_list:        if os.path.isfile(path) or os.path.islink(path):                file_list.append(path)        elif os.path.isdir(path):            origional_directory_list.append(path)            directory_list.extend(get_directories(path))            continue        elif not os.path.exists(path):            delete_list.append(path)                # get all files in given paths 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -