Skip to content

Commit

Permalink
Merge pull request ome#2398 from bpindelski/rebased/develop/12146_all…
Browse files Browse the repository at this point in the history
…_groups

CLI download looks for resources in all user groups (fix #12146). (rebased onto develop)
  • Loading branch information
joshmoore committed May 1, 2014
2 parents ee10a14 + 6c6a700 commit f1efece
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
33 changes: 26 additions & 7 deletions components/tools/OmeroPy/src/omero/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(self, args = None, id = None, \
if id.properties == None:
id.properties = Ice.createProperties(args)

id.properties.parseCommandLineOptions("omero", args);
id.properties.parseCommandLineOptions("omero", args)
if host:
id.properties.setProperty("omero.host", str(host))
if not port:
Expand Down Expand Up @@ -175,7 +175,7 @@ def _initData(self, id):
"""

if not id:
raise omero.ClientError("No initialization data provided.");
raise omero.ClientError("No initialization data provided.")

# Strictly necessary for this class to work
id.properties.setProperty("Ice.ImplicitContext", "Shared")
Expand Down Expand Up @@ -408,6 +408,18 @@ def getImplicitContext(self):
"""
return self.getCommunicator().getImplicitContext()

def getContext(self, group=None):
"""
Returns a copy of the implicit context's context, i.e.
dict(getImplicitContext().getContext()) for use as the
last argument to any remote method.
"""
ctx = self.getImplicitContext().getContext()
ctx = dict(ctx)
if group is not None:
ctx["omero.group"] = str(group)
return ctx

def getProperties(self):
"""
Returns the active properties for this instance
Expand Down Expand Up @@ -504,7 +516,7 @@ def createSession(self, username=None, password=None):
self.__logger.warning(\
"%s - createSession retry: %s"% (reason, retries) )
try:
ctx = dict(self.getImplicitContext().getContext())
ctx = self.getContext()
ctx[omero.constants.AGENT] = self.__agent
if self.__ip is not None:
ctx[omero.constants.IP] = self.__ip
Expand Down Expand Up @@ -761,16 +773,23 @@ def upload(self, filename, name = None, path = None,
return ofile

def download(self, ofile, filename = None, block_size = 1024*1024, filehandle = None):
if not self.__sf:
raise omero.ClientError("No session. Use createSession first.")

# Search for objects in all groups. See #12146
ctx = self.getContext(group=-1)
prx = self.__sf.createRawFileStore()

try:
if not ofile or not ofile.id:
raise omero.ClientError("No file to download")
ofile = self.__sf.getQueryService().get("OriginalFile", ofile.id.val)
ofile = self.__sf.getQueryService().get("OriginalFile", ofile.id.val,
ctx)

if block_size > ofile.size.val:
block_size = ofile.size.val

prx.setFileId(ofile.id.val)
prx.setFileId(ofile.id.val, ctx)

size = ofile.size.val
offset = 0
Expand Down Expand Up @@ -888,7 +907,7 @@ def killSession(self):
except:
self.__logger.warning("Cannot get session service for killSession. Using closeSession")
self.closeSession()
return -1;
return -1

count = 0
try:
Expand Down Expand Up @@ -1025,7 +1044,7 @@ def _noop(self):
pass
def _closeSession(self):
try:
self.oa.deactivate();
self.oa.deactivate()
except Exception, e:
sys.err.write("On session closed: " + str(e))

Expand Down
10 changes: 6 additions & 4 deletions components/tools/OmeroPy/src/omero/plugins/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def get_file_id(self, session, value):
query = session.getQueryService()
if ':' not in value:
try:
ofile = query.get("OriginalFile", long(value))
ofile = query.get("OriginalFile", long(value),
{'omero.group': '-1'})
return ofile.id.val
except ValueError:
self.ctx.die(601, 'Invalid OriginalFile ID input')
Expand All @@ -82,7 +83,8 @@ def get_file_id(self, session, value):
file_id = self.parse_object_id("OriginalFile", value)
if file_id:
try:
ofile = query.get("OriginalFile", file_id)
ofile = query.get("OriginalFile", file_id,
{'omero.group': '-1'})
except omero.ValidationException:
self.ctx.die(601, 'No OriginalFile with input ID')
return ofile.id.val
Expand All @@ -91,7 +93,7 @@ def get_file_id(self, session, value):
fa_id = self.parse_object_id("FileAnnotation", value)
if fa_id:
try:
fa = query.get("FileAnnotation", fa_id)
fa = query.get("FileAnnotation", fa_id, {'omero.group': '-1'})
except omero.ValidationException:
self.ctx.die(601, 'No FileAnnotation with input ID')
return fa.getFile().id.val
Expand All @@ -106,7 +108,7 @@ def get_file_id(self, session, value):
" join fs.usedFiles as uf" \
" join uf.originalFile as f" \
" where i.id = :iid"
query_out = query.projection(sql, params)
query_out = query.projection(sql, params, {'omero.group': '-1'})
if not query_out:
self.ctx.die(602, 'Input image has no associated Fileset')
if len(query_out) > 1:
Expand Down
48 changes: 48 additions & 0 deletions components/tools/OmeroPy/test/integration/clitest/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def create_original_file(self, content):
finally:
rfs.close()

def setup_user_and_two_groups(self):
group1 = self.new_group(perms='rw----')
group2 = self.new_group(perms='rw----')
user = self.new_user()
self.add_groups(user, [group1, group2], owner=True)
return user, group1, group2

@py.test.mark.parametrize(
'bad_input',
['-1', 'OriginalFile:-1', 'FileAnnotation:-1', 'Image:-1'])
Expand Down Expand Up @@ -87,6 +94,17 @@ def testOriginalFileStdout(self, prefix, capsys):
out, err = capsys.readouterr()
assert out == "test"

@py.test.mark.parametrize('prefix', ['', 'OriginalFile:'])
def testOriginalFileMultipleGroups(self, prefix, capsys):
user, group1, group2 = self.setup_user_and_two_groups()
client = self.new_client(user=user, password="ome")
ofile = self.create_original_file("test")
self.set_context(client, group2.id.val)
self.args += ['%s%s' % (prefix, str(ofile.id.val)), '-']
self.cli.invoke(self.args, strict=True)
out, err = capsys.readouterr()
assert out == "test"

# FileAnnotation tests
# ========================================================================
def testNonExistingFileAnnotation(self, tmpdir):
Expand Down Expand Up @@ -119,6 +137,19 @@ def testFileAnnotationStdout(self, capsys):
out, err = capsys.readouterr()
assert out == "test"

def testFileAnnotationMultipleGroups(self, capsys):
user, group1, group2 = self.setup_user_and_two_groups()
client = self.new_client(user=user, password="ome")
ofile = self.create_original_file("test")
fa = omero.model.FileAnnotationI()
fa.setFile(ofile)
fa = self.update.saveAndReturnObject(fa)
self.set_context(client, group2.id.val)
self.args += ['FileAnnotation:%s' % str(fa.id.val), '-']
self.cli.invoke(self.args, strict=True)
out, err = capsys.readouterr()
assert out == "test"

# Image tests
# ========================================================================
def testNonExistingImage(self, tmpdir):
Expand Down Expand Up @@ -163,3 +194,20 @@ def testImageNoFileset(self, tmpdir):
self.args += ["Image:%s" % pixels.getImage().id.val, str(tmpfile)]
with py.test.raises(NonZeroReturnCode):
self.cli.invoke(self.args, strict=True)

def testImageMultipleGroups(self, tmpdir):
user, group1, group2 = self.setup_user_and_two_groups()
client = self.new_client(user=user, password="ome")
filename = self.OmeroPy / ".." / ".." / ".." / \
"components" / "common" / "test" / "tinyTest.d3d.dv"
with open(filename) as f:
bytes1 = f.read()
pix_ids = self.import_image(filename)
pixels = self.query.get("Pixels", long(pix_ids[0]))
tmpfile = tmpdir.join('test')
self.set_context(client, group2.id.val)
self.args += ["Image:%s" % pixels.getImage().id.val, str(tmpfile)]
self.cli.invoke(self.args, strict=True)
with open(str(tmpfile)) as f:
bytes2 = f.read()
assert bytes1 == bytes2

0 comments on commit f1efece

Please sign in to comment.