comparison snp-fuse.py @ 0:eefdd97a6749

planemo upload commit b'7f6183b769772449fbcee903686b8d5ec5b7439f\n'-dirty
author jpayne
date Wed, 24 Jan 2018 14:18:21 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:eefdd97a6749
1 #! /usr/bin/env python3
2
3 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
4 from multiprocessing import Process
5
6 import argparse
7 import logging
8 import os, os.path, sys
9 import subprocess
10 import tarfile
11
12
13 class ArchiveDir(LoggingMixIn, Operations):
14 "FUSE object to open a tar.bz file and expose it as a read-only directory"
15 def __init__(self, archive):
16 self.archive = archive
17 self.openfiles = {}
18
19 def readdir(self, path, fh):
20 for symbol in ['.','..']:
21 yield symbol
22 for name in self.archive.getnames():
23 yield name
24
25 # def statfs(self, fh):
26 # return dict(f_bavail=0,
27 # f_bfree=0,
28 # f_blocks=0,
29 # f_bsize=0,
30 # f_favail=0,
31 # f_ffree=0,
32 # f_files=0,
33 # f_flag=0,
34 # f_frsize=0,
35 # f_namemax=0)
36
37 def getattr(self, path, fh=None):
38 name = os.path.basename(path)
39 info = self.archive.getmember(name)
40 return dict(st_atime=info.mtime,
41 st_ctime=info.mtime,
42 st_gid=info.gid,
43 st_mode=info.mode,
44 st_nlink=2,
45 st_size=info.size,
46 st_mtime=info.mtime)
47
48 def open(self, path, flags):
49 name = os.path.basename(path)
50 self.openfiles[name] = self.archive.extractfile(name)
51
52 def release(self, path, fh):
53 name = os.path.basename(path)
54 self.openfiles[name].close()
55 del self.openfiles[name]
56
57 def read(self, path, length, offset, fh):
58 name = os.path.basename(path)
59 fh = self.openfiles[name]
60 fh.seek(offset)
61 return fh.read(length=length)
62
63
64 def start_fs(arch, mount_point):
65 FUSE(ArchiveDir(arch),
66 mount_point,
67 nothreads=True,
68 foreground=False,
69 rdonly=True,
70 volname="snp-fuse") #does this block?
71
72
73 def main(archive, mount_point, command):
74 with tarfile.open(archive, 'r:bz2') as arch:
75 #t = Process(target=start_fs, args=(arch, mount_point), daemon=True)
76 #t.start()
77 start_fs(arch, mount_point)
78 subprocess.check_call(command, shell=True, stdout=sys.stdout, stderr=sys.stderr)
79 #t.terminate()
80 #t.join()
81
82
83 if __name__ == '__main__':
84 # create the default logger used by the logging mixin
85 logger = logging.getLogger('fuse.log-mixin')
86 logger.setLevel(logging.DEBUG)
87 # create console handler with a higher log level
88 ch = logging.StreamHandler()
89 ch.setLevel(logging.DEBUG)
90 # add the handlers to the logger
91 logger.addHandler(ch)
92 parser = argparse.ArgumentParser(description="mount a bzip2 archive for reading")
93 parser.add_argument('archive')
94 parser.add_argument('mount_point')
95 parser.add_argument('command')
96 args = parser.parse_args()
97 main(**vars(args))