#!/bin/env python
# Remaps path(s) from /import to /local path
import sys, time, string, re, os, argparse, subprocess, shlex

op = argparse.ArgumentParser(description="""
Remaps path(s) from /import to /local path
Useful to remap LM_LICENSE_FILE to local drive
syntax: setenv env-var `remap_path env-var`
""")

op.add_argument('-X',                 help='Show commands, but do not execute',action='store_true')
op.add_argument('cmdline',            help='Positional arguments',nargs='*')
opts = op.parse_args(sys.argv[1:])

def run_cmd(cmd,quiet=None,read=None,always=False):
  if not quiet:
    print cmd
  if not opts.X or always:
    if not read:
      return os.system(cmd)
    else:
      proc = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE)
      return proc.stdout.readlines()
  return 0

file = opts.cmdline[0]

def chase(link):
  file = link
  while True:
    file = re.sub(r'/import', '/local', file)
    if os.path.islink(file):
      path = os.readlink(file)
      if path[0] != '/':
        file = os.path.normpath(os.path.join(os.path.dirname(file), path))
      else:
        file = path
    else:
      return file

remapped_list = []
for link in file.split(':'):
  result = chase(link)
  if os.path.exists(result):
    #print '%s -> %s' % (link, result)
    remapped_list.append(result)
  else:
    print '%s -> %s does not exist' % (link, result)

print ':'.join(remapped_list)

465da87a0855dbbaae1b2918350d36baf2db0751
#!/bin/env python
# Remaps path(s) from /import to /local path
import sys, time, string, re, os, argparse, subprocess, shlex

op = argparse.ArgumentParser(description="""
Remaps path(s) from /import to /local path
Useful to remap LM_LICENSE_FILE to local drive
syntax: setenv env-var `remap_path env-var`
""")

op.add_argument('-X',                 help='Show commands, but do not execute',action='store_true')
op.add_argument('cmdline',            help='Positional arguments',nargs='*')
opts = op.parse_args(sys.argv[1:])

def run_cmd(cmd,quiet=None,read=None,always=False):
  if not quiet:
    print cmd
  if not opts.X or always:
    if not read:
      return os.system(cmd)
    else:
      proc = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE)
      return proc.stdout.readlines()
  return 0

file = opts.cmdline[0]

def chase(link):
  file = link
  while True:
    file = re.sub(r'/import', '/local', file)
    if os.path.islink(file):
      path = os.readlink(file)
      if path[0] != '/':
        file = os.path.normpath(os.path.join(os.path.dirname(file), path))
      else:
        file = path
    else:
      return file

remapped_list = []
for link in file.split(':'):
  result = chase(link)
  if os.path.exists(result):
    #print '%s -> %s' % (link, result)
    remapped_list.append(result)
  else:
    print '%s -> %s does not exist' % (link, result)

print ':'.join(remapped_list)
