Skip to content

Commit 66b2862

Browse files
committed
Create lockfile for Cargo package if missing
1 parent 78e5c3c commit 66b2862

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

easybuild/easyblocks/generic/cargo.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@
4343
import easybuild.tools.systemtools as systemtools
4444
from easybuild.framework.easyconfig import CUSTOM
4545
from easybuild.framework.extensioneasyblock import ExtensionEasyBlock
46+
from easybuild.tools import LooseVersion
4647
from easybuild.tools.build_log import EasyBuildError, print_warning
4748
from easybuild.tools.config import build_option
4849
from easybuild.tools.filetools import CHECKSUM_TYPE_SHA256, compute_checksum, copy_dir, extract_file, mkdir
4950
from easybuild.tools.filetools import read_file, remove_dir, write_file, which
51+
from easybuild.tools.modules import get_software_version
5052
from easybuild.tools.run import run_shell_cmd
5153
from easybuild.tools.toolchain.compiler import OPTARCH_GENERIC
5254

5355
CRATESIO_SOURCE = "https://crates.io/api/v1/crates"
56+
CRATES_REGISTRY_URL = 'registry+https://github.com/rust-lang/crates.io-index'
5457

5558
CONFIG_TOML_SOURCE_VENDOR = """
5659
[source.vendored-sources]
@@ -76,6 +79,14 @@
7679
replace-with = "vendored-sources"
7780
"""
7881

82+
CONFIG_LOCK_SOURCE = """
83+
[[package]]
84+
name = "{name}"
85+
version = "{version}"
86+
source = "{source}"
87+
# checksum intentionally not set
88+
"""
89+
7990
CARGO_CHECKSUM_JSON = '{{"files": {{}}, "package": "{checksum}"}}'
8091

8192

@@ -610,8 +621,33 @@ def prepare_step(self, *args, **kwargs):
610621
self.set_cargo_vars()
611622

612623
def configure_step(self):
613-
"""Empty configuration step."""
614-
pass
624+
"""Create lockfile if it doesn't exist"""
625+
cargo_lock = 'Cargo.lock'
626+
if self.crates and os.path.exists('Cargo.toml') and not os.path.exists(cargo_lock):
627+
root_toml = run_shell_cmd('cargo locate-project --message-format=plain --workspace').output
628+
cargo_lock_path = os.path.join(os.path.dirname(root_toml), cargo_lock)
629+
if not os.path.exists(cargo_lock_path):
630+
rust_version = LooseVersion(get_software_version('Rust'))
631+
# File format version, oldest supported used for compatibility
632+
if rust_version <= '1.37':
633+
version = 1
634+
elif rust_version <= '1.81':
635+
version = 2
636+
else:
637+
version = 3
638+
# Use vendored crates to ensure those versions are used
639+
self.log.info(f"No {cargo_lock} file found, creating one at {cargo_lock_path}")
640+
content = f'version = {version}\n'
641+
for crate_info in self.crates:
642+
if len(crate_info) == 2:
643+
name, version = crate_info
644+
source = CRATES_REGISTRY_URL
645+
else:
646+
name, version, repo, rev = crate_info
647+
source = f'git+{repo}?rev={rev}#{rev}'
648+
649+
content += CONFIG_LOCK_SOURCE.format(name=name, version=version, source=source)
650+
write_file(cargo_lock_path, content)
615651

616652
@property
617653
def profile(self):
@@ -701,7 +737,7 @@ def generate_crate_list(sourcedir):
701737
if name == app_name:
702738
app_in_cratesio = True # exclude app itself, needs to be first in crates list or taken from pypi
703739
else:
704-
if source_url == 'registry+https://github.com/rust-lang/crates.io-index':
740+
if source_url == CRATES_REGISTRY_URL:
705741
crates.append((name, version))
706742
else:
707743
# Lock file has revision and branch in the url

easybuild/easyblocks/generic/cargopythonpackage.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ def extra_options(extra_vars=None):
5050
def extract_step(self):
5151
"""Specifically use the overloaded variant from Cargo as is populates vendored sources with checksums."""
5252
return Cargo.extract_step(self)
53+
54+
def configure_step(self):
55+
"""Run configure for Cargo and PythonPackage"""
56+
Cargo.configure_step(self)
57+
PythonPackage.configure_step(self)

0 commit comments

Comments
 (0)