In any source-tree copy of a jailhouse script using pyjailhouse, modify
sys.path such that pyjailhouse is imported from the source tree, as opposed to
importing it from pip's installation location

Remove this sys.path modification during installation from each such script, so
that installed scripts import pyjailhouse from where pip installs it

Add explanation for import method in pyjailhouse.md

Signed-off-by: Chris Goldsworthy <[email protected]>
---
 Documentation/pyjailhouse.md | 61 ++++++++++++++++++++++++++++++++++++++++++++
 tools/Makefile               |  5 ++++
 tools/jailhouse-cell-linux   |  3 +++
 3 files changed, 69 insertions(+)
 create mode 100644 Documentation/pyjailhouse.md

diff --git a/Documentation/pyjailhouse.md b/Documentation/pyjailhouse.md
new file mode 100644
index 0000000..7cf15b6
--- /dev/null
+++ b/Documentation/pyjailhouse.md
@@ -0,0 +1,61 @@
+# pyjailhouse
+
+## Using source-tree version of pyjailhouse
+
+Python scripts in the **source tree** (i.e. as opposed to python scripts in an
+installation directory) that import pyjailhouse automatically import
+pyjailhouse from the jailhouse root directory. This is achieved by setting
+`sys.path [0]` to be the absolute path of the root directory, just before
+we import pyjailhouse (or something from it):
+
+`sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "[rel. path]"`
+`from pyjailhouse.cell import JailhouseCell`
+
+Where, `[rel.path]` is the relative path from the directory containing the
+running script to the root directory. When we install any python script that
+uses pyjailhouse, we remove `sys.path[0] = os.path.dirname(...` from the
+installed scripts, leaving python to import pyjailhouse from where pip
+installed it.
+
+As a usage example, consider the following directory structure for jailhouse:
+
+`|jailhouse/`
+`|_____> foo/`
+`|___________> bar/`
+`|_______________> __init__.py`
+`|_______________> baz.py`
+`|___________> boz.py`
+`|_____> moo.py`
+`|_____> pyjailhouse/`
+
+baz.py would have:
+`sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/../.."`
+`import pyjailhouse`
+
+boz.py would have:
+`sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/.."`
+`import pyjailhouse.something`
+
+moo.py would have:
+`sys.path[0] = os.path.dirname(os.path.abspath(__file__))`
+`from pyjailhouse import other`
+
+Note that any attempt to import a module after writing to sys.path[0], such
+that the module is located in the directory containing the script that
+initially invoked the python interpreter (with a shebang), will fail.  Read
+about sys.path[0] in any python language reference to understand why this is 
so.
+For the example, the following wouldn't work inside of boz.py:
+
+`sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/.."`
+`import pyjailhouse.something`
+`import bar # Will fail`
+
+**Boilerplate code, with sys.path[0] warning**
+
+`
+sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + ""
+from pyjailhouse import
+# Imports from directory containing this script won't work after the above
+# import statement, see python documentation on sys.path[0]
+`
+
diff --git a/tools/Makefile b/tools/Makefile
index 6bab351..d9ffdca 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -53,6 +53,10 @@ define patch_dirvar
        sed -i 's|^$1 = None|$1 = "$($1)"|' $2
 endef
 
+define patch_pyjh_import
+       sed -i 's|sys.path\[0\] = os\.path\.dirname.*||' $1
+endef
+
 quiet_cmd_gen_collect = GEN     $@
 define cmd_gen_collect
        $< -g $@; \
@@ -85,6 +89,7 @@ install-libexec: $(HELPERS) $(DESTDIR)$(libexecdir)/jailhouse
        $(INSTALL_PROGRAM) $^
        $(Q)$(call patch_dirvar,libexecdir,$(lastword $^)/jailhouse-cell-linux)
        $(Q)$(call patch_dirvar,datadir,$(lastword $^)/jailhouse-config-create)
+       $(Q)$(call patch_pyjh_import,$(lastword $^)/jailhouse-cell-linux)
 
 install-data: $(TEMPLATES) $(DESTDIR)$(datadir)/jailhouse
        $(INSTALL_DATA) $^
diff --git a/tools/jailhouse-cell-linux b/tools/jailhouse-cell-linux
index 23a3ab5..2fc8b6a 100755
--- a/tools/jailhouse-cell-linux
+++ b/tools/jailhouse-cell-linux
@@ -17,7 +17,10 @@ import os
 import struct
 import sys
 
+sys.path[0] = os.path.dirname(os.path.abspath(__file__)) + "/.."
 from pyjailhouse.cell import JailhouseCell
+# Imports from directory containing this script won't work after the above
+# import statement, see python documentation on sys.path[0]
 
 libexecdir = None
 
-- 
2.7.4

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to