Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 2 | # Copyright 2020 The ChromiumOS Authors |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 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 around chromite executable scripts. |
| 7 | |
| 8 | This takes care of creating a consistent environment for chromite scripts |
| 9 | (like setting up import paths) so we don't have to duplicate the logic in |
| 10 | lots of places. |
| 11 | """ |
| 12 | |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 13 | import importlib |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 14 | import importlib.abc |
Mike Frysinger | 087fc4c | 2023-08-22 16:44:11 -0400 | [diff] [blame] | 15 | import importlib.machinery |
| 16 | import importlib.util |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | # Assert some minimum Python versions as we don't test or support any others. |
Jordan Abrahams-Whitehead | 7498dc3 | 2023-09-12 21:44:36 +0000 | [diff] [blame^] | 22 | # We only support Python 3.6+. |
| 23 | if sys.version_info < (3, 6): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | print( |
Jordan Abrahams-Whitehead | 7498dc3 | 2023-09-12 21:44:36 +0000 | [diff] [blame^] | 25 | '%s: chromite: error: Python-3.6+ is required, but "%s" is "%s"' |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | % (sys.argv[0], sys.executable, sys.version.replace("\n", " ")), |
| 27 | file=sys.stderr, |
| 28 | ) |
| 29 | sys.exit(1) |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 30 | |
| 31 | |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 32 | CHROMITE_PATH = os.path.dirname(os.path.realpath(__file__)) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | while not os.path.exists(os.path.join(CHROMITE_PATH, "PRESUBMIT.cfg")): |
| 34 | CHROMITE_PATH = os.path.dirname(CHROMITE_PATH) |
| 35 | assert str(CHROMITE_PATH) != "/", "Unable to locate chromite dir" |
| 36 | CHROMITE_PATH += "/" |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 37 | |
| 38 | |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 39 | # module_repr triggers an abstract warning, but it's deprecated in Python 3.+, |
| 40 | # so we don't want to bother implementing it. |
| 41 | # pylint: disable=abstract-method |
| 42 | class ChromiteLoader(importlib.abc.Loader): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | """Virtual chromite module |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | If the checkout is not named 'chromite', trying to do 'from chromite.xxx' |
| 46 | to import modules fails horribly. Instead, manually locate the chromite |
| 47 | directory (whatever it is named), load & return it whenever someone tries |
| 48 | to import it. This lets us use the stable name 'chromite' regardless of |
| 49 | how things are structured on disk. |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | This also lets us keep the sys.path search clean. Otherwise we'd have to |
| 52 | worry about what other dirs chromite were checked out near to as doing an |
| 53 | import would also search those for .py modules. |
| 54 | """ |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | def __init__(self): |
Alex Klein | 345222d | 2023-01-20 17:33:41 -0700 | [diff] [blame] | 57 | # When trying to load the chromite dir from disk, we'll get called |
| 58 | # again, so make sure to disable our logic to avoid an infinite loop. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | self.loading = False |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | # pylint: disable=unused-argument |
| 62 | def create_module(self, spec): |
| 63 | """Load the current dir.""" |
| 64 | if self.loading: |
| 65 | return None |
| 66 | path, mod = os.path.split(CHROMITE_PATH[:-1]) |
| 67 | sys.path.insert(0, path) |
| 68 | self.loading = True |
| 69 | try: |
| 70 | return importlib.import_module(mod) |
| 71 | finally: |
| 72 | # We can't pop by index as the import might have changed sys.path. |
| 73 | sys.path.remove(path) |
| 74 | self.loading = False |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | # pylint: disable=unused-argument |
| 77 | def exec_module(self, module): |
| 78 | """Required stub as a loader.""" |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 79 | |
| 80 | |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 81 | class ChromiteFinder(importlib.abc.MetaPathFinder): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | """Virtual chromite finder. |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | We'll route any requests for the 'chromite' module. |
| 85 | """ |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | def __init__(self, loader): |
| 88 | self._loader = loader |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | # pylint: disable=unused-argument |
| 91 | def find_spec(self, fullname, path=None, target=None): |
| 92 | if fullname != "chromite" or self._loader.loading: |
| 93 | return None |
| 94 | return importlib.machinery.ModuleSpec(fullname, self._loader) |
Mike Frysinger | d008d9c | 2021-05-13 15:36:53 -0400 | [diff] [blame] | 95 | |
| 96 | |
| 97 | sys.meta_path.insert(0, ChromiteFinder(ChromiteLoader())) |
| 98 | |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 99 | |
| 100 | # We have to put these imports after our meta-importer above. |
| 101 | # pylint: disable=wrong-import-position |
| 102 | from chromite.lib import commandline |
| 103 | |
| 104 | |
| 105 | def FindTarget(target): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | """Turn the path into something we can import from the chromite tree. |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | This supports a variety of ways of running chromite programs: |
| 109 | # Loaded via depot_tools in $PATH. |
| 110 | $ cros_sdk --help |
| 111 | # Loaded via .../chromite/bin in $PATH. |
| 112 | $ cros --help |
| 113 | # No $PATH needed. |
| 114 | $ ./bin/cros --help |
| 115 | # Loaded via ~/bin in $PATH to chromite bin/ subdir. |
| 116 | $ ln -s $PWD/bin/cros ~/bin; cros --help |
| 117 | # No $PATH needed. |
| 118 | $ ./cbuildbot/cbuildbot --help |
| 119 | # No $PATH needed, but symlink inside of chromite dir. |
| 120 | $ ln -s ./cbuildbot/cbuildbot; ./cbuildbot --help |
| 121 | # Loaded via ~/bin in $PATH to non-chromite bin/ subdir. |
| 122 | $ ln -s $PWD/cbuildbot/cbuildbot ~/bin/; cbuildbot --help |
| 123 | # No $PATH needed, but a relative symlink to a symlink to the chromite dir. |
| 124 | $ cd ~; ln -s bin/cbuildbot ./; ./cbuildbot --help |
| 125 | # External chromite module |
| 126 | $ ln -s ../chromite/scripts/wrapper.py foo; ./foo |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | Args: |
Alex Klein | 345222d | 2023-01-20 17:33:41 -0700 | [diff] [blame] | 129 | target: Path to the script we're trying to run. |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | Returns: |
Alex Klein | 345222d | 2023-01-20 17:33:41 -0700 | [diff] [blame] | 132 | The module main functor. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | """ |
| 134 | # We assume/require the script we're wrapping ends in a .py. |
| 135 | full_path = target + ".py" |
| 136 | while True: |
| 137 | # Walk back one symlink at a time until we get into the chromite dir. |
| 138 | parent, base = os.path.split(target) |
| 139 | parent = os.path.realpath(parent) |
| 140 | if parent.startswith(CHROMITE_PATH): |
| 141 | target = base |
| 142 | break |
| 143 | target = os.path.join(os.path.dirname(target), os.readlink(target)) |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | # If we walked all the way back to wrapper.py, it means we're trying to run |
| 146 | # an external module. So we have to import it by filepath and not via the |
| 147 | # chromite.xxx.yyy namespace. |
| 148 | if target != "wrapper3.py": |
| 149 | assert parent.startswith(CHROMITE_PATH), ( |
| 150 | "could not figure out leading path\n" |
| 151 | "\tparent: %s\n" |
| 152 | "\tCHROMITE_PATH: %s" % (parent, CHROMITE_PATH) |
| 153 | ) |
| 154 | parent = parent[len(CHROMITE_PATH) :].split(os.sep) |
| 155 | target = ["chromite"] + parent + [target.replace("-", "_")] |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | if target[1] == "bin": |
| 158 | # Convert chromite/bin/foo -> chromite/scripts/foo. |
| 159 | # Since chromite/bin/ is in $PATH, we want to keep it clean. |
| 160 | target[1] = "scripts" |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | try: |
| 163 | module = importlib.import_module(".".join(target)) |
| 164 | except ImportError as e: |
| 165 | print( |
| 166 | "%s: could not import chromite module: %s: %s" |
| 167 | % (sys.argv[0], full_path, e), |
| 168 | file=sys.stderr, |
| 169 | ) |
| 170 | raise |
| 171 | else: |
| 172 | import types |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | try: |
| 175 | loader = importlib.machinery.SourceFileLoader("main", full_path) |
| 176 | module = types.ModuleType(loader.name) |
| 177 | loader.exec_module(module) |
| 178 | except IOError as e: |
| 179 | print( |
| 180 | "%s: could not import external module: %s: %s" |
| 181 | % (sys.argv[0], full_path, e), |
| 182 | file=sys.stderr, |
| 183 | ) |
| 184 | raise |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | # Run the module's main func if it has one. |
| 187 | main = getattr(module, "main", None) |
| 188 | if main: |
| 189 | return main |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 190 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | # Is this a package? Import it like `python -m...` does. |
| 192 | if target != "wrapper3.py": |
| 193 | mod_name = ".".join(target + ["__main__"]) |
| 194 | try: |
| 195 | module = importlib.import_module(mod_name) |
| 196 | except ImportError: |
| 197 | module = None |
| 198 | if module: |
| 199 | spec = importlib.util.find_spec(mod_name) |
| 200 | loader = spec.loader |
| 201 | code = loader.get_code(mod_name) |
| 202 | # pylint: disable=exec-used |
| 203 | return lambda _argv: exec( |
| 204 | code, {**globals(), "__name__": "__main__"} |
| 205 | ) |
Mike Frysinger | 9848470 | 2021-02-12 14:59:28 -0500 | [diff] [blame] | 206 | |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 207 | |
| 208 | def DoMain(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 209 | commandline.ScriptWrapperMain(FindTarget) |
Mike Frysinger | 1c57dfe | 2020-02-06 00:28:22 -0500 | [diff] [blame] | 210 | |
| 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | if __name__ == "__main__": |
| 213 | DoMain() |