Skip to content

Commit 7d86c6e

Browse files
chore(apollo_infra_utils,blockifier_test_utils): move verify_cairo0_compiler_deps to infra_utils
1 parent c6425a3 commit 7d86c6e

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 = cairo_lang_version_untrimmed.trim();
19+
let requirements_contents = fs::read_to_string(&*PIP_REQUIREMENTS_FILE).unwrap();
20+
let expected_cairo_lang_version = requirements_contents
21+
.lines()
22+
.find(|line| line.starts_with("cairo-lang"))
23+
.unwrap_or_else(|| panic!("Could not find cairo-lang in {:?}.", *PIP_REQUIREMENTS_FILE))
24+
.trim();
25+
26+
assert!(
27+
expected_cairo_lang_version.ends_with(cairo_lang_version),
28+
"cairo-lang version {expected_cairo_lang_version} not found ({}). Please run:\npip3.9 \
29+
install -r {:?}\nthen rerun the test.",
30+
if cairo_lang_version.is_empty() {
31+
String::from("no installed cairo-lang found")
32+
} else {
33+
format!("installed version: {cairo_lang_version}")
34+
},
35+
*PIP_REQUIREMENTS_FILE
36+
);
37+
}

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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1+
use std::env;
12
use std::io::Write;
23
use std::path::{Path, PathBuf};
34
use std::process::{Command, Output};
4-
use std::sync::LazyLock;
5-
use std::{env, fs};
65

6+
use apollo_infra_utils::cairo0_compiler::verify_cairo0_compiler_deps;
77
use apollo_infra_utils::cairo_compiler_version::cairo1_compiler_version;
88
use apollo_infra_utils::compile_time_cargo_manifest_dir;
9-
use apollo_infra_utils::path::resolve_project_relative_path;
109
use tempfile::NamedTempFile;
1110

1211
use crate::contracts::TagAndToolchain;
1312

14-
static CAIRO0_PIP_REQUIREMENTS_FILE: LazyLock<PathBuf> =
15-
LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap());
1613
const CAIRO1_REPO_RELATIVE_PATH_OVERRIDE_ENV_VAR: &str = "CAIRO1_REPO_RELATIVE_PATH";
1714
const DEFAULT_CAIRO1_REPO_RELATIVE_PATH: &str = "../../../cairo";
1815

@@ -138,35 +135,6 @@ pub fn starknet_compile(
138135
sierra_output.stdout
139136
}
140137

141-
/// Verifies that the required dependencies are available before compiling; panics if unavailable.
142-
fn verify_cairo0_compiler_deps() {
143-
// Python compiler. Verify correct version.
144-
let cairo_lang_version_output =
145-
Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout;
146-
let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap();
147-
let cairo_lang_version = cairo_lang_version_untrimmed.trim();
148-
let requirements_contents = fs::read_to_string(&*CAIRO0_PIP_REQUIREMENTS_FILE).unwrap();
149-
let expected_cairo_lang_version = requirements_contents
150-
.lines()
151-
.find(|line| line.starts_with("cairo-lang"))
152-
.unwrap_or_else(|| {
153-
panic!("Could not find cairo-lang in {:?}.", *CAIRO0_PIP_REQUIREMENTS_FILE)
154-
})
155-
.trim();
156-
157-
assert!(
158-
expected_cairo_lang_version.ends_with(cairo_lang_version),
159-
"cairo-lang version {expected_cairo_lang_version} not found ({}). Please run:\npip3.9 \
160-
install -r {:?}\nthen rerun the test.",
161-
if cairo_lang_version.is_empty() {
162-
String::from("no installed cairo-lang found")
163-
} else {
164-
format!("installed version: {cairo_lang_version}")
165-
},
166-
*CAIRO0_PIP_REQUIREMENTS_FILE
167-
);
168-
}
169-
170138
fn get_tag_and_repo_file_path(git_tag_override: Option<String>) -> (String, PathBuf) {
171139
let tag = git_tag_override.unwrap_or(cairo1_compiler_tag());
172140
let cairo_repo_path = local_cairo1_compiler_repo_path();

0 commit comments

Comments
 (0)