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