From 384baab3aa5a78f22db4b8fd1ff34f66df9e55d8 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sun, 13 Apr 2025 13:53:03 +0300 Subject: [PATCH] chore(apollo_infra_utils,blockifier_test_utils): move verify_cairo0_compiler_deps to infra_utils --- .../apollo_infra_utils/src/cairo0_compiler.rs | 43 +++++++++++++++++++ crates/apollo_infra_utils/src/lib.rs | 1 + .../src/cairo_compile.rs | 43 +------------------ 3 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 crates/apollo_infra_utils/src/cairo0_compiler.rs diff --git a/crates/apollo_infra_utils/src/cairo0_compiler.rs b/crates/apollo_infra_utils/src/cairo0_compiler.rs new file mode 100644 index 00000000000..bc346327988 --- /dev/null +++ b/crates/apollo_infra_utils/src/cairo0_compiler.rs @@ -0,0 +1,43 @@ +use std::fs; +use std::path::PathBuf; +use std::process::Command; +use std::sync::LazyLock; + +use crate::path::resolve_project_relative_path; + +/// The local python requirements used to determine the cairo0 compiler version. +static PIP_REQUIREMENTS_FILE: LazyLock = + LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap()); + +/// Verifies that the required Cairo0 compiler is available; panics if unavailable. +pub fn verify_cairo0_compiler_deps() { + // Python compiler. Verify correct version. + let cairo_lang_version_output = + Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout; + let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap(); + let cairo_lang_version = + cairo_lang_version_untrimmed.trim().split("==").nth(1).unwrap_or_else(|| { + panic!("Unexpected cairo-lang version format '{cairo_lang_version_untrimmed}'.") + }); + let requirements_contents = fs::read_to_string(&*PIP_REQUIREMENTS_FILE).unwrap(); + let expected_cairo_lang_version = requirements_contents + .lines() + .find(|line| line.starts_with("cairo-lang")) + .unwrap_or_else(|| panic!("Could not find cairo-lang in {:?}.", *PIP_REQUIREMENTS_FILE)) + .trim() + .split("==") + .nth(1) + .unwrap_or_else(|| { + panic!( + "Malformed cairo-lang dependency (expected 'cairo-lang==X') in {:?}.", + *PIP_REQUIREMENTS_FILE + ) + }); + + assert_eq!( + expected_cairo_lang_version, cairo_lang_version, + "cairo-lang version {expected_cairo_lang_version} not found (installed version: \ + {cairo_lang_version}). Please run:\npip3.9 install -r {:?}\nthen rerun the test.", + *PIP_REQUIREMENTS_FILE + ); +} diff --git a/crates/apollo_infra_utils/src/lib.rs b/crates/apollo_infra_utils/src/lib.rs index d8e0389af27..4cf99b239d6 100644 --- a/crates/apollo_infra_utils/src/lib.rs +++ b/crates/apollo_infra_utils/src/lib.rs @@ -1,3 +1,4 @@ +pub mod cairo0_compiler; #[cfg(any(feature = "testing", test))] pub mod cairo_compiler_version; pub mod command; diff --git a/crates/blockifier_test_utils/src/cairo_compile.rs b/crates/blockifier_test_utils/src/cairo_compile.rs index df0479e8cf6..3ecc5c9f4ba 100644 --- a/crates/blockifier_test_utils/src/cairo_compile.rs +++ b/crates/blockifier_test_utils/src/cairo_compile.rs @@ -1,17 +1,13 @@ -use std::fs; use std::io::Write; use std::path::PathBuf; use std::process::{Command, Output, Stdio}; -use std::sync::LazyLock; +use apollo_infra_utils::cairo0_compiler::verify_cairo0_compiler_deps; use apollo_infra_utils::cairo_compiler_version::cairo1_compiler_version; -use apollo_infra_utils::path::{project_path, resolve_project_relative_path}; +use apollo_infra_utils::path::project_path; use tempfile::NamedTempFile; use tracing::info; -static CAIRO0_PIP_REQUIREMENTS_FILE: LazyLock = - LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap()); - pub enum CompilationArtifacts { Cairo0 { casm: Vec }, Cairo1 { casm: Vec, sierra: Vec }, @@ -145,38 +141,3 @@ fn starknet_sierra_compile(path: String, version: &String) -> Vec { let casm_output = run_and_verify_output(&mut sierra_compile_command); casm_output.stdout } - -/// Verifies that the required dependencies are available before compiling; panics if unavailable. -fn verify_cairo0_compiler_deps() { - // Python compiler. Verify correct version. - let cairo_lang_version_output = - Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout; - let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap(); - let cairo_lang_version = - cairo_lang_version_untrimmed.trim().split("==").nth(1).unwrap_or_else(|| { - panic!("Unexpected cairo-lang version format '{cairo_lang_version_untrimmed}'.") - }); - let requirements_contents = fs::read_to_string(&*CAIRO0_PIP_REQUIREMENTS_FILE).unwrap(); - let expected_cairo_lang_version = requirements_contents - .lines() - .find(|line| line.starts_with("cairo-lang")) - .unwrap_or_else(|| { - panic!("Could not find cairo-lang in {:?}.", *CAIRO0_PIP_REQUIREMENTS_FILE) - }) - .trim() - .split("==") - .nth(1) - .unwrap_or_else(|| { - panic!( - "Malformed cairo-lang dependency (expected 'cairo-lang==X') in {:?}.", - *CAIRO0_PIP_REQUIREMENTS_FILE - ) - }); - - assert_eq!( - expected_cairo_lang_version, cairo_lang_version, - "cairo-lang version {expected_cairo_lang_version} not found (installed version: \ - {cairo_lang_version}). Please run:\npip3.9 install -r {:?}\nthen rerun the test.", - *CAIRO0_PIP_REQUIREMENTS_FILE - ); -}