Skip to content

Commit 7059ac7

Browse files
authored
Merge pull request #4081 from Flamefire/20260227142354_new_pr_cargo
Also handle references to git tags in Cargo crates and download git repos recursively
2 parents c0b3a89 + 805ac9f commit 7059ac7

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

easybuild/easyblocks/generic/cargo.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@
7272
replace-with = "vendored-sources"
7373
"""
7474

75-
CONFIG_TOML_SOURCE_GIT_BRANCH = """
76-
[source."{url}?rev={rev}"]
77-
git = "{url}"
78-
rev = "{rev}"
79-
branch = "{branch}"
80-
replace-with = "vendored-sources"
81-
"""
82-
8375
CONFIG_LOCK_SOURCE = """
8476
[[package]]
8577
name = "{name}"
@@ -253,7 +245,7 @@ def __init__(self, *args, **kwargs):
253245
if repo_name.endswith('.git'):
254246
repo_name = repo_name[:-4]
255247
sources.append({
256-
'git_config': {'url': url, 'repo_name': repo_name, 'commit': rev},
248+
'git_config': {'url': url, 'repo_name': repo_name, 'commit': rev, 'recursive': True},
257249
'filename': self.crate_src_filename(crate, version, rev=rev),
258250
})
259251
self.cfg.update('sources', sources)
@@ -444,15 +436,20 @@ def _setup_offline_config(self, git_sources):
444436
# can't checkout from 'https://github.com/[...]]': you are in the offline mode (--offline)
445437
for (git_repo, rev), src in git_sources.items():
446438
crate_name = src['crate'][0]
447-
git_branch = self._get_crate_git_repo_branch(crate_name)
448-
template = CONFIG_TOML_SOURCE_GIT_BRANCH if git_branch else CONFIG_TOML_SOURCE_GIT
449-
self.log.debug(f"Writing config.toml entry for git repo: {git_repo} branch {git_branch}, rev {rev}")
450-
write_file(config_toml, template.format(url=git_repo, rev=rev, branch=git_branch), append=True)
451-
452-
def _get_crate_git_repo_branch(self, crate_name):
439+
git_refs = self._get_crate_git_repo_refs(crate_name)
440+
branch, tag = git_refs.get('branch'), git_refs.get('tag')
441+
self.log.debug(f"Writing config.toml entry for git repo {git_repo}: branch {branch}, tag {tag}, rev {rev}")
442+
entry = CONFIG_TOML_SOURCE_GIT.format(url=git_repo, rev=rev)
443+
if branch:
444+
entry += f'branch = "{branch}"\n'
445+
if tag:
446+
entry += f'tag = "{tag}"\n'
447+
write_file(config_toml, entry, append=True)
448+
449+
def _get_crate_git_repo_refs(self, crate_name):
453450
"""
454-
Find the dependency definition for given crate in all Cargo.toml files of sources
455-
Return branch target for given crate_name if any
451+
Find the dependency definitions for the given crate in all Cargo.toml files of sources
452+
Return branch and tag if any
456453
"""
457454
# Search all Cargo.toml files in main source and vendored crates
458455
cargo_toml_files = []
@@ -467,8 +464,9 @@ def _get_crate_git_repo_branch(self, crate_name):
467464
)
468465

469466
git_repo_spec = re.compile(re.escape(crate_name) + r"\s*=\s*{([^}]*)}", re.M)
470-
git_branch_spec = re.compile(r'branch\s*=\s*"([^"]*)"', re.M)
467+
git_branch_spec = re.compile(r'(?P<ref>branch|tag)\s*=\s*"(?P<value>[^"]*)"', re.M)
471468

469+
found_specs = {}
472470
for cargo_toml in cargo_toml_files:
473471
git_repo_crate = git_repo_spec.search(read_file(cargo_toml))
474472
if git_repo_crate:
@@ -477,11 +475,12 @@ def _get_crate_git_repo_branch(self, crate_name):
477475
git_repo_crate_contents = git_repo_crate.group(1)
478476
git_branch_crate = git_branch_spec.search(git_repo_crate_contents)
479477
if git_branch_crate:
480-
self.log.debug(f"Found git branch requirement for crate '{crate_name}': " +
481-
git_branch_crate.group())
482-
return git_branch_crate.group(1)
478+
ref = git_branch_crate['ref']
479+
value = git_branch_crate['value']
480+
self.log.debug(f"Found git {ref} requirement for crate '{crate_name}': {value}")
481+
found_specs[ref] = value
483482

484-
return None
483+
return found_specs
485484

486485
def prepare_step(self, *args, **kwargs):
487486
"""

0 commit comments

Comments
 (0)