tarfs.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » vfs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » vfs » tarfs.py
# Time-stamp: <03/08/11 13:12:06 smulloni>

######################################################################## 
#  Copyright (C) 2002 Andrew Csillag <drew_csillag@yahoo.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
########################################################################

import tarlib
from vfs import FS,VFSException
from rosio import RO_StringIO
import pathutil
import os


def bslog(msg):
#    try:
#        open('/tmp/bullshit','a').write('%s\n' % msg)
#    except:
        pass
    
class TarFS(FS):
    def __init__(self, path, root='', prefix='/'):
        self.path=os.path.abspath(path)
        self._contents={} #filled by _readTar
        self._filelist=[] #filled by _readTar
        self._readTar()
        self.__archive=pathutil.Archive(root, prefix)
  self.root=root
        self.prefix=prefix
        self.__archive.savePaths(self._filelist)
        bslog(str(self._filelist))
        
    def ministat(self, path):
        bslog('ministat %s' % path)
        adjusted=pathutil._adjust_user_path(path)
        if not self.__archive.paths.has_key(adjusted): 
            raise VFSException, "no such file or directory: %s" % path
        realname=self.__archive.paths[adjusted]
        if realname==None:
            arcstat=os.stat(self.zpath)[7:]
            return (0,) + arcstat
        item = self._contents[realname]
        return item[1], -1, item[2], item[2]
    
    def _readTar(self):
        if self.path[-2:] == 'gz':
            import gzip
            tarlib.readTar(gzip.GzipFile(self.path), self._readTarEater)
        else:
            tarlib.readTar(open(self.path), self._readTarEater)

    def _readTarEater(self, name, contents, size, mode, uid, gid, mtime, 
                      typeflag, linkname, uname, gname, devmaj, devmin):
        self._contents[name] = (contents, size, mtime)
        self._filelist.append(name)

    def open(self, path, mode='r'):
        bslog('getting %s' % path)
        adjusted=pathutil._adjust_user_path(path)
        if mode!='r':
            raise VFSException, "unsupported file open mode"
        if not self.__archive.paths.has_key(adjusted):
            raise VFSException, "no such file or directory: %s" % path
        realname=self.__archive.paths[adjusted]
        if realname!=None:
            return RO_StringIO(adjusted,
                               self._contents[realname][0])
        else:
            raise VFSException, "cannot open directory as file: %s" % path

    def listdir(self, path):
        bslog('listdir %s' % path)
        return self.__archive.listdir(path)
        
    def isdir(self, path):
        bslog('isdir %s' % path)
        adjusted=pathutil._adjust_user_path(path)
        if not self.__archive.paths.has_key(adjusted):
            #raise VFSException, "no such file or directory: %s" % path
      return 0
        realname=self.__archive.paths[adjusted]
        if realname==None:
            return 1
        else:
            return realname.endswith('/') and \
                   self._contents[realname][1]==0

    def isfile(self, path):
        bslog('isfile %s' % path)
        adjusted=pathutil._adjust_user_path(path)
        if not self.__archive.paths.has_key(adjusted):
            #raise VFSException, "no such file or directory: %s" % path
      return 0
        realname=self.__archive.paths[adjusted]
        if realname==None:
            return 0
        else:
            return not adjusted.endswith('/')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.