svc.api_token = token
kwargs['http'].cache = None
return svc
-
-class SafeApi(object):
- """Threadsafe wrapper for API object. This stores and returns a different api
- object per thread, because httplib2 which underlies apiclient is not
- threadsafe.
- """
-
- def __init__(self, config=None, keep_params={}):
- if not config:
- config = arvados.config.settings()
- self.host = config.get('ARVADOS_API_HOST')
- self.api_token = config.get('ARVADOS_API_TOKEN')
- self.insecure = config.flag_is_true('ARVADOS_API_HOST_INSECURE')
- self.local = threading.local()
- self.keep = arvados.KeepClient(api_client=self, **keep_params)
-
- def localapi(self):
- if 'api' not in self.local.__dict__:
- self.local.api = arvados.api('v1', False, self.host,
- self.api_token, self.insecure)
- return self.local.api
-
- def __getattr__(self, name):
- # Proxy nonexistent attributes to the thread-local API client.
- try:
- return getattr(self.localapi(), name)
- except AttributeError:
- return super(SafeApi, self).__getattr__(name)
@_synchronized
def clone(self, new_parent):
"""Make a copy of this file."""
- cp = ArvadosFile()
- cp.parent = new_parent
+ cp = ArvadosFile(new_parent)
cp._modified = False
map_loc = {}
map_loc[r.locator] = self.parent._my_block_manager().dup_block(r.locator, cp).blockid
new_loc = map_loc[r.locator]
- cp.segments.append(Range(new_loc, r.range_start, r.range_size, r.segment_offset))
+ cp._segments.append(Range(new_loc, r.range_start, r.range_size, r.segment_offset))
return cp
from keep import *
from .stream import StreamReader, normalize_stream, locator_block_size
from .ranges import Range, LocatorAndRange
+from .safeapi import SafeApi
import config
import errors
import util
if p[0] == '.':
del p[0]
- if len(p) > 0:
+ if p and p[0]:
item = self._items.get(p[0])
if len(p) == 1:
# item must be a file
@_must_be_writable
@_synchronized
- def copyto(self, target_path, source_path, source_collection=None, overwrite=False):
+ def copy(self, source_path, target_path, source_collection=None, overwrite=False):
"""
copyto('/foo', '/bar') will overwrite 'foo' if it exists.
copyto('/foo/', '/bar') will place 'bar' in subcollection 'foo'
# Find parent collection the target path
tp = target_path.split("/")
- target_dir = self.find(tp[0:-1].join("/"), create=True, create_collection=True)
+ target_dir = self.find("/".join(tp[0:-1]), create=True, create_collection=True)
# Determine the name to use.
target_name = tp[-1] if tp[-1] else sp[-1]
- if target_name in target_dir and not overwrite:
- raise IOError((errno.EEXIST, "File already exists"))
+ if target_name in target_dir:
+ if isinstance(target_dir[target_name], SynchronizedCollectionBase):
+ target_dir = target_dir[target_name]
+ target_name = sp[-1]
+ elif not overwrite:
+ raise IOError((errno.EEXIST, "File already exists"))
# Actually make the copy.
dup = source_obj.clone(target_dir)
self[k].merge(other[k])
else:
if self[k] != other[k]:
- name = "%s~conflict-%s~" % (k, time.strftime("%Y-%m-%d~%H:%M%:%S",
+ name = "%s~conflict-%s~" % (k, time.strftime("%Y-%m-%d_%H:%M%:%S",
time.gmtime()))
self[name] = other[k].clone(self)
self.notify(self, name, ADD, self[name])
@_synchronized
def _my_api(self):
if self._api_client is None:
- self._api_client = arvados.api.SafeApi(self._config)
+ self._api_client = arvados.SafeApi(self._config)
self._keep_client = self._api_client.keep
return self._api_client
--- /dev/null
+import threading
+import api
+import keep
+import config
+
+class SafeApi(object):
+ """Threadsafe wrapper for API object. This stores and returns a different api
+ object per thread, because httplib2 which underlies apiclient is not
+ threadsafe.
+ """
+
+ def __init__(self, apiconfig=None, keep_params={}):
+ if not apiconfig:
+ apiconfig = config
+ self.host = apiconfig.get('ARVADOS_API_HOST')
+ self.api_token = apiconfig.get('ARVADOS_API_TOKEN')
+ self.insecure = apiconfig.flag_is_true('ARVADOS_API_HOST_INSECURE')
+ self.local = threading.local()
+ self.keep = keep.KeepClient(api_client=self, **keep_params)
+
+ def localapi(self):
+ if 'api' not in self.local.__dict__:
+ self.local.api = api.api('v1', False, self.host,
+ self.api_token, self.insecure)
+ return self.local.api
+
+ def __getattr__(self, name):
+ # Proxy nonexistent attributes to the thread-local API client.
+ try:
+ return getattr(self.localapi(), name)
+ except AttributeError:
+ return super(SafeApi, self).__getattr__(name)
self.assertEqual(False, c.modified())
self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
- def test_remove(self):
- with import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
- self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", export_manifest(c))
- self.assertTrue("count1.txt" in c)
- c.remove("count1.txt")
- self.assertFalse("count1.txt" in c)
- self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", export_manifest(c))
-
- def test_remove_in_subdir(self):
- with import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
- c.remove("foo/count2.txt")
- self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
-
- def test_remove_empty_subdir(self):
- with import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
- c.remove("foo/count2.txt")
- c.remove("foo")
- self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
-
- def test_remove_empty_subdir(self):
- with import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
- with self.assertRaises(IOError):
- c.remove("foo")
- c.remove("foo", rm_r=True)
- self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
-
- def test_prefetch(self):
- keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
- with import_manifest(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep=keep) as c:
- r = c.open("count.txt", "r")
- self.assertEqual("0123", r.read(4))
- self.assertTrue("2e9ec317e197819358fbc43afca7d837+8" in keep.requests)
- self.assertTrue("e8dc4081b13434b45189a720b77b6818+8" in keep.requests)
class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
class MockParent(object):
sfile.read(5)
self.assertEqual(3, sfile.tell())
+ def test_prefetch(self):
+ keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
+ with import_manifest(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep=keep) as c:
+ r = c.open("count.txt", "r")
+ self.assertEqual("0123", r.read(4))
+ self.assertTrue("2e9ec317e197819358fbc43afca7d837+8" in keep.requests)
+ self.assertTrue("e8dc4081b13434b45189a720b77b6818+8" in keep.requests)
+
class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
def reader_for(self, coll_name, **kwargs):
import run_test_server
import arvados_testutil as tutil
from arvados.ranges import Range, LocatorAndRange
+from arvados import import_manifest, export_manifest
+from arvados.arvfile import SYNC_EXPLICIT
class TestResumableWriter(arvados.ResumableCollectionWriter):
KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
"""
self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", arvados.export_manifest(arvados.Collection(m1)))
+
+ def test_remove(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", export_manifest(c))
+ self.assertTrue("count1.txt" in c)
+ c.remove("count1.txt")
+ self.assertFalse("count1.txt" in c)
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", export_manifest(c))
+
+ def test_remove_in_subdir(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.remove("foo/count2.txt")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+ def test_remove_empty_subdir(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.remove("foo/count2.txt")
+ c.remove("foo")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+ def test_remove_nonempty_subdir(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+ with self.assertRaises(IOError):
+ c.remove("foo")
+ c.remove("foo", rm_r=True)
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+ def test_copy_to_dir1(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.copy("count1.txt", "foo/count2.txt")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", export_manifest(c))
+
+ def test_copy_to_dir2(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.copy("count1.txt", "foo")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+ def test_copy_to_dir2(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.copy("count1.txt", "foo/")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+ def test_copy_file(self):
+ with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+ c.copy("count1.txt", "count2.txt")
+ self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", export_manifest(c))
+
+
if __name__ == '__main__':
unittest.main()
import arvados.commands._util as arv_cmd
from arvados_fuse import *
+from arvados.api import SafeApi
logger = logging.getLogger('arvados.arv-mount')