mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 10:10:41 +00:00
Refactoring script to move common/base/ to base/
This is a prerequisite for sys_util* to move into the base crate. The crate should never have been in common in the first place since it is not shared. BUG=b:223206469 TEST=python tools/contrib/cargo_refactor.py Change-Id: I60115e0418c6980d7bf7ff624a3cc0a24e71a57d Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3530502 Reviewed-by: Anton Romanov <romanton@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Vikram Auradkar <auradkar@google.com>
This commit is contained in:
parent
f3ceb8e1b9
commit
fd0b4cb74a
2 changed files with 28 additions and 63 deletions
|
@ -13,32 +13,39 @@ import os
|
|||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from typing import List, Tuple, Union
|
||||
|
||||
SearchPattern = Union[str, re.Pattern[str]]
|
||||
|
||||
|
||||
def replace_in_file(file_path: Path, search: str, replace: str):
|
||||
def replace_in_file(file_path: Path, search: SearchPattern, replace: str):
|
||||
if not file_path.exists():
|
||||
print(f"WARNING: Does not exist {file_path}")
|
||||
return
|
||||
if isinstance(search, str):
|
||||
search = re.escape(search)
|
||||
with open(file_path, "r") as file:
|
||||
contents = file.read()
|
||||
(contents, count) = re.subn(search, replace, contents)
|
||||
if count > 0:
|
||||
print(file_path, search, replace)
|
||||
print(f"replacing '{search}' with '{replace}' in {file_path}")
|
||||
with open(file_path, "w") as file:
|
||||
file.write(contents)
|
||||
|
||||
|
||||
def replace_in_files(glob: str, replacements: List[Tuple[SearchPattern, str]]):
|
||||
for file in Path().glob(glob):
|
||||
for (search, replace) in replacements:
|
||||
replace_in_file(file, search, replace)
|
||||
|
||||
|
||||
def replace_path_in_all_cargo_toml(old_path: Path, new_path: Path):
|
||||
"Replace path in all cargo.toml files, accounting for relative paths."
|
||||
for toml in Path(".").glob("**/Cargo.toml"):
|
||||
for toml in Path().glob("**/Cargo.toml"):
|
||||
crate_dir = toml.parent
|
||||
old_rel = os.path.relpath(old_path, crate_dir)
|
||||
new_rel = os.path.relpath(new_path, crate_dir)
|
||||
replace_in_file(
|
||||
toml, re.escape(f'path = "{old_rel}"'), f'path = "{new_rel}"'
|
||||
)
|
||||
|
||||
|
||||
def replace_in_all_workspace_toml(search: str, replace: str):
|
||||
for toml in sorted(Path(".").glob("*/Cargo.toml")):
|
||||
replace_in_file(toml, search, replace)
|
||||
replace_in_file(toml, re.escape(f'path = "{old_rel}"'), f'path = "{new_rel}"')
|
||||
|
||||
|
||||
def update_path_deps(toml: Path, from_path: Path, to_path: Path):
|
||||
|
@ -47,9 +54,7 @@ def update_path_deps(toml: Path, from_path: Path, to_path: Path):
|
|||
contents = file.read()
|
||||
for old_dep in re.findall('{ path = "([^"]+)"', contents):
|
||||
new_dep = os.path.relpath((from_path / old_dep).resolve(), to_path)
|
||||
contents = contents.replace(
|
||||
f'path = "{old_dep}"', f'path = "{new_dep}"'
|
||||
)
|
||||
contents = contents.replace(f'path = "{old_dep}"', f'path = "{new_dep}"')
|
||||
with open(toml, "w") as file:
|
||||
file.write(contents)
|
||||
|
||||
|
@ -61,66 +66,32 @@ def move_crate(from_path: Path, to_path: Path):
|
|||
shutil.rmtree(to_path)
|
||||
subprocess.check_call(["git", "mv", str(from_path), str(to_path)])
|
||||
update_path_deps(to_path / "Cargo.toml", from_path, to_path)
|
||||
replace_path_in_all_cargo_toml(from_path, to_path)
|
||||
replace_in_files("**/*/Cargo.toml", [(str(from_path), str(to_path))])
|
||||
replace_in_file(Path("Cargo.toml"), str(from_path), str(to_path))
|
||||
|
||||
|
||||
def update_workspace_members():
|
||||
"To copy/paste into the main cargo.toml"
|
||||
members: list[str] = []
|
||||
members.append("members = [")
|
||||
for toml in sorted(Path(".").glob("*/Cargo.toml")):
|
||||
for toml in sorted(Path().glob("*/Cargo.toml")):
|
||||
members.append(f' "{toml.parent}",')
|
||||
members.append(' "third_party/vmm_vhost",')
|
||||
|
||||
members.append("]")
|
||||
replace_in_file(
|
||||
Path("Cargo.toml"), r"members = \[[^\]]+\]", "\n".join(members)
|
||||
)
|
||||
replace_in_file(Path("Cargo.toml"), re.compile(r"members = \[[^\]]+\]"), "\n".join(members))
|
||||
|
||||
exclude: list[str] = []
|
||||
exclude.append("exclude = [")
|
||||
for toml in sorted(Path(".").glob("common/*/Cargo.toml")):
|
||||
for toml in sorted(Path().glob("common/*/Cargo.toml")):
|
||||
exclude.append(f' "{toml.parent}",')
|
||||
exclude.append("]")
|
||||
replace_in_file(
|
||||
Path("Cargo.toml"), r"exclude = \[[^\]]+\]", "\n".join(exclude)
|
||||
)
|
||||
replace_in_file(Path("Cargo.toml"), re.compile(r"exclude = \[[^\]]+\]"), "\n".join(exclude))
|
||||
|
||||
|
||||
def main():
|
||||
# Move crates from the root to common/
|
||||
crates_to_move = [
|
||||
"assertions",
|
||||
"audio_streams",
|
||||
"base",
|
||||
"cros_async",
|
||||
"data_model",
|
||||
"io_uring",
|
||||
"sync",
|
||||
"sys_util",
|
||||
]
|
||||
for crate in crates_to_move:
|
||||
move_crate(Path(crate), Path("common") / crate)
|
||||
|
||||
# Rename fuzz crate to match package name
|
||||
move_crate(Path("fuzz"), Path("crosvm-fuzz"))
|
||||
replace_in_file(
|
||||
Path("tools/impl/test_config.py"),
|
||||
r'"fuzz"',
|
||||
r'"crosvm-fuzz"',
|
||||
)
|
||||
|
||||
# Remove old ebuild annotations from crosvm internal crates
|
||||
replace_in_all_workspace_toml(
|
||||
r"\ #\ [a-zA-Z0-9_]+\ by\ ebuild",
|
||||
r"",
|
||||
)
|
||||
|
||||
# Remove separate workspaces from top level crates
|
||||
replace_in_all_workspace_toml(
|
||||
r"\[workspace\]",
|
||||
r"",
|
||||
)
|
||||
os.chdir(Path(__file__).parent.parent.parent)
|
||||
|
||||
move_crate(Path("common/base"), Path("base"))
|
||||
update_workspace_members()
|
||||
|
||||
|
||||
|
|
|
@ -403,12 +403,6 @@ def main():
|
|||
print("Offending line: " + error)
|
||||
sys.exit(-1)
|
||||
|
||||
hygiene, crates = is_sys_util_independent()
|
||||
if not hygiene:
|
||||
print("Error: Following files depend on sys_util, sys_util_core or on win_sys_util")
|
||||
print(crates)
|
||||
sys.exit(-1)
|
||||
|
||||
crlf_endings = has_crlf_line_endings()
|
||||
if crlf_endings:
|
||||
print("Error: Following files have crlf(dos) line encodings")
|
||||
|
|
Loading…
Reference in a new issue