Skip to content

Commit

Permalink
S1990 (#108)
Browse files Browse the repository at this point in the history
* Added support for streaming vls results
  • Loading branch information
andamian authored Jun 6, 2018
1 parent 9a9a5e5 commit 7612ca7
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 51 deletions.
7 changes: 3 additions & 4 deletions vos/test/scripts/vospace-token-atest.tcsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/tcsh -f

if (! ${?VOSPACE_WEBSERVICE} ) then
setenv VOSPACE_WEBSERVICE 'www.canfar.phys.uvic.ca'
setenv VOSPACE_WEBSERVICE 'www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca'
echo "VOSPACE_WEBSERVICE env variable not set, use default WebService URL $VOSPACE_WEBSERVICE"
else
echo "WebService URL (VOSPACE_WEBSERVICE env variable): $VOSPACE_WEBSERVICE"
Expand Down Expand Up @@ -48,9 +48,8 @@ set TOKEN = "`curl -s -d username=$username -d password=$password ${ACCESS_PAGE}

echo -n "create containers"

echo $MKDIRCMD --token="$TOKEN" -p $CONTAINER/A
$MKDIRCMD --token="$TOKEN" -p $CONTAINER/A > /dev/null || echo " [FAIL]" && exit -1
$MKDIRCMD --token="$TOKEN" $CONTAINER/B > /dev/null || echo " [FAIL]" && exit -1
$MKDIRCMD --token "$TOKEN" -p $CONTAINER/A > /dev/null || echo " [FAIL]" && exit -1
$MKDIRCMD --token "$TOKEN" $CONTAINER/B > /dev/null || echo " [FAIL]" && exit -1
echo " [OK]"

echo -n "set permissions"
Expand Down
65 changes: 30 additions & 35 deletions vos/vos/commands/vls.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ def vls():
columns.extend(
['readGroup', 'writeGroup', 'isLocked', 'size', 'date'])

# determine if their is a sorting order
sort_key = (opt.time and "date") or (opt.Size and "size") or False

# create a client to send VOSpace command
client = vos.Client(vospace_certfile=opt.certfile,
vospace_token=opt.token)
Expand All @@ -124,51 +121,49 @@ def vls():
else:
files.append(target)

info_list = []
for f in files:
client.get_node(f)
info_list = info_list + client.get_info_list(f)
# determine if their is a sorting order
if opt.Size:
sort = vos.SortNodeProperty.LENGTH
elif opt.time:
sort = vos.SortNodeProperty.DATE
else:
sort = None

if opt.reverse:
order = 'desc'
else:
order = 'asc'

_display_target(columns, info_list, opt, sort_key)
for f in files:
for row in client.get_children_info(f, sort, order):
_display_target(columns, row)

for d in dirs:
n = client.get_node(d, limit=None, force=True)
n = client.get_node(d, limit=0, force=True)
if (len(dirs) + len(files)) > 1:
sys.stdout.write('\n{}:\n'.format(n.name))
if opt.long:
sys.stdout.write('total: {}\n'.format(
int(n.get_info()['size'])))
info_list = client.get_info_list(d)
_display_target(columns, info_list, opt, sort_key)
for row in client.get_children_info(d, sort, order):
_display_target(columns, row)

except Exception as ex:
exit_on_exception(ex)


def _display_target(columns, info_list, opt, sort_key):
if sort_key:
# noinspection PyBroadException
try:
sorted_list = \
sorted(info_list,
key=lambda name: name.get_info()[sort_key],
reverse=not opt.reverse)
except Exception:
sorted_list = info_list
finally:
info_list = sorted_list
for n in info_list:
name_string = n.name
info = n.get_info()
for col in columns:
value = info.get(col, None)
value = value is not None and value or ""
if col in __LIST_FORMATS__:
sys.stdout.write(__LIST_FORMATS__[col](value))
if info["permissions"][0] == 'l':
name_string = "%s -> %s" % (
name_string, info['target'])
sys.stdout.write("%s\n" % name_string)
def _display_target(columns, row):
name_string = row.name
info = row.get_info()
for col in columns:
value = info.get(col, None)
value = value is not None and value or ""
if col in __LIST_FORMATS__:
sys.stdout.write(__LIST_FORMATS__[col](value))
if info["permissions"][0] == 'l':
name_string = "%s -> %s" % (
row.name, info['target'])
sys.stdout.write("%s\n" % name_string)


vls.__doc__ = DESCRIPTION
8 changes: 4 additions & 4 deletions vos/vos/commonparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from .version import version


# handle interrupts nicely
def signal_handler(signum, frame):
raise KeyboardInterrupt(
"SIGINT signal handler. {0} {1}".format(signum, frame))
"""Exit without calling cleanup handlers, flushing stdio buffers, etc. """
os._exit(signum)


signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGINT, signal_handler) # Ctrl-C
signal.signal(signal.SIGPIPE, signal_handler) # Pipe gone (head, more etc)


def exit_on_exception(ex):
Expand Down
34 changes: 33 additions & 1 deletion vos/vos/tests/test_vos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import os
import unittest
import pytest
import requests
from xml.etree import ElementTree
from mock import Mock, patch, MagicMock, call
from vos import Client, Connection, Node, VOFile
from vos import vos as vos
from six.moves.urllib.parse import urlparse
from six.moves import urllib


# The following is a temporary workaround for Python issue 25532
# (https://bugs.python.org/issue25532)
Expand Down Expand Up @@ -34,6 +38,33 @@ class Object(object):
pass


def test_get_node_url():
client = Client()
with pytest.raises(TypeError):
client.get_node_url('vos://cadc.nrc.ca!vospace/auser', sort='Blah')
with pytest.raises(ValueError):
client.get_node_url('vos://cadc.nrc.ca!vospace/auser', order='Blah')

response = Mock()
response.status_code = 303
client.conn.session.get = Mock(return_value=response)
equery = urlparse(client.get_node_url('vos://cadc.nrc.ca!vospace/auser',
sort=vos.SortNodeProperty.DATE)).query
assert(urllib.parse.unquote(equery) ==
'sort={}'.format(vos.SortNodeProperty.DATE.value))

equery = urlparse(client.get_node_url('vos://cadc.nrc.ca!vospace/auser',
sort=vos.SortNodeProperty.LENGTH, order='asc')).query
args = urllib.parse.unquote(equery).split('&')
assert(2 == len(args))
assert('order=asc' in args)
assert('sort={}'.format(vos.SortNodeProperty.LENGTH.value) in args)

equery = urlparse(client.get_node_url('vos://cadc.nrc.ca!vospace/auser',
order='desc')).query
assert('order=desc' == urllib.parse.unquote(equery))


class TestClient(unittest.TestCase):
"""Test the vos Client class.
"""
Expand Down Expand Up @@ -113,7 +144,8 @@ def test_get_info_list(self):
mock_link_node.target = 'vos:/somefile'
client = Client()
client.get_node = MagicMock(side_effect=[mock_link_node, mock_node])
self.assertEquals([mock_node], client.get_info_list('vos:/somenode'))
self.assertEquals([mock_node],
client.get_children_info('vos:/somenode'))

def test_nodetype(self):
mock_node = MagicMock(id=333)
Expand Down
Loading

0 comments on commit 7612ca7

Please sign in to comment.