/usr/share/pyshared/jsb/utils/fileutils.py is in jsonbot 0.84.4-1.
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 | # jsb/utils/fileutils.py
#
# Description: Various file utilities
# Author: Wijnand 'tehmaze' Modderman
# Author URL: http://tehmaze.com
# License: BSD
""" provide file related helpers. """
## jsb imports
from jsb.utils.generic import istr
## basic imports
import tarfile
import os
import types
import cStringIO
import bz2
import gzip
## tarextract function
def tarextract(package, fileobj=None, prefix=None, base=None):
'''
Extracts a tarball from ``package``, or, if ``fileobj`` is either a string or a seekable
IO stream, it will extract the data from there. We only extract files from the tarball
that are member of the ``base`` directory if a ``base`` is specified.
'''
extracted = []
if fileobj:
if type(fileobj) == types.StringType: fileobj = cStringIO.StringIO(fileobj)
tarf = tarfile.open(mode='r|', fileobj=fileobj)
else: tarf = tarfile.open(package, 'r')
for tarinfo in tarf:
if tarinfo.name.startswith('/'): tarinfo.name = tarinfo.name[1:] # strip leading /
if not base or ((tarinfo.name.rstrip('/') == base and tarinfo.isdir()) or tarinfo.name.startswith(base+os.sep)):
if prefix: tarinfo.name = '/'.join([prefix, tarinfo.name])
tarf.extract(tarinfo)
extracted.append(tarinfo.name)
tarf.close()
if fileobj:
try: fileobj.close()
except: pass
del fileobj
return extracted
## unzip functions
def bunzip2(fileobj):
""" bunzip2 the file object. """
return bz2.decompress(fileobj)
def gunzip(fileobj):
""" gunzip the file object. """
if type(fileobj) == types.StringType or isinstance(fileobj, istr): fileobj = cStringIO.StringIO(str(fileobj))
return gzip.GzipFile(mode='rb', fileobj=fileobj).read()
## mtime functions
def mtime(path):
""" return last modification time. """
try: return os.stat(os.getcwd + os.sep + package.replace(".", os.sep))[stat.ST_MTIME]
except: pass
|