cros 2.66 KB
Newer Older
1
#!/usr/bin/env python3
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Wrapper for chromite tools.

The script is intend to be symlinked to any number of chromite tools, attempts
to find the path for chromite, and hands off to the right tool via exec if
possible.

It is intended to used strictly outside of the chroot.
"""

import os
import sys


19 20 21 22 23 24 25
# Min version of Python that we *want*.  We warn for older versions.
MIN_PYTHON_VER_SOFT = (3, 6)
# Min version of Python that we *require*.  We abort for older versions.
# TODO(vapier): Hard require Python 3.6 by end of 2020.
MIN_PYTHON_VER_HARD = (3, 4)


26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
def _FindChromite(path):
  """Find the chromite dir in a repo, gclient, or submodule checkout."""
  path = os.path.abspath(path)
  # Depending on the checkout type (whether repo chromeos or gclient chrome)
  # Chromite lives in a different location.
  roots = (
      ('.repo', 'chromite/.git'),
      ('.gclient', 'src/third_party/chromite/.git'),
      ('src/.gitmodules', 'src/third_party/chromite/.git'),
  )

  while path != '/':
    for root, chromite_git_dir in roots:
      if all(os.path.exists(os.path.join(path, x))
             for x in [root, chromite_git_dir]):
        return os.path.dirname(os.path.join(path, chromite_git_dir))
    path = os.path.dirname(path)
  return None


def _MissingErrorOut(target):
  sys.stderr.write("""ERROR: Couldn't find the chromite tool %s.

Please change to a directory inside your Chromium OS source tree
and retry.  If you need to setup a Chromium OS source tree, see
  https://chromium.googlesource.com/chromiumos/docs/+/HEAD/developer_guide.md
""" % target)
  return 127


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
def _CheckPythonVersion():
  """Verify active Python is new enough."""
  if sys.version_info >= MIN_PYTHON_VER_SOFT:
    return

  progname = os.path.basename(sys.argv[0])
  print('%s: Chrome OS requires Python-%s+, but found "%s"' %
        (progname, '.'.join(str(x) for x in MIN_PYTHON_VER_SOFT),
         sys.version.replace('\n', ' ')), file=sys.stderr)
  if sys.version_info < MIN_PYTHON_VER_HARD:
    print('%s: fatal: giving up since Python is too old.' % (progname,),
          file=sys.stderr)
    sys.exit(1)

  print('warning: temporarily continuing anyways; you must upgrade soon to '
        'maintain support.', file=sys.stderr)


74
def main():
75 76
  _CheckPythonVersion()

77 78 79 80 81 82 83 84 85 86 87
  chromite_dir = _FindChromite(os.getcwd())
  target = os.path.basename(sys.argv[0])
  if chromite_dir is None:
    return _MissingErrorOut(target)

  path = os.path.join(chromite_dir, 'bin', target)
  os.execv(path, [path] + sys.argv[1:])


if __name__ == '__main__':
  sys.exit(main())