Skip to content

Commit

Permalink
removed all ref to os for uos in client
Browse files Browse the repository at this point in the history
  • Loading branch information
revesansparole committed Feb 9, 2016
1 parent aeebe3d commit f727a0c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 24 deletions.
30 changes: 30 additions & 0 deletions src/oaserver/dirac_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ def ls(url):
return pths


def ls_dir(url):
"""List all available resources at the given location
Warnings: work only on directory like resources.
Args:
url: (urlparse.SplitResult) Resource locator
Returns:
(list of str): list of resources names.
"""
root = url.path
res = check_output(["dirac-dms-find-lfns Path=%s" % root], shell=True)

pths = set()

for line in res.splitlines()[1:]:
line = line.strip()
if len(line) > 0:
if line.startswith(root):
pth = line[len(root):][1:]
dname = os.path.dirname(pth)
if len(dname) > 0:
pths.add(pth)
else:
raise URLError("bad path")

return pths


def exists(url):
"""Check the existence of a resource.
Expand Down
46 changes: 22 additions & 24 deletions src/oaserver/oa_client_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
with scifloware specifications to communicate with OAServers.
"""

from os import listdir, remove
from os.path import exists, isdir
from os.path import join as pj
from time import sleep

from oaserver.json_tools import get_json, post_json
from .json_tools import get_json, post_json
from .uos import exists, ls_dir, remove, URLError


class OAClientFile(object):
Expand All @@ -34,30 +32,30 @@ def connect(self):
if self._sid is not None:
raise UserWarning("Already connected, clear first?")

for name in listdir(self._com_pth):
pth = pj(self._com_pth, name)
if isdir(pth):
stdout = pj(pth, "stdout")
if exists(stdout):
if exists(pj(stdout, "reg.json")):
reg = get_json(pj(stdout, "reg.json"))
remove(pj(stdout, "reg.json"))

assert reg['type'] == "FileSystemEngine"
args = reg['args']
self._sid = args['id']
self._stdout = stdout
self._compute_pth = pj(pth, "stdin", args["url"])
self._ping_pth = pj(pth, "stdin", args["urlping"])
self._delete_pth = pj(pth, "stdin", args["urldelete"])
return self._sid
for name in ls_dir(self._com_pth):
pth = self._com_pth + "/" + name
stdout = pth + "/stdout"
reg_pth = stdout + "/reg.json"
try:
reg = get_json(reg_pth)
assert reg['type'] == "FileSystemEngine"
remove(reg_pth)
args = reg['args']
self._sid = args['id']
self._stdout = stdout
self._compute_pth = pth + "/stdin/" + args["url"]
self._ping_pth = pth + "/stdin/" + args["urlping"]
self._delete_pth = pth + "/stdin/" + args["urldelete"]
return self._sid
except URLError:
pass

return None

def ping(self):
"""Poll associated server.
"""
res_pth = pj(self._stdout, "ping.json")
res_pth = self._stdout + "/ping.json"
if exists(res_pth):
raise UserWarning("already existing ping answer!!!")

Expand Down Expand Up @@ -92,7 +90,7 @@ def compute(self, pycode, data):
if self._sid is None:
raise UserWarning("No registered server, connect first???")

res_pth = pj(self._stdout, "result.json")
res_pth = self._stdout + "/result.json"
if exists(res_pth):
raise UserWarning("already existing computation answer!!!")

Expand All @@ -109,7 +107,7 @@ def get_result(self):
Returns:
(any) actual result of script call
"""
res_pth = pj(self._stdout, "result.json")
res_pth = self._stdout + "/result.json"
if not exists(res_pth):
raise UserWarning("Computation still running????")

Expand Down
44 changes: 44 additions & 0 deletions src/oaserver/uos.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,50 @@ def ls(url):
return names


def ls_dir(url):
"""List all directories at a given location.
Args:
url: (url) path to check
Returns:
(list of str): list of directory names
"""
url = ensure_url(url)

if url.netloc != "":
raise UserWarning("don't know how to handle web ls")

if url.scheme == "dirac":
return dirac_api.ls_dir(url)
else:
names = []
for name in os.listdir(url.path):
if os.path.isdir(os.path.join(url.path, name)):
names.append(name)

return names


def exists(url):
"""Check the existence of a resource.
Args:
url: (urlparse.SplitResult) Resource locator
Returns:
(Bool): True if resource is accessible
"""
url = ensure_url(url)
if url.netloc != "":
raise UserWarning("don't know how to handle web ls")

if url.scheme == "dirac":
return dirac_api.exists(url)
else:
return os.path.exists(url.path)


def remove(url):
"""Delete specified file.
Expand Down

0 comments on commit f727a0c

Please sign in to comment.