=== modified file 'linaro-media-create'
--- linaro-media-create	2011-02-02 12:08:05 +0000
+++ linaro-media-create	2011-02-02 17:32:10 +0000
@@ -121,7 +121,11 @@
     unpack_binary_tarball(args.binary, TMP_DIR)
 
     hwpacks = args.hwpacks
-    install_hwpacks(ROOTFS_DIR, TMP_DIR, args.hwpack_force_yes, *hwpacks)
+    lmc_dir = os.path.dirname(__file__)
+    if lmc_dir == '':
+        lmc_dir = None
+    install_hwpacks(
+        ROOTFS_DIR, TMP_DIR, lmc_dir, args.hwpack_force_yes, *hwpacks)
 
     boot_partition, root_partition = setup_partitions(
         board_config, media, args.image_size, args.boot_label, args.rfs_label,

=== modified file 'linaro_media_create/hwpack.py'
--- linaro_media_create/hwpack.py	2011-02-01 17:07:04 +0000
+++ linaro_media_create/hwpack.py	2011-02-02 17:32:10 +0000
@@ -21,7 +21,10 @@
 import sys
 
 from linaro_media_create import cmd_runner
-from linaro_media_create.utils import is_arm_host
+from linaro_media_create.utils import (
+    is_arm_host,
+    find_command,
+    )
 
 
 # It'd be nice if we could use atexit here, but all the things we need to undo
@@ -29,7 +32,8 @@
 # functions would only be called after l-m-c.py exits.
 local_atexit = []
 
-def install_hwpacks(chroot_dir, tmp_dir, hwpack_force_yes, *hwpack_files):
+def install_hwpacks(
+    chroot_dir, tmp_dir, tools_dir, hwpack_force_yes, *hwpack_files):
     """Install the given hwpacks onto the given chroot."""
 
     chroot_etc = os.path.join(chroot_dir, 'etc')
@@ -40,13 +44,11 @@
         copy_file('/usr/bin/qemu-arm-static',
                   os.path.join(chroot_dir, 'usr', 'bin'))
 
-    # FIXME: This is an ugly hack to make sure we use the l-h-i script from
-    # the current development tree when possible.
-    here = os.path.dirname(__file__)
-    linaro_hwpack_install_path = os.path.join(
-        here, '..', 'linaro-hwpack-install')
-    if not os.path.exists(linaro_hwpack_install_path):
-        linaro_hwpack_install_path = '/usr/bin/linaro-hwpack-install'
+    linaro_hwpack_install_path = find_command(
+        'linaro-hwpack-install', prefer_dir=tools_dir)
+    # FIXME: shouldn't use chroot/usr/bin as this might conflict with installed
+    # packages; would be best to use some custom directory like
+    # chroot/linaro-image-tools/bin
     copy_file(linaro_hwpack_install_path,
               os.path.join(chroot_dir, 'usr', 'bin'))
 

=== modified file 'linaro_media_create/tests/test_media_create.py'
--- linaro_media_create/tests/test_media_create.py	2011-02-01 17:29:25 +0000
+++ linaro_media_create/tests/test_media_create.py	2011-02-02 17:32:10 +0000
@@ -21,6 +21,7 @@
 import glob
 import os
 import random
+import stat
 import string
 import subprocess
 import sys
@@ -82,6 +83,7 @@
 from linaro_media_create.unpack_binary_tarball import unpack_binary_tarball
 from linaro_media_create.utils import (
     ensure_command,
+    find_command,
     install_package_providing,
     UnableToFindPackageProvidingCommand,
     )
@@ -121,6 +123,32 @@
             utils, 'install_package_providing', mock_func))
 
 
+class TestFindCommand(TestCaseWithFixtures):
+
+    def test_preferred_dir(self):
+        tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
+        lmc = 'linaro-media-create'
+        path = os.path.join(tempdir, lmc)
+        open(path, 'w').close()
+        os.chmod(path, stat.S_IXUSR)
+        self.assertEquals(path, find_command(lmc, tempdir))
+
+    def test_existing_command(self):
+        lmc = 'linaro-media-create'
+        # running from bzr checkout?
+        if os.path.isabs(__file__):
+            expected, _ = cmd_runner.run(
+                ['which', lmc, ],
+                stdout=subprocess.PIPE).communicate()
+            expected = expected.strip()
+        else:
+            expected = os.path.join(os.getcwd(), lmc)
+        self.assertEquals(expected, find_command(lmc))
+
+    def test_nonexisting_command(self):
+        self.assertEquals(find_command('linaro-moo'), None)
+
+
 class TestInstallPackageProviding(TestCaseWithFixtures):
 
     def test_found_package(self):
@@ -1083,8 +1111,17 @@
             sys, 'stdout', open('/dev/null', 'w')))
         fixture = self.useFixture(MockCmdRunnerPopenFixture())
         force_yes = True
+
+        prefer_dir = None
+        # running from bzr checkout?
+        if not os.path.isabs(__file__):
+            prefer_dir = os.getcwd()
+
         install_hwpacks(
-            'chroot', '/tmp/dir', force_yes, 'hwpack1.tgz', 'hwpack2.tgz')
+            'chroot', '/tmp/dir', prefer_dir, force_yes, 'hwpack1.tgz',
+            'hwpack2.tgz')
+        linaro_hwpack_install = find_command(
+            'linaro-hwpack-install', prefer_dir=prefer_dir)
         self.assertEquals(
             [['sudo', 'mv', '-f', 'chroot/etc/resolv.conf',
               '/tmp/dir/resolv.conf'],
@@ -1092,8 +1129,7 @@
              ['sudo', 'mv', '-f', 'chroot/etc/hosts', '/tmp/dir/hosts'],
              ['sudo', 'cp', '/etc/hosts', 'chroot/etc'],
              ['sudo', 'cp', '/usr/bin/qemu-arm-static', 'chroot/usr/bin'],
-             ['sudo', 'cp', 'linaro_media_create/../linaro-hwpack-install',
-              'chroot/usr/bin'],
+             ['sudo', 'cp', linaro_hwpack_install, 'chroot/usr/bin'],
              ['sudo', 'mount', 'proc', 'chroot/proc', '-t', 'proc'],
              ['sudo', 'cp', 'hwpack1.tgz', 'chroot'],
              ['sudo', 'chroot', 'chroot', 'linaro-hwpack-install',
@@ -1152,11 +1188,17 @@
             linaro_media_create.hwpack, 'run_local_atexit_funcs',
             mock_run_local_atexit_functions))
 
+        prefer_dir = None
+        # running from bzr checkout?
+        if not os.path.isabs(__file__):
+            prefer_dir = os.getcwd()
+
         force_yes = True
         exception_caught = False
         try:
             install_hwpacks(
-                'chroot', '/tmp/dir', force_yes, 'hwp.tgz', 'hwp2.tgz')
+                'chroot', '/tmp/dir', prefer_dir, force_yes, 'hwp.tgz',
+                'hwp2.tgz')
         except:
             exception_caught = True
         self.assertTrue(self.run_local_atexit_functions_called)

=== modified file 'linaro_media_create/utils.py'
--- linaro_media_create/utils.py	2011-01-28 19:50:48 +0000
+++ linaro_media_create/utils.py	2011-02-02 16:37:15 +0000
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Linaro Image Tools.  If not, see <http://www.gnu.org/licenses/>.
 
+import os
 import platform
 
 try:
@@ -61,6 +62,37 @@
     except cmd_runner.SubcommandNonZeroReturnValue:
         install_package_providing(command)
 
+def find_command(name, prefer_dir=None):
+    """Finds a linaro-image-tools command.
+
+    Prefers specified directory, otherwise searches only the current directory
+    when running from a checkout, or only PATH when running from an installed
+    version.
+    """
+    assert name != ""
+    assert os.path.dirname(name) == ""
+
+    if not os.environ.has_key("PATH"):
+        os.environ["PATH"] = ":/bin:usr/bin"
+
+    # default to searching in current directory when running from a bzr
+    # checkout
+    dirs = [os.getcwd(),]
+    if os.path.isabs(__file__):
+        dirs = os.environ["PATH"].split(os.pathsep)
+        # empty dir in PATH means current directory
+        dirs = map(lambda x: x == '' and '.' or x, dirs)
+
+    if prefer_dir is not None:
+        dirs.insert(0, prefer_dir)
+
+    for dir in dirs:
+        path = os.path.join(dir, name)
+        if os.path.exists(path) and os.access(path, os.X_OK):
+            return path
+
+    return None
+
 
 def is_arm_host():
     return platform.machine().startswith('arm')

