refactor_use_references.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2022 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. # Tools for refactoring references in rust code.
  5. #
  6. # Contains the last run refactoring for reference. Don't run this script, it'll
  7. # fail, but use it as a foundation for other refactorings.
  8. from contextlib import contextmanager
  9. import os
  10. import re
  11. import subprocess
  12. from pathlib import Path
  13. from typing import Callable, NamedTuple, Union
  14. SearchPattern = Union[str, re.Pattern[str]]
  15. class Token(NamedTuple):
  16. token: str
  17. start: int
  18. end: int
  19. def tokenize(source: str):
  20. "Split source by whitespace with start/end indices annotated."
  21. start = 0
  22. for i in range(len(source)):
  23. if source[i] in (" ", "\n", "\t") and i - start > 0:
  24. token = source[start:i].strip()
  25. if token:
  26. yield Token(token, start, i)
  27. start = i
  28. def parse_module_chunks(source: str):
  29. """Terrible parser to split code by `mod foo { ... }` statements. Please don't judge me.
  30. Returns the original source split with module names anntated as ('module name', 'source')
  31. """
  32. tokens = list(tokenize(source))
  33. prev = 0
  34. for i in range(len(tokens) - 2):
  35. if tokens[i].token == "mod" and tokens[i + 2].token == "{":
  36. brackets = 1
  37. for j in range(i + 3, len(tokens)):
  38. if "{" not in tokens[j].token or "}" not in tokens[j].token:
  39. if "{" in tokens[j].token:
  40. brackets += 1
  41. elif "}" in tokens[j].token:
  42. brackets -= 1
  43. if brackets == 0:
  44. start = tokens[i + 2].end
  45. end = tokens[j].start
  46. yield ("", source[prev:start])
  47. yield (tokens[i + 1].token, source[start:end])
  48. prev = end
  49. break
  50. if prev != len(source):
  51. yield ("", source[prev:])
  52. def replace_use_references(file_path: Path, callback: Callable[[list[str], str], str]):
  53. """Calls 'callback' for each foo::bar reference in `file_path`.
  54. The callback is called with the reference as an argument and is expected to return the rewritten
  55. reference.
  56. Additionally, the absolute path in the module tree is provided, taking into account the file
  57. path as well as modules defined in the source itself.
  58. eg.
  59. src/foo.rs:
  60. ```
  61. mod tests {
  62. use crate::baz;
  63. }
  64. ```
  65. will call `callback(['foo', 'tests'], 'crate::baz')`
  66. """
  67. module_parts = list(file_path.parts[:-1])
  68. if file_path.stem not in ("mod", "lib"):
  69. module_parts.append(file_path.stem)
  70. with open(file_path, "r") as file:
  71. contents = file.read()
  72. chunks: list[str] = []
  73. for module, source in parse_module_chunks(contents):
  74. if module:
  75. full_module_parts = module_parts + [module]
  76. else:
  77. full_module_parts = module_parts
  78. chunks.append(
  79. re.sub(
  80. r"([\w\*\_\$]+\:\:)+[\w\*\_]+",
  81. lambda m: callback(full_module_parts, m.group(0)),
  82. source,
  83. )
  84. )
  85. with open(file_path, "w") as file:
  86. file.write("".join(chunks))
  87. @contextmanager
  88. def chdir(path: Union[Path, str]):
  89. origin = Path().absolute()
  90. try:
  91. os.chdir(path)
  92. yield
  93. finally:
  94. os.chdir(origin)
  95. def use_super_instead_of_crate(root: Path):
  96. """Expects to be run directly on the src directory and assumes
  97. that directory to be the module crate:: refers to."""
  98. def replace(module: list[str], use: str):
  99. # Patch up weird module structure...
  100. if len(module) > 1 and module[0] == "win":
  101. # Only the listed modules are actually in win::.
  102. # The rest is in the top level.
  103. if module[1] not in (
  104. "file_traits",
  105. "syslog",
  106. "platform_timer_utils",
  107. "file_util",
  108. "shm",
  109. "wait",
  110. "mmap",
  111. "stream_channel",
  112. "timer",
  113. ):
  114. del module[0]
  115. if len(module) > 0 and module[0] in ("punch_hole", "write_zeros"):
  116. module = ["write_zeroes", module[0]]
  117. if use.startswith("crate::"):
  118. new_use = use.replace("crate::", "super::" * len(module))
  119. print("::".join(module), use, "->", new_use)
  120. return new_use
  121. return use
  122. with chdir(root):
  123. for file in Path().glob("**/*.rs"):
  124. replace_use_references(file, replace)
  125. def main():
  126. path = Path("common") / "win_sys_util/src"
  127. subprocess.check_call(["git", "checkout", "-f", str(path)])
  128. # Use rustfmt to re-format use statements to be one per line.
  129. subprocess.check_call(
  130. ["rustfmt", "+nightly", "--config=imports_granularity=item", f"{path}/lib.rs"]
  131. )
  132. use_super_instead_of_crate(path)
  133. subprocess.check_call(
  134. ["rustfmt", "+nightly", "--config=imports_granularity=crate", f"{path}/lib.rs"]
  135. )
  136. main()