Skip to content

Commit

Permalink
Merge pull request #46 from pigay/irods-v4
Browse files Browse the repository at this point in the history
Irods v4 partial compatibility
  • Loading branch information
pigay authored Jul 3, 2020
2 parents 96c3f7f + 33301ea commit f6571e2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
46 changes: 32 additions & 14 deletions mcia_irods_utils/icommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import string
import re
import subprocess
import json

class IrodsCommand:
"An iRODS iCommand wrapper"
Expand Down Expand Up @@ -46,9 +47,18 @@ def __call__( self, cmdline = [] ):
for l in stdout:
yield self.output_filter( l )

def parse_env(path):
def env_file3(pid):
repo = os.path.expanduser(os.path.join("~", ".irods"))
return os.path.join(repo, ".irodsEnv.%d" % pid)

def parse_env(path=None):
"parse iRODS iCommands environment files"

if path is None:
path = env_file3(os.getpid())
if not os.path.isfile(path):
path = env_file3(os.getppid())

envre = re.compile("^\s*(?P<name>\w+)\s*(=(?P<value1>.*)|\s+[\'\"](?P<value2>.*)[\'\"]|(?P<value3>[^\'\"].*))\s*$")

ret = {}
Expand All @@ -63,25 +73,33 @@ def parse_env(path):

return ret

def env_file4(pid):
repo = os.path.expanduser(os.path.join("~", ".irods"))
return os.path.join(repo, "irods_environment.json.%d" % pid)

def parse_env4(path=None):
if path is None:
path = env_file4(os.getpid())
if not os.path.isfile(path):
path = env_file4(os.getppid())

return json.load(open(path, 'r'))

def guess_icwd():
"guess iCommand working directory"
pid = os.getpid()
ppid = os.getppid()

repo = os.path.expanduser(os.path.join("~", ".irods"))

icwd = None
try:
icwd = parse_env(os.path.join(repo, ".irodsEnv.%d" % pid))["irodsCwd"]
except:
#try iRODS v3 environment
icwd = parse_env()["irodsCwd"]
except Exception as e:
try:
icwd = parse_env(os.path.join(repo, ".irodsEnv.%d" % ppid))["irodsCwd"]
except:
pass

if not icwd:
ipwd = IrodsCommand("ipwd", output_filter = string.strip)
_retcode, icwd = ipwd()
# try iRODS v4 environment
icwd = parse_env4()['irods_cwd']
except Exception as e:
# fallback to icommand
ipwd = IrodsCommand("ipwd", output_filter = string.strip)
_retcode, icwd = ipwd()

return icwd

Expand Down
5 changes: 3 additions & 2 deletions scripts/ilsw
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ if __name__ == "__main__":
import sys

icwd = guess_icwd()

args = iargw( sys.argv[1:] )

# because we spawn ils in a subprocess, we need to expand relative paths here,
Expand All @@ -20,7 +19,9 @@ if __name__ == "__main__":
arg = os.path.normpath( icwd + '/' + arg )
newargs.append( arg )

retcode, output = ils( newargs )
newargs = newargs or [icwd]

retcode, output = ils(newargs)

print output.rstrip()

Expand Down

0 comments on commit f6571e2

Please sign in to comment.