Anonymous avatar Anonymous committed 67630e4

Commit the new setupenv.sh written by Feth Arezki

Comments (0)

Files changed (1)

 #Why not erase PYTHONPATH ? Conservative option chosen.
 #PYTHONPATH=""
 
-echo "Set PYTHONPATH"
-export PYTHONPATH=`cat << EOF | /usr/bin/env python -
-from os import environ, getcwd
+if [ $0 == $BASH_SOURCE ] ; then
+	echo "Please source this file to set your shell variables, don't execute it"
+        echo "source $0"
+	exit 1
+fi
 
-subprojects=[
-"hachoir-core",
-"hachoir-parser",
-"hachoir-editor",
-"hachoir-metadata",
-"hachoir-regex",
-"hachoir-subfile"
-]
+MY_NAME="setupenv.sh"
 
-cur_dir = getcwd()
+SETUPENV_DIR=` \
+	echo ${BASH_SOURCE} |\
 
-try:
-  path_list = environ["PYTHONPATH"].split(':')
-  try:
-    #occurs when the path is set but empty
-    path_list.remove('')
-  except ValueError:
-    pass
-except KeyError:
-  # occurs when PYTHONPATH is not set at all
-  path_list = []
+	#sed expressions :
+	# 0 Changes non-absolute paths to absolute paths
+	# 1 Removes file part of the path to retrieve the directory
 
-for project in subprojects:
-  path_item = "%s/%s" % (cur_dir, project)
-  if path_item not in path_list:
-    path_list.append(path_item)
+	sed  \
+		-e "s,^\([^/].*\),${PWD}/${BASH_SOURCE},"  \
+		-e "s,${MY_NAME}$,,"` 
 
-#Here, the bash variable is set from Python s stdout
-print ":".join(path_list)
+eval `cat << EOF | /usr/bin/env python - ${SETUPENV_DIR} 
+"""
+This script fetches PATH and PYTHONPATH. It issues some echo commands to
+tell what's happening, and if needed, extends PATH and PYTHONPATH (also with 
+"export PATH=..." statements)
+"""
+from os import environ
+from os import getcwd
+from os.path import join
+from os.path import realpath
+from sys import argv
+
+#can't guess file, we are stdin
+cur_dir = realpath(argv[1])
+
+subprojects = """
+hachoir-core
+hachoir-editor
+hachoir-metadata
+hachoir-parser
+hachoir-qt
+hachoir-regex
+hachoir-subfile
+"""
+
+exe_dirs = """
+hachoir-urwid
+hachoir-metadata
+hachoir-qt
+"""
+
+def _realpaths(partials):
+  for partial in partials.split():
+    yield(join(cur_dir, partial))
+
+def _get_pathlist(name):
+  pathlist = environ.get(name, '').split(':')
+  while '' in pathlist:
+    #happens around solitary ':'
+    pathlist.remove('')
+  return [realpath(item) for item in pathlist]
+
+def _issuecmd(cmd):
+  print cmd, ';'
+
+def echo(msg):
+  _issuecmd('echo %s' % msg)
+
+def setvariable(variable, value):
+  _issuecmd('export %s=%s' % (variable, value))
+
+def newpath(needed_dirs, varname):
+  """
+  main function
+  """
+  orig_path = _get_pathlist(varname)
+  unchanged = True
+  for needed_dir in _realpaths(needed_dirs):
+    if needed_dir not in orig_path:
+      echo("%s: adding %s" % (varname, needed_dir))
+      unchanged = False
+      orig_path.append(needed_dir)
+  if unchanged:
+    echo("%s unchanged" % varname)
+  else:
+    value = ":".join(orig_path)
+    setvariable(varname, value)
+    echo("Set %s to %s" % (varname, value))
+
+echo("Setting up env around %s" % cur_dir)
+newpath(exe_dirs, 'PATH')
+newpath(subprojects, 'PYTHONPATH')
 
 EOF
 `
 
-echo "Set PATH"
-export PATH=$PWD/hachoir-urwid:$PWD/hachoir-metadata:$PATH
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.