diff options
author | Bob Haarman <inglorion@chromium.org> | 2024-01-31 23:16:07 +0000 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-02-01 18:48:06 +0000 |
commit | 0a0912727ddaf0bae67a4b8c0fd41aa5d57b00b5 (patch) | |
tree | 985bc9cd7ea05a8143a985c35a17245349c94731 | |
parent | c2b9e7a4b98906464c89062c3e9d755e7919963a (diff) | |
download | toolchain-utils-0a0912727ddaf0bae67a4b8c0fd41aa5d57b00b5.tar.gz |
rust_uprev: refactor create_ebuild() to take strings instead of a path
create_ebuild() used to take a path to an ebuild file to copy and a
pkgatom which it would split and use to compute the new ebuild path
from. This change refactors create_ebuild() to take the ebuild's
category and name as separate strings (to avoid having to split
strings), and computes both the path to the source ebuild and the
path to the ebuild to be created (to avoid the inconsistency of
computing one but requiring the other to be passed in). This
refactor will be built upon by an upcoming refactor of
create_rust_uprev(), for which it is convenient not to have to
compute the source ebuild path before calling create_ebuild().
BUG=b:322348600
TEST=presubmit
Change-Id: I000e45423e7e33b110d60bd04a59c9f2863597b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5255378
Reviewed-by: George Burgess <gbiv@chromium.org>
Tested-by: Bob Haarman <inglorion@chromium.org>
Commit-Queue: Bob Haarman <inglorion@chromium.org>
-rwxr-xr-x | rust_tools/rust_uprev.py | 48 | ||||
-rwxr-xr-x | rust_tools/rust_uprev_test.py | 37 |
2 files changed, 52 insertions, 33 deletions
diff --git a/rust_tools/rust_uprev.py b/rust_tools/rust_uprev.py index ce88f9a5..f64b3f3a 100755 --- a/rust_tools/rust_uprev.py +++ b/rust_tools/rust_uprev.py @@ -188,10 +188,13 @@ class PreparedUprev(NamedTuple): """Container for the information returned by prepare_uprev.""" template_version: RustVersion - ebuild_path: Path bootstrap_version: RustVersion +def compute_ebuild_path(category: str, name: str, version: RustVersion) -> Path: + return EBUILD_PREFIX / category / name / f"{name}-{version}.ebuild" + + def compute_rustc_src_name(version: RustVersion) -> str: return f"rustc-{version}-src.tar.gz" @@ -408,17 +411,21 @@ def prepare_uprev( ) logging.info("rust-bootstrap version is %s", bootstrap_version) - return PreparedUprev(template, ebuild_path, bootstrap_version) + return PreparedUprev(template, bootstrap_version) def create_ebuild( - template_ebuild: PathOrStr, pkgatom: str, new_version: RustVersion -) -> str: - filename = f"{Path(pkgatom).name}-{new_version}.ebuild" - ebuild = EBUILD_PREFIX.joinpath(f"{pkgatom}/{filename}") - shutil.copyfile(template_ebuild, ebuild) - subprocess.check_call(["git", "add", filename], cwd=ebuild.parent) - return str(ebuild) + category: str, + name: str, + template_version: RustVersion, + new_version: RustVersion, +) -> None: + template_ebuild = compute_ebuild_path(category, name, template_version) + new_ebuild = compute_ebuild_path(category, name, new_version) + shutil.copyfile(template_ebuild, new_ebuild) + subprocess.check_call( + ["git", "add", new_ebuild.name], cwd=new_ebuild.parent + ) def set_include_profdata_src(ebuild_path: os.PathLike, include: bool) -> None: @@ -783,22 +790,20 @@ def perform_step( def prepare_uprev_from_json(obj: Any) -> Optional[PreparedUprev]: if not obj: return None - version, ebuild_path, bootstrap_version = obj + version, bootstrap_version = obj return PreparedUprev( RustVersion(*version), - Path(ebuild_path), RustVersion(*bootstrap_version), ) def prepare_uprev_to_json( prepared_uprev: Optional[PreparedUprev], -) -> Optional[Tuple[RustVersion, str, RustVersion]]: +) -> Optional[Tuple[RustVersion, RustVersion]]: if prepared_uprev is None: return None return ( prepared_uprev.template_version, - str(prepared_uprev.ebuild_path), prepared_uprev.bootstrap_version, ) @@ -817,7 +822,7 @@ def create_rust_uprev( ) if prepared is None: return - template_version, template_ebuild, old_bootstrap_version = prepared + template_version, old_bootstrap_version = prepared run_step( "mirror bootstrap sources", @@ -859,13 +864,13 @@ def create_rust_uprev( "turn off profile data sources in cros-rustc.eclass", lambda: set_include_profdata_src(CROS_RUSTC_ECLASS, include=False), ) - template_host_ebuild = EBUILD_PREFIX.joinpath( - f"dev-lang/rust-host/rust-host-{template_version}.ebuild" - ) run_step( "create host ebuild", lambda: create_ebuild( - template_host_ebuild, "dev-lang/rust-host", rust_version + "dev-lang", + "rust-host", + template_version, + rust_version, ), ) run_step( @@ -874,7 +879,12 @@ def create_rust_uprev( ) run_step( "create target ebuild", - lambda: create_ebuild(template_ebuild, "dev-lang/rust", rust_version), + lambda: create_ebuild( + "dev-lang", + "rust", + template_version, + rust_version, + ), ) run_step( "update target manifest to add new version", diff --git a/rust_tools/rust_uprev_test.py b/rust_tools/rust_uprev_test.py index 67adc3a4..38c26f22 100755 --- a/rust_tools/rust_uprev_test.py +++ b/rust_tools/rust_uprev_test.py @@ -501,7 +501,7 @@ class PrepareUprevTest(unittest.TestCase): ) mock_find_ebuild.return_value = bootstrap_ebuild_path expected = rust_uprev.PreparedUprev( - self.version_old, Path("/path/to/ebuild"), self.bootstrap_version + self.version_old, self.bootstrap_version ) actual = rust_uprev.prepare_uprev( rust_version=self.version_new, template=self.version_old @@ -530,15 +530,12 @@ class PrepareUprevTest(unittest.TestCase): mock_command.assert_not_called() def test_prepare_uprev_from_json(self): - ebuild_path = Path("/path/to/the/ebuild") json_result = ( list(self.version_new), - ebuild_path, list(self.bootstrap_version), ) expected = rust_uprev.PreparedUprev( self.version_new, - Path(ebuild_path), self.bootstrap_version, ) actual = rust_uprev.prepare_uprev_from_json(json_result) @@ -747,35 +744,47 @@ class RustUprevOtherStagesTests(unittest.TestCase): @mock.patch.object(shutil, "copyfile") @mock.patch.object(subprocess, "check_call") def test_create_rust_ebuild(self, mock_call, mock_copy): - template_ebuild = f"/path/to/rust-{self.current_version}-r2.ebuild" + template_ebuild = ( + rust_uprev.EBUILD_PREFIX + / f"dev-lang/rust/rust-{self.current_version}.ebuild" + ) + new_ebuild = ( + rust_uprev.EBUILD_PREFIX + / f"dev-lang/rust/rust-{self.new_version}.ebuild" + ) rust_uprev.create_ebuild( - template_ebuild, "dev-lang/rust", self.new_version + "dev-lang", "rust", self.current_version, self.new_version ) mock_copy.assert_called_once_with( template_ebuild, - rust_uprev.RUST_PATH.joinpath(f"rust-{self.new_version}.ebuild"), + new_ebuild, ) mock_call.assert_called_once_with( ["git", "add", f"rust-{self.new_version}.ebuild"], - cwd=rust_uprev.RUST_PATH, + cwd=new_ebuild.parent, ) @mock.patch.object(shutil, "copyfile") @mock.patch.object(subprocess, "check_call") def test_create_rust_host_ebuild(self, mock_call, mock_copy): - template_ebuild = f"/path/to/rust-host-{self.current_version}-r2.ebuild" + template_ebuild = ( + rust_uprev.EBUILD_PREFIX + / f"dev-lang/rust-host/rust-host-{self.current_version}.ebuild" + ) + new_ebuild = ( + rust_uprev.EBUILD_PREFIX + / f"dev-lang/rust-host/rust-host-{self.new_version}.ebuild" + ) rust_uprev.create_ebuild( - template_ebuild, "dev-lang/rust-host", self.new_version + "dev-lang", "rust-host", self.current_version, self.new_version ) mock_copy.assert_called_once_with( template_ebuild, - rust_uprev.EBUILD_PREFIX.joinpath( - f"dev-lang/rust-host/rust-host-{self.new_version}.ebuild" - ), + new_ebuild, ) mock_call.assert_called_once_with( ["git", "add", f"rust-host-{self.new_version}.ebuild"], - cwd=rust_uprev.EBUILD_PREFIX.joinpath("dev-lang/rust-host"), + cwd=new_ebuild.parent, ) @mock.patch.object(rust_uprev, "find_ebuild_for_package") |