1#!/usr/bin/env python3
2# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Wrapper for chromite tools.
7
8The script is intend to be symlinked to any number of chromite tools, attempts
9to find the path for chromite, and hands off to the right tool via exec if
10possible.
11
12It is intended to used strictly outside of the chroot.
13"""
14
15import os
16import sys
17
18
19# Min version of Python that we *want*.  We warn for older versions.
20MIN_PYTHON_VER_SOFT = (3, 6)
21# Min version of Python that we *require*.  We abort for older versions.
22MIN_PYTHON_VER_HARD = (3, 6)
23
24
25def _FindChromite(path):
26  """Find the chromite dir in a repo, gclient, or submodule checkout."""
27  path = os.path.abspath(path)
28  # Depending on the checkout type (whether repo chromeos or gclient chrome)
29  # Chromite lives in a different location.
30  roots = (
31      ('.repo', 'chromite/.git'),
32      ('.gclient', 'src/third_party/chromite/.git'),
33      ('src/.gitmodules', 'src/third_party/chromite/.git'),
34  )
35
36  while path != '/':
37    for root, chromite_git_dir in roots:
38      if all(os.path.exists(os.path.join(path, x))
39             for x in [root, chromite_git_dir]):
40        return os.path.dirname(os.path.join(path, chromite_git_dir))
41    path = os.path.dirname(path)
42  return None
43
44
45def _MissingErrorOut(target):
46  sys.stderr.write("""ERROR: Couldn't find the chromite tool %s.
47
48Please change to a directory inside your Chromium OS source tree
49and retry.  If you need to setup a Chromium OS source tree, see
50  https://chromium.googlesource.com/chromiumos/docs/+/HEAD/developer_guide.md
51""" % target)
52  return 127
53
54
55def _CheckPythonVersion():
56  """Verify active Python is new enough."""
57  if sys.version_info >= MIN_PYTHON_VER_SOFT:
58    return
59
60  progname = os.path.basename(sys.argv[0])
61  print('%s: Chrome OS requires Python-%s+, but "%s" is "%s"' %
62        (progname, '.'.join(str(x) for x in MIN_PYTHON_VER_SOFT),
63         sys.executable, sys.version.replace('\n', ' ')),
64        file=sys.stderr)
65  if sys.version_info < MIN_PYTHON_VER_HARD:
66    print('%s: fatal: giving up since Python is too old.' % (progname,),
67          file=sys.stderr)
68    sys.exit(1)
69
70  print('warning: temporarily continuing anyways; you must upgrade soon to '
71        'maintain support.', file=sys.stderr)
72
73
74def main():
75  _CheckPythonVersion()
76
77  chromite_dir = _FindChromite(os.getcwd())
78  target = os.path.basename(sys.argv[0])
79  if chromite_dir is None:
80    return _MissingErrorOut(target)
81
82  path = os.path.join(chromite_dir, 'bin', target)
83  os.execv(path, [path] + sys.argv[1:])
84
85
86if __name__ == '__main__':
87  sys.exit(main())
88