cargo_refactor.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2021 The ChromiumOS Authors
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # Refactoring tools for moving around crates and updating dependencies
  5. # in toml files.
  6. #
  7. # Contains the last run refactoring for reference. Don't run this script, it'll
  8. # fail, but use it as a foundation for other refactorings.
  9. from contextlib import contextmanager
  10. from pathlib import Path
  11. import os
  12. import re
  13. import shutil
  14. import subprocess
  15. from typing import Callable, List, Tuple, Union
  16. SearchPattern = Union[str, re.Pattern[str]]
  17. Replacement = Union[str, Callable[[re.Match[str]], str]]
  18. def append_to_file(file_path: Path, appendix: str):
  19. contents = file_path.read_text()
  20. file_path.write_text(contents.rstrip() + "\n" + appendix + "\n")
  21. def replace_in_file(file_path: Path, search: SearchPattern, replace: Replacement):
  22. if not file_path.exists():
  23. print(f"WARNING: Does not exist {file_path}")
  24. return
  25. if isinstance(search, str):
  26. search = re.escape(search)
  27. contents = file_path.read_text()
  28. (contents, count) = re.subn(search, replace, contents)
  29. if count > 0:
  30. print(f"replacing '{search}' with '{replace}' in {file_path}")
  31. file_path.write_text(contents)
  32. def replace_in_files(glob: str, replacements: List[Tuple[SearchPattern, Replacement]]):
  33. for file in Path().glob(glob):
  34. for search, replace in replacements:
  35. replace_in_file(file, search, replace)
  36. def replace_path_in_all_cargo_toml(old_path: Path, new_path: Path):
  37. "Replace path in all cargo.toml files, accounting for relative paths."
  38. for toml in Path().glob("**/Cargo.toml"):
  39. crate_dir = toml.parent
  40. old_rel = os.path.relpath(old_path, crate_dir)
  41. new_rel = os.path.relpath(new_path, crate_dir)
  42. replace_in_file(toml, re.escape(f'path = "{old_rel}"'), f'path = "{new_rel}"')
  43. def update_path_deps(toml: Path, from_path: Path, to_path: Path):
  44. "Update path deps in toml file after moving it"
  45. contents = toml.read_text()
  46. for old_dep in re.findall('{ path = "([^"]+)"', contents):
  47. new_dep = os.path.relpath((from_path / old_dep).resolve(), to_path)
  48. contents = contents.replace(f'path = "{old_dep}"', f'path = "{new_dep}"')
  49. toml.write_text(contents)
  50. def move_crate(from_path: Path, to_path: Path):
  51. "Move crate and update dependencies"
  52. print(f"{from_path} -> {to_path}")
  53. if to_path.exists():
  54. shutil.rmtree(to_path)
  55. shutil.copytree(str(from_path), str(to_path))
  56. update_path_deps(to_path / "Cargo.toml", from_path, to_path)
  57. replace_in_files("**/*/Cargo.toml", [(str(from_path), str(to_path))])
  58. replace_in_file(Path("Cargo.toml"), str(from_path), str(to_path))
  59. def update_workspace_members():
  60. members: list[str] = []
  61. members.append("members = [")
  62. for toml in sorted(Path().glob("*/Cargo.toml")):
  63. members.append(f' "{toml.parent}",')
  64. for toml in sorted(Path().glob("common/*/Cargo.toml")):
  65. members.append(f' "{toml.parent}",')
  66. members.append(' "third_party/vmm_vhost",')
  67. members.append("]")
  68. replace_in_file(Path("Cargo.toml"), re.compile(r"members = \[[^\]]+\]"), "\n".join(members))
  69. @contextmanager
  70. def chdir(path: Union[Path, str]):
  71. origin = Path().absolute()
  72. try:
  73. os.chdir(path)
  74. yield
  75. finally:
  76. os.chdir(origin)
  77. def copy_crate_src_to_module(source: str, destination: str):
  78. shutil.rmtree(destination, ignore_errors=True)
  79. shutil.copytree(source, destination)
  80. with chdir(destination):
  81. Path("lib.rs").rename("mod.rs")
  82. IMPORT = """pub mod linux;
  83. #[cfg(windows)]
  84. pub mod windows;
  85. """
  86. BUILD_RS = """\
  87. // Copyright 2022 The ChromiumOS Authors
  88. // Use of this source code is governed by a BSD-style license that can be
  89. // found in the LICENSE file.
  90. fn main() {
  91. cc::Build::new()
  92. .file("src/windows/stdio_fileno.c")
  93. .compile("stdio_fileno");
  94. }
  95. """
  96. def main():
  97. os.chdir(Path(__file__).parent.parent.parent)
  98. update_workspace_members()
  99. main()