| 1 |
import os |
| 2 |
import stat, errno |
| 3 |
|
| 4 |
UID = 1000 |
| 5 |
GID = 1000 |
| 6 |
ATIME = MTIME = CTIME = 0 |
| 7 |
INO_COUNTER = 0 |
| 8 |
|
| 9 |
|
| 10 |
class FSObject(object): |
| 11 |
read_only = True |
| 12 |
|
| 13 |
def stat(self): |
| 14 |
try: |
| 15 |
st_ino = self._st_ino |
| 16 |
except AttributeError: |
| 17 |
global INO_COUNTER |
| 18 |
INO_COUNTER += 1 |
| 19 |
st_ino = self._st_ino = INO_COUNTER |
| 20 |
st_dev = 1 |
| 21 |
st_nlink = 1 |
| 22 |
st_size = self.getsize() |
| 23 |
st_mode = self.kind |
| 24 |
st_mode |= stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
| 25 |
if stat.S_ISDIR(self.kind): |
| 26 |
st_mode |= stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
| 27 |
if self.read_only: |
| 28 |
st_uid = 0 |
| 29 |
st_gid = 0 |
| 30 |
else: |
| 31 |
st_uid = UID |
| 32 |
st_gid = GID |
| 33 |
st_atime = ATIME |
| 34 |
st_mtime = MTIME |
| 35 |
st_ctime = CTIME |
| 36 |
return os.stat_result( |
| 37 |
(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, |
| 38 |
st_size, st_atime, st_mtime, st_ctime)) |
| 39 |
|
| 40 |
def access(self, mode): |
| 41 |
s = self.stat() |
| 42 |
e_mode = s.st_mode & stat.S_IRWXO |
| 43 |
if UID == s.st_uid: |
| 44 |
e_mode |= (s.st_mode & stat.S_IRWXU) >> 6 |
| 45 |
if GID == s.st_gid: |
| 46 |
e_mode |= (s.st_mode & stat.S_IRWXG) >> 3 |
| 47 |
return (e_mode & mode) == mode |
| 48 |
|
| 49 |
def keys(self): |
| 50 |
raise OSError(errno.ENOTDIR, self) |
| 51 |
|
| 52 |
def open(self): |
| 53 |
raise OSError(errno.EACCES, self) |
| 54 |
|
| 55 |
def getsize(self): |
| 56 |
return 0 |
| 57 |
|
| 58 |
|
| 59 |
class Dir(FSObject): |
| 60 |
kind = stat.S_IFDIR |
| 61 |
def __init__(self, entries={}): |
| 62 |
self.entries = entries |
| 63 |
def keys(self): |
| 64 |
return self.entries.keys() |
| 65 |
def join(self, name): |
| 66 |
try: |
| 67 |
return self.entries[name] |
| 68 |
except KeyError: |
| 69 |
raise OSError(errno.ENOENT, name) |
| 70 |
|
| 71 |
class RealDir(Dir): |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
def __init__(self, path, show_dotfiles=False, follow_links=False, |
| 80 |
exclude=[]): |
| 81 |
self.path = path |
| 82 |
self.show_dotfiles = show_dotfiles |
| 83 |
self.follow_links = follow_links |
| 84 |
self.exclude = [excl.lower() for excl in exclude] |
| 85 |
def __repr__(self): |
| 86 |
return '<RealDir %s>' % (self.path,) |
| 87 |
def keys(self): |
| 88 |
names = os.listdir(self.path) |
| 89 |
if not self.show_dotfiles: |
| 90 |
names = [name for name in names if not name.startswith('.')] |
| 91 |
for excl in self.exclude: |
| 92 |
names = [name for name in names if not name.lower().endswith(excl)] |
| 93 |
return names |
| 94 |
def join(self, name): |
| 95 |
if name.startswith('.') and not self.show_dotfiles: |
| 96 |
raise OSError(errno.ENOENT, name) |
| 97 |
for excl in self.exclude: |
| 98 |
if name.lower().endswith(excl): |
| 99 |
raise OSError(errno.ENOENT, name) |
| 100 |
path = os.path.join(self.path, name) |
| 101 |
if self.follow_links: |
| 102 |
st = os.stat(path) |
| 103 |
else: |
| 104 |
st = os.lstat(path) |
| 105 |
if stat.S_ISDIR(st.st_mode): |
| 106 |
return RealDir(path, show_dotfiles = self.show_dotfiles, |
| 107 |
follow_links = self.follow_links, |
| 108 |
exclude = self.exclude) |
| 109 |
elif stat.S_ISREG(st.st_mode): |
| 110 |
return RealFile(path) |
| 111 |
else: |
| 112 |
|
| 113 |
raise OSError(errno.EACCES, path) |
| 114 |
|
| 115 |
class File(FSObject): |
| 116 |
kind = stat.S_IFREG |
| 117 |
def __init__(self, data=''): |
| 118 |
self.data = data |
| 119 |
def getsize(self): |
| 120 |
return len(self.data) |
| 121 |
def open(self): |
| 122 |
import cStringIO |
| 123 |
return cStringIO.StringIO(self.data) |
| 124 |
|
| 125 |
class RealFile(File): |
| 126 |
def __init__(self, path, mode=0): |
| 127 |
self.path = path |
| 128 |
self.kind |= mode |
| 129 |
def __repr__(self): |
| 130 |
return '<RealFile %s>' % (self.path,) |
| 131 |
def getsize(self): |
| 132 |
return os.stat(self.path).st_size |
| 133 |
def open(self): |
| 134 |
try: |
| 135 |
return open(self.path, "rb") |
| 136 |
except IOError as e: |
| 137 |
raise OSError(e.errno, "open failed") |