diff --git a/chromite b/chromite index 4e9e49fc5..48b09070c 100755 --- a/chromite +++ b/chromite @@ -20,58 +20,55 @@ here: http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite Since this script is _copied_, it should remain small and not use internal libs. + """ # Python imports. import os import sys -if __name__ == '__main__': - # Look relative to CROS_WORKON_SRCROOT if that variable exists. This is - # the "inside the chroot" case. - if 'CROS_WORKON_SRCROOT' in os.environ: - chromite_path = os.path.join(os.environ['CROS_WORKON_SRCROOT'], - 'chromite', 'shell', 'main.py') - if os.path.isfile(chromite_path): - # Exec the script, which will never return. - os.execv(chromite_path, sys.argv) - else: - print ( - "ERROR: Couldn't find the chromite tool.\n" - "\n" - "You may need to update your chroot. If you need help, see:\n" - " http://www.chromium.org/chromium-os/developer-guide" - ) - sys.exit(1) +def Search(path): + """Return an iterator of lists of places to look for chromite.""" - # Outside the chroot, search upward until the "parent" dir doesn't change - # (on Linux, that means we're at '/'). That is an error case. - dir = os.getcwd() - prev_dir = None - while dir != prev_dir: - chromite_path = os.path.join(dir, 'chromite', 'shell', 'main.py') - if os.path.isfile(chromite_path): - # Add the directory above chromite to PYTHONPATH before executing, so - # that "import chromite.abc.xyz" works... - env = os.environ.copy() - if 'PYTHONPATH' in env: - env['PYTHONPATH'] += ':%s' % dir - else: - env['PYTHONPATH'] = dir + if os.path.exists('/etc/debian_chroot'): + # We're in the chroot. Chromite should be in the python path inside the + # chroot, so we don't do any searching. NOTE that we purposely don't want + # CROS_WORKON_SRCROOT in the python path. + yield [] + else: + # Look in $CROS_WORKON_SRCROOT first. The idea is that a user would set + # this manually if they wanted to specify a particular version of chromite. + if 'CROS_WORKON_SRCROOT' in os.environ: + yield [os.environ['CROS_WORKON_SRCROOT']] - # Exec the script, which will never return. - os.execve(chromite_path, sys.argv, env) + # Search upward until we either end up with a blank dir or the "parent" dir + # doesn't change. + prev_path = None + while path and path != prev_path: + yield [path] + path, prev_path = os.path.dirname(path), path - prev_dir = dir - dir = os.path.dirname(dir) +for path in Search(os.getcwd()): + sys.path = path + sys.path + try: + import chromite.shell.main + break + except ImportError, e: + # We've got different modules named chromite in the tree, pulling in the + # wrong one will break the right one. So unload it. + if 'chromite' in sys.modules: + del sys.modules['chromite'] + sys.path = sys.path[len(path):] +else: # TODO(dianders): Should we actually print out the 'repo init' call that # the user should use? - print ( + sys.stderr.write( "ERROR: Couldn't find the chromite tool.\n" "\n" "Please change to a directory inside your Chromium OS source tree\n" "and retry. If you need to setup a Chromium OS source tree, see:\n" - " http://www.chromium.org/chromium-os/developer-guide" - ) + " http://www.chromium.org/chromium-os/developer-guide\n") sys.exit(1) + +chromite.shell.main.main()