Skip to content

Commit fff899d

Browse files
chore(apollo_infra_utils,blockifier_test_utils): move verify_cairo0_compiler_deps to infra_utils
1 parent 8f26617 commit fff899d

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::fs;
2+
use std::path::PathBuf;
3+
use std::process::Command;
4+
use std::sync::LazyLock;
5+
6+
use crate::path::resolve_project_relative_path;
7+
8+
/// The local python requirements used to determine the cairo0 compiler version.
9+
static PIP_REQUIREMENTS_FILE: LazyLock<PathBuf> =
10+
LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap());
11+
12+
/// Verifies that the required Cairo0 compiler is available; panics if unavailable.
13+
pub fn verify_cairo0_compiler_deps() {
14+
// Python compiler. Verify correct version.
15+
let cairo_lang_version_output =
16+
Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout;
17+
let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap();
18+
let cairo_lang_version =
19+
cairo_lang_version_untrimmed.trim().split("==").nth(1).unwrap_or_else(|| {
20+
panic!("Unexpected cairo-lang version format '{cairo_lang_version_untrimmed}'.")
21+
});
22+
let requirements_contents = fs::read_to_string(&*PIP_REQUIREMENTS_FILE).unwrap();
23+
let expected_cairo_lang_version = requirements_contents
24+
.lines()
25+
.find(|line| line.starts_with("cairo-lang"))
26+
.unwrap_or_else(|| panic!("Could not find cairo-lang in {:?}.", *PIP_REQUIREMENTS_FILE))
27+
.trim()
28+
.split("==")
29+
.nth(1)
30+
.unwrap_or_else(|| {
31+
panic!(
32+
"Malformed cairo-lang dependency (expected 'cairo-lang==X') in {:?}.",
33+
*PIP_REQUIREMENTS_FILE
34+
)
35+
});
36+
37+
assert_eq!(
38+
expected_cairo_lang_version, cairo_lang_version,
39+
"cairo-lang version {expected_cairo_lang_version} not found (installed version: \
40+
{cairo_lang_version}). Please run:\npip3.9 install -r {:?}\nthen rerun the test.",
41+
*PIP_REQUIREMENTS_FILE
42+
);
43+
}

crates/apollo_infra_utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod cairo0_compiler;
12
#[cfg(any(feature = "testing", test))]
23
pub mod cairo_compiler_version;
34
pub mod command;

crates/blockifier_test_utils/src/cairo_compile.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
use std::fs;
21
use std::io::Write;
32
use std::path::PathBuf;
43
use std::process::{Command, Output, Stdio};
5-
use std::sync::LazyLock;
64

5+
use apollo_infra_utils::cairo0_compiler::verify_cairo0_compiler_deps;
76
use apollo_infra_utils::cairo_compiler_version::cairo1_compiler_version;
8-
use apollo_infra_utils::path::{project_path, resolve_project_relative_path};
7+
use apollo_infra_utils::path::project_path;
98
use tempfile::NamedTempFile;
109
use tracing::info;
1110

12-
static CAIRO0_PIP_REQUIREMENTS_FILE: LazyLock<PathBuf> =
13-
LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap());
14-
1511
pub enum CompilationArtifacts {
1612
Cairo0 { casm: Vec<u8> },
1713
Cairo1 { casm: Vec<u8>, sierra: Vec<u8> },
@@ -145,38 +141,3 @@ fn starknet_sierra_compile(path: String, version: &String) -> Vec<u8> {
145141
let casm_output = run_and_verify_output(&mut sierra_compile_command);
146142
casm_output.stdout
147143
}
148-
149-
/// Verifies that the required dependencies are available before compiling; panics if unavailable.
150-
fn verify_cairo0_compiler_deps() {
151-
// Python compiler. Verify correct version.
152-
let cairo_lang_version_output =
153-
Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout;
154-
let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap();
155-
let cairo_lang_version =
156-
cairo_lang_version_untrimmed.trim().split("==").nth(1).unwrap_or_else(|| {
157-
panic!("Unexpected cairo-lang version format '{cairo_lang_version_untrimmed}'.")
158-
});
159-
let requirements_contents = fs::read_to_string(&*CAIRO0_PIP_REQUIREMENTS_FILE).unwrap();
160-
let expected_cairo_lang_version = requirements_contents
161-
.lines()
162-
.find(|line| line.starts_with("cairo-lang"))
163-
.unwrap_or_else(|| {
164-
panic!("Could not find cairo-lang in {:?}.", *CAIRO0_PIP_REQUIREMENTS_FILE)
165-
})
166-
.trim()
167-
.split("==")
168-
.nth(1)
169-
.unwrap_or_else(|| {
170-
panic!(
171-
"Malformed cairo-lang dependency (expected 'cairo-lang==X') in {:?}.",
172-
*CAIRO0_PIP_REQUIREMENTS_FILE
173-
)
174-
});
175-
176-
assert_eq!(
177-
expected_cairo_lang_version, cairo_lang_version,
178-
"cairo-lang version {expected_cairo_lang_version} not found (installed version: \
179-
{cairo_lang_version}). Please run:\npip3.9 install -r {:?}\nthen rerun the test.",
180-
*CAIRO0_PIP_REQUIREMENTS_FILE
181-
);
182-
}

0 commit comments

Comments
 (0)