|
1 | | -use std::fs; |
| 1 | +#[cfg(any(test, feature = "testing"))] |
2 | 2 | use std::path::PathBuf; |
3 | 3 | use std::process::Command; |
| 4 | +#[cfg(any(test, feature = "testing"))] |
4 | 5 | use std::sync::LazyLock; |
5 | 6 |
|
| 7 | +#[cfg(any(test, feature = "testing"))] |
6 | 8 | use crate::path::resolve_project_relative_path; |
7 | 9 |
|
| 10 | +#[cfg(test)] |
| 11 | +#[path = "cairo0_compiler_test.rs"] |
| 12 | +pub mod test; |
| 13 | + |
| 14 | +pub const STARKNET_COMPILE_DEPRECATED: &str = "starknet-compile-deprecated"; |
| 15 | +pub const CAIRO0_COMPILE: &str = "cairo-compile"; |
| 16 | +pub const EXPECTED_CAIRO0_VERSION: &str = "0.13.5"; |
| 17 | + |
8 | 18 | /// The local python requirements used to determine the cairo0 compiler version. |
9 | | -static PIP_REQUIREMENTS_FILE: LazyLock<PathBuf> = |
| 19 | +#[cfg(any(test, feature = "testing"))] |
| 20 | +pub(crate) static PIP_REQUIREMENTS_FILE: LazyLock<PathBuf> = |
10 | 21 | LazyLock::new(|| resolve_project_relative_path("scripts/requirements.txt").unwrap()); |
11 | 22 |
|
| 23 | +#[derive(thiserror::Error, Debug)] |
| 24 | +pub enum Cairo0CompilerVersionError { |
| 25 | + #[error("{compiler} version is not correct: required {required}, got {existing}.")] |
| 26 | + IncorrectVersion { compiler: String, existing: String, required: String }, |
| 27 | + #[error("{0} not found.")] |
| 28 | + NotFound(String), |
| 29 | +} |
| 30 | + |
| 31 | +pub fn cairo0_compilers_correct_version() -> Result<(), Cairo0CompilerVersionError> { |
| 32 | + for compiler in [CAIRO0_COMPILE, STARKNET_COMPILE_DEPRECATED] { |
| 33 | + let version = match Command::new(compiler).arg("--version").output() { |
| 34 | + Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(), |
| 35 | + Err(error) => { |
| 36 | + return Err(Cairo0CompilerVersionError::NotFound(format!( |
| 37 | + "Failed to get {compiler} version: {error}." |
| 38 | + ))); |
| 39 | + } |
| 40 | + }; |
| 41 | + if !version.trim().ends_with(EXPECTED_CAIRO0_VERSION) { |
| 42 | + return Err(Cairo0CompilerVersionError::IncorrectVersion { |
| 43 | + compiler: compiler.to_string(), |
| 44 | + existing: version, |
| 45 | + required: EXPECTED_CAIRO0_VERSION.to_string(), |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
12 | 53 | /// Verifies that the required Cairo0 compiler is available; panics if unavailable. |
| 54 | +/// For use in tests only. If cairo0 compiler verification is required in business logic, use |
| 55 | +/// `crate::cairo0_compiler::cairo0_compilers_correct_version` instead. |
| 56 | +#[cfg(any(test, feature = "testing"))] |
13 | 57 | 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 ({}). Run the following \ |
29 | | - commands (enter a python venv and install dependencies) and retry:\npython -m venv \ |
30 | | - sequencer_venv\n. sequencer_venv/bin/activate\npip install -r {:?}", |
31 | | - if cairo_lang_version.is_empty() { |
32 | | - String::from("no installed cairo-lang found") |
33 | | - } else { |
34 | | - format!("installed version: {cairo_lang_version}") |
35 | | - }, |
36 | | - *PIP_REQUIREMENTS_FILE |
| 58 | + let specific_error = match cairo0_compilers_correct_version() { |
| 59 | + Ok(_) => { |
| 60 | + return; |
| 61 | + } |
| 62 | + Err(Cairo0CompilerVersionError::NotFound(_)) => "no installed cairo-lang found".to_string(), |
| 63 | + Err(Cairo0CompilerVersionError::IncorrectVersion { existing, .. }) => { |
| 64 | + format!("installed version: {existing}") |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + panic!( |
| 69 | + "cairo-lang version {EXPECTED_CAIRO0_VERSION} not found ({specific_error}). Run the \ |
| 70 | + following commands (enter a python venv and install dependencies) and retry:\npython -m \ |
| 71 | + venv sequencer_venv\n. sequencer_venv/bin/activate\npip install -r {:?}", |
| 72 | + PIP_REQUIREMENTS_FILE.to_str().expect("Path to requirements.txt is valid unicode.") |
37 | 73 | ); |
38 | 74 | } |
0 commit comments