Skip to content

Commit

Permalink
Refactor code to allow reusing client session
Browse files Browse the repository at this point in the history
  • Loading branch information
SupraSummus committed Jan 24, 2020
1 parent 3c399a3 commit 1697d8a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
33 changes: 20 additions & 13 deletions bin/ipfs-api-mount
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

import argparse
import socket

import fuse
import ipfshttpclient

from ipfs_api_mount import IPFSMount

Expand All @@ -21,16 +23,21 @@ if __name__ == '__main__':

args = parser.parse_args()

fuse.FUSE(
IPFSMount(
args.root,
ls_cache_size=args.ls_cache_size,
object_data_cache_size=args.object_data_cache_size,
object_links_cache_size=args.object_links_cache_size,
api_host=args.api_host,
api_port=args.api_port,
),
args.mountpoint,
foreground=not args.background,
nothreads=not args.multithreaded,
)
ip = socket.gethostbyname(args.api_host)

with ipfshttpclient.connect(
'/ip4/{}/tcp/{}/http'.format(ip, args.api_port)
) as client:

fuse.FUSE(
IPFSMount(
args.root,
client,
ls_cache_size=args.ls_cache_size,
object_data_cache_size=args.object_data_cache_size,
object_links_cache_size=args.object_links_cache_size,
),
args.mountpoint,
foreground=not args.background,
nothreads=not args.multithreaded,
)
7 changes: 3 additions & 4 deletions ipfs_api_mount/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from functools import lru_cache
import errno
import os
import socket
import stat

import fuse
Expand All @@ -19,18 +18,18 @@ class IPFSMount(fuse.Operations):
def __init__(
self,
root, # root IPFS path
api_host='127.0.0.1', api_port=5001,
ipfs_client, # ipfshttpclient client instance
ls_cache_size=64,
object_data_cache_size=256, # ~256MB assuming 1MB max block size
object_links_cache_size=256,
ready=None, # an event to notify that everything is set-up
):
ip = socket.gethostbyname(api_host)
api = ipfshttpclient.connect('/ip4/{}/tcp/{}/http'.format(ip, api_port))
self.root = root

# trick to get lrucache use only one arg

api = ipfs_client

@lru_cache(maxsize=object_data_cache_size)
def object_data(object_id):
try:
Expand Down
5 changes: 2 additions & 3 deletions ipfs_api_mount/ipfs_mounted.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@contextmanager
def ipfs_mounted(root, host='localhost', port=5001, multithreaded=False, **kwargs):
def ipfs_mounted(root, ipfs_client, multithreaded=False, **kwargs):
with tempfile.TemporaryDirectory() as mountpoint:
# start fuse thread
ready = Event()
Expand All @@ -17,8 +17,7 @@ def _do_fuse_things():
fuse.FUSE(
IPFSMount(
root,
api_host=host,
api_port=port,
ipfs_client,
ready=ready,
**kwargs,
),
Expand Down
12 changes: 6 additions & 6 deletions tests/test_mounted_filesystem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .tools import ipfs_dir, ipfs_file
from .tools import ipfs_client, ipfs_dir, ipfs_file
from ipfs_api_mount.ipfs_mounted import ipfs_mounted
from unittest import TestCase
import os
Expand All @@ -7,7 +7,7 @@
class DirectoryTestCase(TestCase):
def test_empty_dir(self):
root = ipfs_dir({})
with ipfs_mounted(root) as mountpoint:
with ipfs_mounted(root, ipfs_client) as mountpoint:
self.assertEqual(
os.listdir(mountpoint),
[],
Expand All @@ -18,7 +18,7 @@ def test_nonempty_dir(self):
'aaa': ipfs_dir({}),
'bbb': ipfs_dir({}),
})
with ipfs_mounted(root) as mountpoint:
with ipfs_mounted(root, ipfs_client) as mountpoint:
self.assertEqual(
os.listdir(mountpoint),
['aaa', 'bbb'],
Expand All @@ -29,7 +29,7 @@ def test_file_times(self):
root = ipfs_dir({
'bbb': ipfs_file(b'blabla'),
})
with ipfs_mounted(root) as mountpoint:
with ipfs_mounted(root, ipfs_client) as mountpoint:
s = os.stat(os.path.join(mountpoint, 'bbb'))
self.assertEqual(s.st_ctime, 0)
self.assertEqual(s.st_mtime, 0)
Expand All @@ -40,7 +40,7 @@ class FileTestCase(TestCase):
def test_small_file_read(self):
content = b'I forgot newline at the end. Ups.'
root = ipfs_dir({'file': ipfs_file(content)})
with ipfs_mounted(root) as mountpoint:
with ipfs_mounted(root, ipfs_client) as mountpoint:
with open(os.path.join(mountpoint, 'file'), 'rb') as f:
self.assertEqual(
f.read(),
Expand All @@ -50,7 +50,7 @@ def test_small_file_read(self):
def test_10MiB_file_read(self):
content = os.urandom(10 * 1024 * 1024)
root = ipfs_dir({'file': ipfs_file(content)})
with ipfs_mounted(root) as mountpoint:
with ipfs_mounted(root, ipfs_client) as mountpoint:
with open(os.path.join(mountpoint, 'file'), 'rb') as f:
self.assertEqual(
f.read(),
Expand Down
8 changes: 4 additions & 4 deletions tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import ipfshttpclient


api = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001/http')
ipfs_client = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001/http')


def ipfs_file(content):
# workaround for https://github.com/ipfs/py-ipfs-http-client/issues/187
with tempfile.NamedTemporaryFile() as f:
f.write(content)
f.flush()
return api.add(f.name)['Hash']
return ipfs_client.add(f.name)['Hash']


def ipfs_dir(contents):
node = api.object.new(template='unixfs-dir')['Hash']
node = ipfs_client.object.new(template='unixfs-dir')['Hash']
for name, val in contents.items():
node = api.object.patch.add_link(node, name, val)['Hash']
node = ipfs_client.object.patch.add_link(node, name, val)['Hash']
return node

0 comments on commit 1697d8a

Please sign in to comment.