George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2022 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 | """Populates the top-level Cargo.toml with all necessary workspace entries.""" |
| 6 | |
| 7 | import logging |
| 8 | import os |
| 9 | from pathlib import Path |
| 10 | import sys |
| 11 | from typing import List |
| 12 | |
| 13 | WORKSPACE_FILE_HEADER = """\ |
| 14 | # Copyright 2022 The ChromiumOS Authors. |
| 15 | # Use of this source code is governed by a BSD-style license that can be |
| 16 | # found in the LICENSE file. |
| 17 | # |
| 18 | # !! Autogenerated by `populate-workspace.py`; please don't edit. !! |
| 19 | |
| 20 | """ |
| 21 | |
| 22 | |
| 23 | def main(argv: List[str]): |
| 24 | logging.basicConfig( |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 25 | format=">> %(asctime)s: %(levelname)s: %(filename)s:%(lineno)d: " |
| 26 | "%(message)s", |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 27 | level=logging.INFO, |
| 28 | ) |
| 29 | |
| 30 | projects_dir = Path(__file__).resolve().parent |
| 31 | projects = [] |
| 32 | for dir_path, subdirs, files in os.walk(projects_dir): |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 33 | if "Cargo.toml" not in files: |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 34 | continue |
| 35 | |
| 36 | dir_path = Path(dir_path) |
| 37 | if dir_path == projects_dir: |
| 38 | continue |
| 39 | |
| 40 | projects.append(dir_path.relative_to(projects_dir)) |
| 41 | |
| 42 | # It's a waste to descend into src directories. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 43 | if "src" in subdirs: |
| 44 | del subdirs[subdirs.index("src")] |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 45 | else: |
| 46 | # ...Though if src/ doesn't exist, Cargo will get confused. |
| 47 | # Synthesize one with a nop lib.rs. |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 48 | src = dir_path / "src" |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 49 | src.mkdir() |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 50 | (src / "lib.rs").write_bytes(b"") |
| 51 | logging.info("Synthesized src/lib.rs for %s", dir_path) |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 52 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 53 | assert projects, f"No projects found under {projects_dir}" |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 54 | projects.sort() |
| 55 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 56 | logging.info("Identified %d projects", len(projects)) |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 57 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 58 | workspace_toml_file = projects_dir / "Cargo.toml" |
| 59 | with workspace_toml_file.open("w", encoding="utf-8") as f: |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 60 | f.write(WORKSPACE_FILE_HEADER) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 61 | # The `toml` crate writes this as a massive line, which is hard to |
| 62 | # read. Since this is simple to write, write it directly. |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 63 | # TODO(b/242668603): find a toml crate with prettier formatting |
| 64 | f.write('[workspace]\nmembers = [\n') |
| 65 | for project in projects: |
| 66 | project = str(project) |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 67 | assert '"' not in project and "\\" not in project, project |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 68 | f.write(f' "{project}",\n') |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 69 | f.write("]") |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 70 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 71 | logging.info("Workspace Cargo.toml successfully written.") |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 72 | |
| 73 | |
George Burgess IV | 7dffc25 | 2022-08-31 14:37:01 -0700 | [diff] [blame^] | 74 | if __name__ == "__main__": |
George Burgess IV | 18af563 | 2022-08-30 14:10:53 -0700 | [diff] [blame] | 75 | sys.exit(main(sys.argv[1:])) |