From d2af8f58d58eae02393d2610c2c854bc678baa71 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 28 Sep 2023 15:14:48 +0300 Subject: [PATCH] fixes after review --- multiversx_sdk_cli/config.py | 1 + multiversx_sdk_cli/dependencies/modules.py | 25 +++++++++----------- multiversx_sdk_cli/projects/project_clang.py | 7 ++---- multiversx_sdk_cli/projects/project_cpp.py | 7 ++---- multiversx_sdk_cli/projects/project_rust.py | 12 +++------- multiversx_sdk_cli/projects/shared.py | 12 +++++----- multiversx_sdk_cli/tests/test_cli_deps.py | 21 +++++++--------- 7 files changed, 33 insertions(+), 52 deletions(-) diff --git a/multiversx_sdk_cli/config.py b/multiversx_sdk_cli/config.py index 31972ffb..3cf89ba4 100644 --- a/multiversx_sdk_cli/config.py +++ b/multiversx_sdk_cli/config.py @@ -157,6 +157,7 @@ def get_defaults() -> Dict[str, Any]: "dependencies.golang.urlTemplate.osx": "https://golang.org/dl/{TAG}.darwin-amd64.tar.gz", "dependencies.golang.urlTemplate.windows": "https://golang.org/dl/{TAG}.windows-amd64.zip", "dependencies.twiggy.tag": "latest", + "dependencies.sc-meta.tag": "latest", "dependencies.testwallets.tag": "latest", "dependencies.testwallets.urlTemplate.linux": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz", "dependencies.testwallets.urlTemplate.osx": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz", diff --git a/multiversx_sdk_cli/dependencies/modules.py b/multiversx_sdk_cli/dependencies/modules.py index 0b313210..19224462 100644 --- a/multiversx_sdk_cli/dependencies/modules.py +++ b/multiversx_sdk_cli/dependencies/modules.py @@ -276,15 +276,15 @@ def install(self, overwrite: bool) -> None: if overwrite: logger.info("Overwriting the current rust version...") - elif self._is_installed_and_set_to_nightly(): + elif self.is_installed(""): return - self._do_install(tag) + self._install_rust(tag) self._install_sc_meta() self._install_wasm_opt() self._install_twiggy() - def _do_install(self, tag: str) -> None: + def _install_rust(self, tag: str) -> None: installer_url = self._get_installer_url() installer_path = self._get_installer_path() @@ -304,7 +304,12 @@ def _do_install(self, tag: str) -> None: def _install_sc_meta(self): logger.info("Installing multiversx-sc-meta.") + tag = config.get_dependency_tag("sc-meta") args = ["cargo", "install", "multiversx-sc-meta"] + + if tag != "latest": + args.extend(["--version", tag]) + myprocess.run_process(args) def _install_wasm_opt(self): @@ -315,11 +320,11 @@ def _install_wasm_opt(self): def _install_twiggy(self): logger.info("Installing twiggy.") - default_tag = config.get_dependency_tag("twiggy") + tag = config.get_dependency_tag("twiggy") args = ["cargo", "install", "twiggy"] - if default_tag != "latest": - args.extend(["--version", default_tag]) + if tag != "latest": + args.extend(["--version", tag]) myprocess.run_process(args) @@ -340,14 +345,6 @@ def uninstall(self, tag: str): if os.path.isdir(directory): shutil.rmtree(directory) - def _is_installed_and_set_to_nightly(self) -> bool: - # the method parameter is not used in this specific module - is_rust_installed = self.is_installed("") - - if not is_rust_installed: - return False - return True - def get_directory(self, tag: str) -> Path: tools_folder = workstation.get_tools_folder() return tools_folder / "vendor-rust" diff --git a/multiversx_sdk_cli/projects/project_clang.py b/multiversx_sdk_cli/projects/project_clang.py index 467cafc0..1b800b7c 100644 --- a/multiversx_sdk_cli/projects/project_clang.py +++ b/multiversx_sdk_cli/projects/project_clang.py @@ -1,6 +1,5 @@ import logging import subprocess -import sys from os import path from pathlib import Path from typing import List @@ -8,7 +7,7 @@ from multiversx_sdk_cli import dependencies, errors, myprocess, utils from multiversx_sdk_cli.projects.project_base import Project, rename_wasm_files from multiversx_sdk_cli.projects.shared import \ - are_clang_and_cpp_dependencies_installed + check_clang_and_cpp_dependencies_installed logger = logging.getLogger('ProjectClang') @@ -29,9 +28,7 @@ def perform_build(self): self.file_output = self.unit.with_suffix('.wasm') try: - is_installed = are_clang_and_cpp_dependencies_installed() - if not is_installed: - sys.exit(1) + check_clang_and_cpp_dependencies_installed() self.do_clang() self.do_llvm_link() diff --git a/multiversx_sdk_cli/projects/project_cpp.py b/multiversx_sdk_cli/projects/project_cpp.py index 72567927..eb2dafef 100644 --- a/multiversx_sdk_cli/projects/project_cpp.py +++ b/multiversx_sdk_cli/projects/project_cpp.py @@ -1,7 +1,6 @@ import logging import os import subprocess -import sys from os import path from pathlib import Path from typing import List @@ -9,7 +8,7 @@ from multiversx_sdk_cli import dependencies, errors, myprocess, utils from multiversx_sdk_cli.projects.project_base import Project, rename_wasm_files from multiversx_sdk_cli.projects.shared import \ - are_clang_and_cpp_dependencies_installed + check_clang_and_cpp_dependencies_installed logger = logging.getLogger("ProjectCpp") @@ -26,9 +25,7 @@ def perform_build(self): self.file_export = self.unit.with_suffix(".export") try: - is_installed = are_clang_and_cpp_dependencies_installed() - if not is_installed: - sys.exit(1) + check_clang_and_cpp_dependencies_installed() self._do_clang() self._do_llc() diff --git a/multiversx_sdk_cli/projects/project_rust.py b/multiversx_sdk_cli/projects/project_rust.py index 092b6456..cc837eb1 100644 --- a/multiversx_sdk_cli/projects/project_rust.py +++ b/multiversx_sdk_cli/projects/project_rust.py @@ -1,11 +1,11 @@ import logging -import shutil import subprocess from pathlib import Path from typing import Any, Dict, List, Set, cast from multiversx_sdk_cli import dependencies, errors, utils, workstation from multiversx_sdk_cli.constants import DEFAULT_CARGO_TARGET_DIR_NAME +from multiversx_sdk_cli.dependencies.modules import Rust from multiversx_sdk_cli.projects.project_base import Project logger = logging.getLogger("ProjectRust") @@ -54,14 +54,7 @@ def prepare_build_wasm_args(self, args: List[str]): self.get_output_folder() ]) - def check_if_sc_meta_is_installed(self): - which_sc_meta = shutil.which("sc-meta") - - if which_sc_meta is None: - raise errors.KnownError("'sc-meta' is not installed. Install it manually or simply run `mxpy deps install rust --overwrite` then try again.") - def run_meta(self): - self.check_if_sc_meta_is_installed() env = self.get_env() args = [ @@ -125,7 +118,8 @@ def get_env(self): return dependencies.get_module_by_key("rust").get_env() def build_wasm_with_debug_symbols(self, build_options: Dict[str, Any]): - self.check_if_sc_meta_is_installed() + rust_module = Rust("rust") + rust_module.install(overwrite=False) cwd = self.path env = self.get_env() diff --git a/multiversx_sdk_cli/projects/shared.py b/multiversx_sdk_cli/projects/shared.py index 51ebbd32..a99bb42b 100644 --- a/multiversx_sdk_cli/projects/shared.py +++ b/multiversx_sdk_cli/projects/shared.py @@ -2,6 +2,7 @@ import shutil from pathlib import Path +from multiversx_sdk_cli.errors import KnownError from multiversx_sdk_cli.ux import show_critical_error logger = logging.getLogger("projects.shared") @@ -30,7 +31,7 @@ def _directory_contains_file(directory: Path, name_suffix: str) -> bool: return False -def are_clang_and_cpp_dependencies_installed() -> bool: +def check_clang_and_cpp_dependencies_installed() -> None: which_clang = shutil.which("clang") which_llc = shutil.which("llc") which_wasm_ld = shutil.which("wasm-ld") @@ -43,14 +44,13 @@ def are_clang_and_cpp_dependencies_installed() -> bool: dependencies = [which_clang, which_llc, which_wasm_ld, which_llvm_link] is_installed = all(dependency is not None for dependency in dependencies) - if is_installed: - return True - message = """ + if is_installed is False: + message = """ `clang` is not installed. Please install it manually, then try again. How to install on Ubuntu: https://linux.how2shout.com/how-to-install-clang-on-ubuntu-linux/ How to install on MacOS: https://www.incredibuild.com/integrations/clang For more details check out this page: https://clang.llvm.org/get_started.html""" - show_critical_error(message) - return False + show_critical_error(message) + raise KnownError("The required dependencies are not installed. Please check the above message.") diff --git a/multiversx_sdk_cli/tests/test_cli_deps.py b/multiversx_sdk_cli/tests/test_cli_deps.py index 7493dee7..3859505b 100644 --- a/multiversx_sdk_cli/tests/test_cli_deps.py +++ b/multiversx_sdk_cli/tests/test_cli_deps.py @@ -8,7 +8,7 @@ def test_deps_install_rust(): return_code = main(["deps", "install", "rust", "--overwrite"]) - assert True if return_code == 0 else False + assert return_code == 0 def test_deps_check_rust(): @@ -16,33 +16,28 @@ def test_deps_check_rust(): assert True if return_code == 0 else False which_rustc = shutil.which("rustc") - if which_rustc: - assert Path.is_file(Path(which_rustc)) + assert which_rustc and Path.is_file(Path(which_rustc)) which_cargo = shutil.which("cargo") - if which_cargo: - assert Path.is_file(Path(which_cargo)) + assert which_cargo and Path.is_file(Path(which_cargo)) which_sc_meta = shutil.which("sc-meta") - if which_sc_meta: - assert Path.is_file(Path(which_sc_meta)) + assert which_sc_meta and Path.is_file(Path(which_sc_meta)) which_wasm_opt = shutil.which("wasm-opt") - if which_wasm_opt: - assert Path.is_file(Path(which_wasm_opt)) + assert which_wasm_opt and Path.is_file(Path(which_wasm_opt)) which_twiggy = shutil.which("twiggy") - if which_twiggy: - assert Path.is_file(Path(which_twiggy)) + assert which_twiggy and Path.is_file(Path(which_twiggy)) @pytest.mark.skip_on_windows def test_deps_install_vmtools(): return_code = main(["deps", "install", "vmtools"]) - assert True if return_code == 0 else False + assert return_code == 0 @pytest.mark.skip_on_windows def test_deps_check_vmtools(): return_code = main(["deps", "check", "vmtools"]) - assert True if return_code == 0 else False + assert return_code == 0