diff --git a/mcia_irods_utils/icommand.py b/mcia_irods_utils/icommand.py index 6176948..6d58bb1 100644 --- a/mcia_irods_utils/icommand.py +++ b/mcia_irods_utils/icommand.py @@ -4,6 +4,7 @@ import string import re import subprocess +import json class IrodsCommand: "An iRODS iCommand wrapper" @@ -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\w+)\s*(=(?P.*)|\s+[\'\"](?P.*)[\'\"]|(?P[^\'\"].*))\s*$") ret = {} @@ -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 diff --git a/scripts/ilsw b/scripts/ilsw index 5f25534..1a97355 100644 --- a/scripts/ilsw +++ b/scripts/ilsw @@ -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, @@ -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()