cros: convert to pathlib

Change-Id: I696be34716f31cb4ca9fc39cc2c8a3f51890f92b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4983214
Reviewed-by: Tim Bain <tbain@google.com>
Commit-Queue: Tim Bain <tbain@google.com>
Auto-Submit: Mike Frysinger <vapier@chromium.org>
changes/14/4983214/2
Mike Frysinger 2 years ago committed by LUCI CQ
parent 7751ddfa9e
commit fa9968dcd4

23
cros

@ -12,24 +12,25 @@ It is intended to used strictly outside of the chroot.
""" """
import os import os
import pathlib from pathlib import Path
import subprocess import subprocess
import sys import sys
from typing import Optional
# Min version of Python that we *want*. We warn for older versions. # Min version of Python that we *want*. We warn for older versions.
MIN_PYTHON_VER_SOFT = (3, 8) MIN_PYTHON_VER_SOFT = (3, 8)
# Min version of Python that we *require*. We abort for older versions. # Min version of Python that we *require*. We abort for older versions.
MIN_PYTHON_VER_HARD = (3, 8) MIN_PYTHON_VER_HARD = (3, 8)
DEPOT_TOOLS_DIR = pathlib.Path(__file__).resolve().parent DEPOT_TOOLS_DIR = Path(__file__).resolve().parent
# Directory where cros-specific CIPD packages are installed. # Directory where cros-specific CIPD packages are installed.
CIPD_CACHE_DIR = DEPOT_TOOLS_DIR / '.cipd_bin_cros_python2' CIPD_CACHE_DIR = DEPOT_TOOLS_DIR / '.cipd_bin_cros_python2'
def _FindChromite(path): def _FindChromite(path: Path) -> Optional[Path]:
"""Find the chromite dir in a repo, gclient, or submodule checkout.""" """Find the chromite dir in a repo, gclient, or submodule checkout."""
path = os.path.abspath(path) path = path.resolve()
# Depending on the checkout type (whether repo chromeos or gclient chrome) # Depending on the checkout type (whether repo chromeos or gclient chrome)
# Chromite lives in a different location. # Chromite lives in a different location.
roots = ( roots = (
@ -45,13 +46,13 @@ def _FindChromite(path):
('../.citc', 'third_party/chromite/__init__.py'), ('../.citc', 'third_party/chromite/__init__.py'),
) )
while path != '/': while path != Path("/"):
for root, chromite_git_dir in roots: for root, chromite_git_dir in roots:
if all( if all(
os.path.exists(os.path.join(path, x)) (path / x).exists()
for x in [root, chromite_git_dir]): for x in [root, chromite_git_dir]):
return os.path.dirname(os.path.join(path, chromite_git_dir)) return (path / chromite_git_dir).parent
path = os.path.dirname(path) path = path.parent
return None return None
@ -99,15 +100,15 @@ def _BootstrapVpython27():
def main(): def main():
_CheckPythonVersion() _CheckPythonVersion()
chromite_dir = _FindChromite(os.getcwd()) chromite_dir = _FindChromite(Path.cwd())
target = os.path.basename(sys.argv[0]) target = os.path.basename(sys.argv[0])
if chromite_dir is None: if chromite_dir is None:
return _MissingErrorOut(target) return _MissingErrorOut(target)
path = os.path.join(chromite_dir, 'bin', target) path = chromite_dir / "bin" / target
# Check to see if this is a script requiring vpython2.7. # Check to see if this is a script requiring vpython2.7.
with open(path, 'rb') as fp: with path.open("rb") as fp:
shebang = next(fp).strip() shebang = next(fp).strip()
interpreter = shebang.split()[-1] interpreter = shebang.split()[-1]
if interpreter in (b'python', b'python2', b'python2.7', b'vpython'): if interpreter in (b'python', b'python2', b'python2.7', b'vpython'):

Loading…
Cancel
Save