From 2b61f4f000c36756e12f213b2207dc44b8f57203 Mon Sep 17 00:00:00 2001
From: Peter Wu <peter@lekensteyn.nl>
Date: Wed, 21 Jan 2015 11:07:43 +0100
Subject: [PATCH] Fix running binwalk from build dir

This patch fixes some cases in running binwalk:

 - from build dir: src/scripts/binwalk
 - from repo dir: src/build/binwalk
 - from custom dir, but without full path: ./binwalk
   (`dirname(dirname('./binwalk'))` becomes an empty string which is
   treated as "current working directory" while the expected output is
   "..")
---
 src/scripts/binwalk | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/scripts/binwalk b/src/scripts/binwalk
index 4d298a4..1fe1a4a 100755
--- a/src/scripts/binwalk
+++ b/src/scripts/binwalk
@@ -7,12 +7,20 @@ from threading import Thread
 # If installed to a custom prefix directory, binwalk may not be in
 # the default module search path(s). Try to resolve the prefix module
 # path and make it the first entry in sys.path.
-_module_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
-                            "lib",
-                            "python%d.%d" % (sys.version_info[0], sys.version_info[1]),
-                            "site-packages")
-if os.path.exists(_module_path) and _module_path not in sys.path:
-    sys.path = [_module_path] + sys.path
+# Ensure that 'src/binwalk' becomes '.' instead of an empty string
+_parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+for _module_path in [
+    # from repo: src/scripts/ -> src/
+    _parent_dir,
+    # from build dir: build/scripts-3.4/ -> build/lib/
+    os.path.join(_parent_dir, "lib"),
+    # installed in non-default path: bin/ -> lib/python3.4/site-packages/
+    os.path.join(_parent_dir, "lib",
+                 "python%d.%d" % (sys.version_info[0], sys.version_info[1]),
+                 "site-packages")
+]:
+    if os.path.exists(_module_path) and _module_path not in sys.path:
+        sys.path = [_module_path] + sys.path
 
 import binwalk
 import binwalk.modules
--
libgit2 0.26.0