cros 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python3
  2. # Copyright 2011 The ChromiumOS Authors
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Wrapper for chromite tools.
  6. The script is intend to be symlinked to any number of chromite tools, attempts
  7. to find the path for chromite, and hands off to the right tool via exec if
  8. possible.
  9. It is intended to used strictly outside of the chroot.
  10. """
  11. import enum
  12. import os
  13. from pathlib import Path
  14. import subprocess
  15. import sys
  16. from typing import List, NamedTuple, Optional, Tuple
  17. # Min version of Python that we *want*. We warn for older versions.
  18. MIN_PYTHON_VER_SOFT = (3, 11)
  19. # Min version of Python that we *require*. We abort for older versions.
  20. MIN_PYTHON_VER_HARD = (3, 8)
  21. DEPOT_TOOLS_DIR = Path(__file__).resolve().parent
  22. # Directory where cros-specific CIPD packages are installed.
  23. CIPD_CACHE_DIR = DEPOT_TOOLS_DIR / '.cipd_bin_cros_python2'
  24. class Checkout(NamedTuple):
  25. """Some details about this checkout."""
  26. root: Path
  27. chromite_dir: Path
  28. def _FindChromite(path: Path) -> Optional[Checkout]:
  29. """Find the chromite dir in a repo, gclient, or submodule checkout."""
  30. path = path.resolve()
  31. # Depending on the checkout type (whether repo chromeos or gclient chrome)
  32. # Chromite lives in a different location.
  33. roots = (
  34. # CrOS checkout using normal manifest.
  35. ('.repo', 'chromite/.git'),
  36. # CrOS checkout using CitC.
  37. ('../.citc', 'chromite/__init__.py'),
  38. # Chromium checkout using gclient+DEPS.
  39. ('.gclient', 'src/third_party/chromite/.git'),
  40. # Chromium checkout using git submodules (submodules at root).
  41. ('.gitmodules', 'chromite/.git'),
  42. # Chromium checkout using git submodules.
  43. ('src/.gitmodules', 'src/third_party/chromite/.git'),
  44. # Chromium checkout using CitC.
  45. ('../.citc', 'third_party/chromite/__init__.py'),
  46. )
  47. while path != Path("/"):
  48. for root, chromite_git_dir in roots:
  49. if all((path / x).exists() for x in [root, chromite_git_dir]):
  50. return Checkout(path, (path / chromite_git_dir).parent)
  51. path = path.parent
  52. return None
  53. def _MissingErrorOut(target):
  54. sys.stderr.write("""ERROR: Couldn't find the chromite tool %s.
  55. Please change to a directory inside your ChromiumOS source tree
  56. and retry. If you need to setup a ChromiumOS source tree, see
  57. https://chromium.googlesource.com/chromiumos/docs/+/HEAD/developer_guide.md
  58. """ % target)
  59. return 127
  60. def _CheckPythonVersion():
  61. """Verify active Python is new enough."""
  62. if sys.version_info >= MIN_PYTHON_VER_SOFT:
  63. return
  64. progname = os.path.basename(sys.argv[0])
  65. print('%s: ChromiumOS requires Python-%s+, but "%s" is "%s"' %
  66. (progname, '.'.join(str(x) for x in MIN_PYTHON_VER_SOFT),
  67. sys.executable, sys.version.replace('\n', ' ')),
  68. file=sys.stderr)
  69. if sys.version_info < MIN_PYTHON_VER_HARD:
  70. print('%s: fatal: giving up since Python is too old.' % (progname, ),
  71. file=sys.stderr)
  72. sys.exit(1)
  73. print(
  74. 'warning: temporarily continuing anyways; you must upgrade soon to '
  75. 'maintain support.',
  76. file=sys.stderr)
  77. def _BootstrapVpython27():
  78. """Installs the vpython2.7 packages into the cipd cache directory."""
  79. subprocess.run([
  80. 'cipd', 'ensure', '-log-level', 'info', '-ensure-file',
  81. DEPOT_TOOLS_DIR / 'cipd_manifest_cros_python2.txt', '-root',
  82. CIPD_CACHE_DIR
  83. ],
  84. check=True)
  85. def main():
  86. _CheckPythonVersion()
  87. result = _FindChromite(Path.cwd())
  88. target = os.path.basename(sys.argv[0])
  89. if result is None:
  90. return _MissingErrorOut(target)
  91. root, chromite_dir = result
  92. is_citc = (root.parent / ".citc").is_dir()
  93. if is_citc:
  94. # citc workspaces don't like dirty files like pyc.
  95. os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
  96. path = chromite_dir / "bin" / target
  97. # Check to see if this is a script requiring vpython2.7.
  98. with path.open("rb") as fp:
  99. shebang = next(fp).strip()
  100. interpreter = shebang.split()[-1]
  101. if interpreter in (b'python', b'python2', b'python2.7', b'vpython'):
  102. _BootstrapVpython27()
  103. vpython = CIPD_CACHE_DIR / 'vpython'
  104. args = [vpython]
  105. if interpreter != b'vpython':
  106. args.extend(
  107. ['-vpython-spec', DEPOT_TOOLS_DIR / 'cros_python2.vpython'])
  108. args.append(path)
  109. path = vpython
  110. else:
  111. args = [path]
  112. os.execv(path, args + sys.argv[1:])
  113. if __name__ == '__main__':
  114. sys.exit(main())