/usr/share/pyshared/pyepl/transarchive.py is in python-pyepl 1.1.0+git12-g365f8e3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | # PyEPL: ./transarchive.py
#
# Copyright (C) 2003-2005 Michael J. Kahana
# Authors: Ian Schleifer, Per Sederberg, Aaron Geller, Josh Jacobs
# URL: http://memory.psych.upenn.edu/programming/pyepl
#
# Distributed under the terms of the GNU Lesser General Public License
# (LGPL). See the license.txt that came with this file.
# Revision 1.5 2005/11/08 23:04:01 aaron
# log test
#
"""
Module for accessing heirarchical disk data.
"""
from pyepl.exceptions import BadFileExtension
import os
import shutil
import StringIO
from tempfile import TemporaryFile
import tarfile
import weakref
class AbstractArchive:
"""
Abstract interface to heirarchical data storage (file systems).
"""
def copyTo(self, arc):
"""
Replicate contents within arc.
"""
for name in self.listDir():
if self.isFile(name):
src = self.open(name)
src.seek(0)
trg = arc.createFile(name)
trg.seek(0)
trg.write(src.read())
else:
self.open(name).copyTo(arc.createDirectory(name))
def listDir(self):
"""
Return a list of filenames in this Archive.
"""
pass #...
def exists(self, name):
"""
Return true if file exists. Otherwise return False.
"""
pass #...
def isFile(self, name):
"""
Return true if name specifies a file (not a directory).
Otherwise return False.
"""
pass #...
def delete(self, name):
"""
Delete named file or directory.
"""
pass #...
def open(self, name):
"""
If the name specifies a file, return a file or file-like
object (rw for non-compressed, r for compressed). If it's a
directory, return a new Archive object.
"""
pass #...
def createDirectory(self, name):
"""
Create an empty directory.
"""
pass #...
def createFile(self, name):
"""
Create an empty file.
"""
pass #...
class VirtualArchive(AbstractArchive):
"""
Class to represent a virtual directory.
"""
def __init__(self):
"""
Create empty virtual directory.
"""
self.dir = {}
def listDir(self):
"""
Return a list of filenames in this Archive.
"""
return self.dir.keys()
def exists(self, name):
"""
Return true if file exists. Otherwise return False.
"""
return name in self.dir.keys()
def isFile(self, name):
"""
Return true if name specifies a file (not a directory).
Otherwise return False.
"""
return isinstance(self.dir[name], StringIO.StringIO)
def delete(self, name):
"""
Delete named file or directory.
"""
del self.dir[name]
def open(self, name):
"""
If the name specifies a file, return a file or file-like
object (rw for non-compressed, r for compressed). If it's a
directory, return a new Archive object.
"""
return self.dir[name]
def createDirectory(self, name):
"""
Create an empty directory.
"""
x = SoftArchive()
self.dir[name] = x
return x
def createFile(self, name):
"""
create an emptry file.
"""
x = StringIO.StringIO()
self.dir[name] = x
return x
class Archive(AbstractArchive):
"""
Class to represent a physical directory.
"""
def __init__(self, path):
"""
Construct archive object from path.
"""
path = os.path.abspath(path)
if not os.path.exists(path):
os.mkdir(path)
self.path = path
def listDir(self):
"""
Return a list of filenames in this Archive.
"""
return os.listdir(self.path)
def exists(self, name):
"""
Return true if file exists. Otherwise return False.
"""
return os.path.exists(self.fullPath(name))
def isFile(self, name):
"""
Return true if name specifies a file (not a directory).
Otherwise return False.
"""
return not os.path.isdir(self.fullPath(name))
def delete(self, name):
"""
Delete named file or directory.
"""
fp = self.fullPath(name)
if os.path.isdir(fp):
shutil.rmtree(fp)
else:
os.remove(fp)
def open(self, name):
"""
If the name specifies a file, return a file or file-like
object (rw for non-compressed, r for compressed). If it's a
directory, return a new Archive object.
"""
p = self.fullPath(name)
if os.path.isdir(p):
return Archive(p)
return open(p, "r+b")
def fullPath(self, name = ""):
"""
Return the absolute path of this archive.
"""
return os.path.join(self.path, name)
def createDirectory(self, name):
"""
Create an empty directory.
"""
if not self.exists(name):
os.mkdir(self.fullPath(name))
return self.open(name)
def createFile(self, name):
"""
create an emptry file.
"""
if not self.exists(name):
open(self.fullPath(name), "w")
return open(self.fullPath(name), "r+b")
class TarredFile:
"""
"""
def __init__(self, tarpath, arc):
"""
"""
tf = TemporaryFile()
self.tarpath = tarpath
self.arc = arc
self.stored_away = False
def __getattr__(self, name):
"""
"""
return getattr(self.tf, name)
def store_away(self):
"""
"""
if not self.stored_away:
self.stored_away = True
self.tf.seek(0, 2)
ti = tarfile.TarInfo(self.tarpath)
ti.size = self.tf.tell()
self.tf.seek(0)
self.arc.addfile(ti, self.tf)
def __del__(self):
"""
"""
self.store_away()
del tf
def close(self):
"""
"""
self.store_away()
tf.close()
class TarArchive(AbstractArchive):
"""
"""
def __init__(self, filename, arc = None):
"""
"""
if arc:
self.arc = arc
self.tarpath = filename
return
if os.path.exists(filename):
self.arc = tarfile.open(filename, "r")
else:
if filename.endswith(".tar"):
self.arc = tarfile.open(filename, "w")
elif filename.endswith(".tgz") or filename.endswith(".tar.gz"):
self.arc = tarfile.open(filename, "w:gz")
elif filename.endswith(".tar.bz2"):
self.arc = tarfile.open(filename, "w:bz2")
else:
raise BadFileExtension, "Tarfile with extension: %s" % os.path.splitext(filename)[1][1:].upper()
self.tarpath = ""
self.openned = weakref.WeakValueDictionary()
def listDir(self):
"""
Return a list of filenames in this Archive.
"""
r = []
tp = len(self.tarpath)
for name in self.arc.getnames():
if name.startswith(self.tarpath):
r.append(name[tp:name.find("/", tp)])
return r
def exists(self, name):
"""
Return true if file exists. Otherwise return False.
"""
try:
self.arc.getmember(self.tarpath + name)
return True
except KeyError:
return False
def isFile(self, name):
"""
Return true if name specifies a file (not a directory).
Otherwise return False.
"""
return self.arc.getmember(self.tarpath + name).isfile()
def delete(self, name):
"""
Delete named file or directory.
"""
raise ValueError, "TarArchive does not support deletion."
def open(self, name):
"""
If the name specifies a file, return a file or file-like
object (rw for non-compressed, r for compressed). If it's a
directory, return a new Archive object.
"""
try:
return self.openned[name]
except KeyError:
pass
fn = self.tarpath + name
ti = self.arc.getmember(fn)
if ti.type == tarfile.DIRTYPE:
return TarArchive(fn + "/", self.arc)
r = self.arc.extractfile(ti)
self.openned[name] = r
return r
def createDirectory(self, name):
"""
Create an empty directory.
"""
fn = self.tarpath + name
ti = tarfile.TarInfo(fn)
ti.type = tarfile.DIRTYPE
self.arc.addfile(ti)
r = TarArchive(fn + "/", self.arc)
self.openned[name] = r
return r
def createFile(self, name):
"""
Create an empty file.
"""
r = TarredFile(self.tarpath + name, self.arc)
self.openned[name] = r
return r
def openArchive(filename):
"""
"""
if filename.endswith(".tar") or filename.endswith(".tgz") or filename.endswith(".tar.gz") or filename.endswith(".tar.bz2"):
return TarArchive(filename)
else:
return Archive(filename)
|