Commit 6b9c4fd6 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

try to add signalhandling, not working yet

parent b2814827
......@@ -4,6 +4,7 @@ import argparse
import sys
import os.path
import logging
import signal
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
......@@ -15,8 +16,12 @@ from walker import Walker
from mbdata.models import Base, Recording
from mbdata import patch_model_schemas, NO_SCHEMAS
class Main:
def signal_handler(signal, frame):
logger.info("received Ctrl+C, aborting...")
main.shutdown()
sys.exit(0)
class Main:
def init_database(self, dbfile):
engine = create_engine('sqlite:///' + dbfile,
connect_args={'check_same_thread':False},
......@@ -58,9 +63,15 @@ class Main:
collector.join()
translator.join()
def shutdown(self):
collector.shutdown() #module method
logger = logging.getLogger('main')
main = Main()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-5s %(name)-18s %(message)s',
datefmt='%y-%m-%d %H:%M')
logger = logging.getLogger('main')
Main().main(sys.argv)
signal.signal(signal.SIGINT, signal_handler)
main.main(sys.argv)
......@@ -14,7 +14,10 @@ from mbdata.models import Recording, ArtistCredit, ArtistCreditName, Artist, Rel
from utils import pairwise
from fetcher import Fetcher
Paths = queue.Queue(maxsize=10)
Paths = queue.Queue(maxsize=3)
def shutdown():
Paths.put(("end", "of life"))
class Collector(threading.Thread):
"""
......@@ -30,11 +33,15 @@ class Collector(threading.Thread):
def run(self):
while True:
mbid, path = Paths.get()
if mbid == "end":
break
self.log.info("Working on file: {}".format(path))
self.fetch_recording(mbid, path)
self._session.commit()
Paths.task_done()
self._session.commit()
self._session.close()
self.log.info("successful collector shutdown")
def create_artist_credit(self, acresult, acphrase):
ac = self._session.query(ArtistCredit).filter_by(name=acphrase).first()
......
......@@ -3,11 +3,15 @@ import llfuse
from hlfuse.model import *
from hlfuse.operations import Operations
def init(mountpoint, rootnode, options=[]):
def init(mountpoint, rootnode, options=[], single=True):
opfs = Operations(rootnode)
llfuse.init(opfs, mountpoint, options)
try:
llfuse.main(single=True)
llfuse.main(single)
except:
print("MAIN LOOP STOPPED")
llfuse.close(unmount=False)
raise
def close(ummount=True):
llfuse.close(unmount)
#!/usr/bin/env python3
import errno, llfuse, sys, logging, stat, os, time
log = logging.getLogger()
fse = sys.getfilesystemencoding()
def bytes2str(s):
return s.decode(fse, 'surrogateescape')
def str2bytes(s):
return s.encode(fse, 'surrogateescape')
class Testfs(llfuse.Operations):
def __init__(self):
super(llfuse.Operations, self).__init__()
self.hellopath = "/hello"
self.hellostring = "hello world"
self.inode2path = {llfuse.ROOT_INODE: "/",
llfuse.ROOT_INODE+1: self.hellopath}
self.path2inode = {"/": llfuse.ROOT_INODE,
self.hellopath: llfuse.ROOT_INODE+1}
def getattr(self, inode):
entry = llfuse.EntryAttributes()
if inode in self.inode2path:
if self.inode2path[inode] is "/":
entry.st_mode = (stat.S_IFDIR | 0o755)
entry.st_nlink = 2
entry.st_size = 4096
elif self.inode2path[inode] is self.hellopath:
entry.st_mode = (stat.S_IFREG | 0o644)
entry.st_nlink = 1
entry.st_size = len(self.hellostring) + 1
else:
raise(llfuse.FUSEError(errno.ENOENT))
entry.attr_timeout = 300
entry.entry_timeout = 300
entry.generation = 0
entry.st_atime = int(time.time())
entry.st_blksize = 512
entry.st_blocks = 1
entry.st_ctime = int(time.time())
entry.st_gid = os.getgid()
entry.st_ino = inode
entry.st_mtime = int(time.time())
entry.st_rdev = 1
entry.st_uid = os.getuid()
return entry
def lookup(self, parent_inode, name):
path = self.inode2path[parent_inode] + bytes2str(name)
if path != self.hellopath:
raise(llfuse.FUSEError(errno.ENOENT))
return self.getattr(self.path2inode[path])
def opendir(self, inode):
if inode not in self.inode2path or self.inode2path[inode] != '/':
raise(llfuse.FUSEError(errno.ENOENT))
return inode
def readdir(self, fh, off):
if fh not in self.inode2path or self.inode2path[fh] != "/":
raise(llfuse.FUSEError(errno.ENOENT))
#only one value
if off is 1:
return
yield (str2bytes(self.hellopath[1:]), self.getattr(self.path2inode[self.hellopath]), 1)
def open(self, inode, flags):
if self.inode2path[inode] != self.hellopath:
raise(llfuse.FUSEError(errno.ENOENT))
if flags&3 != os.O_RDONLY:
raise(llfuse.FUSEError(errno.EACCES))
return inode
def read(self, fh, off, size):
if self.inode2path[fh] != self.hellopath:
raise(llfuse.FUSEError(errno.ENOENT))
return str2bytes(self.hellostring[off:off+size])
def init_logging():
formatter = logging.Formatter('%(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
log.setLevel(logging.INFO)
log.addHandler(handler)
def main():
init_logging()
testfs = Testfs()
llfuse.init(testfs, sys.argv[1], [ 'fsname=hellofs' ])
try:
llfuse.main(single=True)
except:
llfuse.close(unmount=False)
raise
llfuse.close()
if __name__ == '__main__':
main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
lltest.py - Example file system for Python-LLFUSE.
This program presents a static file system containing a single file. It is
compatible with both Python 2.x and 3.x. Based on an example from Gerion Entrup.
Copyright © 2015 Nikolaus Rath <Nikolaus.org>
Copyright © 2015 Gerion Entrup.
This file is part of Python-LLFUSE. This work may be distributed under
the terms of the GNU LGPL.
'''
from __future__ import division, print_function, absolute_import
import os
import sys
# We are running from the Python-LLFUSE source directory, make sure
# that we use modules from this directory
basedir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..'))
if (os.path.exists(os.path.join(basedir, 'setup.py')) and
os.path.exists(os.path.join(basedir, 'src', 'llfuse'))):
sys.path = [os.path.join(basedir, 'src')] + sys.path
from argparse import ArgumentParser
import stat
import logging
import errno
import llfuse
log = logging.getLogger(__name__)
class TestFs(llfuse.Operations):
def __init__(self):
super(TestFs, self).__init__()
self.hello_name = b"message"
self.hello_inode = llfuse.ROOT_INODE+1
self.hello_data = b"hello world\n"
def getattr(self, inode):
entry = llfuse.EntryAttributes()
if inode == llfuse.ROOT_INODE:
entry.st_mode = (stat.S_IFDIR | 0o755)
entry.st_size = 0
entry.st_nlink = 2
elif inode == self.hello_inode:
entry.st_mode = (stat.S_IFREG | 0o644)
entry.st_size = len(self.hello_data)
entry.st_nlink = 1
else:
raise llfuse.FUSEError(errno.ENOENT)
stamp = 1438467123.985654
entry.st_atime = stamp
entry.st_ctime = stamp
entry.st_mtime = stamp
entry.st_gid = os.getgid()
entry.st_uid = os.getuid()
entry.st_ino = inode
entry.st_rdev = 0
entry.attr_timeout = 1
entry.entry_timeout = 1
entry.generation = 1
entry.st_blksize = 1
entry.st_blocks = 1
return entry
def lookup(self, parent_inode, name):
if parent_inode != llfuse.ROOT_INODE or name != self.hello_name:
raise llfuse.FUSEError(errno.ENOENT)
return self.getattr(self.hello_inode)
def opendir(self, inode):
if inode != llfuse.ROOT_INODE:
raise llfuse.FUSEError(errno.ENOENT)
return inode
def readdir(self, fh, off):
assert fh == llfuse.ROOT_INODE
# only one entry
if off == 0:
yield (self.hello_name, self.getattr(self.hello_inode), 1)
def open(self, inode, flags):
if inode != self.hello_inode:
raise llfuse.FUSEError(errno.ENOENT)
if flags & os.O_RDWR or flags & os.O_WRONLY:
raise llfuse.FUSEError(errno.EPERM)
return inode
def read(self, fh, off, size):
assert fh == self.hello_inode
return self.hello_data[off:off+size]
def init_logging(debug=False):
formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(threadName)s: '
'[%(name)s] %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger()
if debug:
handler.setLevel(logging.DEBUG)
root_logger.setLevel(logging.DEBUG)
else:
handler.setLevel(logging.INFO)
root_logger.setLevel(logging.INFO)
root_logger.addHandler(handler)
def parse_args():
'''Parse command line'''
parser = ArgumentParser()
parser.add_argument('mountpoint', type=str,
help='Where to mount the file system')
parser.add_argument('--debug', action='store_true', default=False,
help='Enable debugging output')
return parser.parse_args()
def main():
options = parse_args()
init_logging(options.debug)
testfs = TestFs()
llfuse.init(testfs, options.mountpoint, [ 'fsname=lltest' ])
try:
llfuse.main(single=True)
except:
llfuse.close(unmount=False)
raise
llfuse.close()
if __name__ == '__main__':
main()
#!/usr/bin/env python3
import sys, signal, threading, llfuse
from lltest import TestFs
class Filesystem(threading.Thread):
def __init__(self, mpoint):
super().__init__(target=self)
self.mpoint = mpoint
def run(self):
testfs = TestFs()
llfuse.init(testfs, self.mpoint, [])
try:
llfuse.main(True)
except:
print("Exception occurs")
llfuse.close()
raise
llfuse.close()
def signal_handler(signal, frame):
print("received Ctrl+C or SIGTERM, aborting...")
# do some cleanup stuff in other threads and end them
# how can I say the fusefilesystem to stop?
sys.exit(0) # this seems to have no effect
if len(sys.argv) != 3:
print("Usage: {} <mountpoint> <true|false>".format(sys.argv[0]))
print(" <mountpoint>: Path to mountpoint")
print(" <true|false>: Use the signal_handler or not")
sys.exit(1)
if sys.argv[2] == "true":
signal.signal(signal.SIGINT, signal_handler) # for Ctrl-C
signal.signal(signal.SIGTERM, signal_handler)
# start thread
filesystem = Filesystem(sys.argv[1])
filesystem.start()
filesystem.join()
import hlfuse, os
import threading
import queue
from mbdata.models import Recording, Release, ReleaseGroup, Track, Artist, ArtistCredit, ArtistCreditName, Medium
from sqlalchemy import distinct
_cache = {}
_sig = queue.Queue(maxsize=1)
def _get_file(recording):
if recording not in _cache:
_cache[recording] = BrainzFile(0o444, recording.path)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment