Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
'loguru>=0.7.2',
'gitpython>=3.1.43',
'msgspec>=0.19.0',
'packaging',
]
description = "ANTLR4 grammars for McStas and McXtrace"
readme = "README.md"
Expand Down
55 changes: 31 additions & 24 deletions src/mccode_antlr/reader/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,31 +519,38 @@ def registry_from_specification(spec: str):
REGISTRY_PRIORITY_HIGHEST=10


def _get_remote_repository_version_tags(url):
import re
import git
g = git.cmd.Git()
res = g.ls_remote(url, sort='-v:refname', tags=True)
ex = re.compile(r'v\d+(?:\.\d+(?:\.\d+)?)?')
return list(dict.fromkeys(ex.findall(res)))

def _source_registry_tag():
"""This causes the confuse config directory to be created, so could fail
on read-only systems."""
from mccode_antlr.config import config
requested_tag = config['mccode_pooch']['tag'].as_str_expanded()
registry_url = config['mccode_pooch']['registry'].as_str_expanded()
source_url = config['mccode_pooch']['source'].as_str_expanded()

known_tags = _get_remote_repository_version_tags(registry_url)
if requested_tag.lower() == 'latest':
requested_tag = known_tags[0]
elif requested_tag not in known_tags:
raise RuntimeError(f"The specified version tag, {requested_tag}, is not available in {registry_url}")
return source_url, registry_url, requested_tag


def mccode_registry_version():
from packaging.version import Version
_, _, tag = _source_registry_tag()
return Version(tag)


def _mccode_pooch_registries(flavor: Flavor):
def get_remote_repository_version_tags(url):
import re
import git
g = git.cmd.Git()
res = g.ls_remote(url, sort='-v:refname', tags=True)
ex = re.compile(r'v\d+(?:\.\d+(?:\.\d+)?)?')
return list(dict.fromkeys(ex.findall(res)))

def source_registry_tag():
"""This causes the confuse config directory to be created, so could fail
on read-only systems."""
from mccode_antlr.config import config
requested_tag = config['mccode_pooch']['tag'].as_str_expanded()
registry_url = config['mccode_pooch']['registry'].as_str_expanded()
source_url = config['mccode_pooch']['source'].as_str_expanded()

known_tags = get_remote_repository_version_tags(registry_url)
if requested_tag.lower() == 'latest':
requested_tag = known_tags[0]
elif requested_tag not in known_tags:
raise RuntimeError(f"The specified version tag, {requested_tag}, is not available in {registry_url}")
return source_url, registry_url, requested_tag

src, reg, tag = source_registry_tag()
src, reg, tag = _source_registry_tag()

def make_registry(name):
return GitHubRegistry(name, src, tag, registry=reg, priority=REGISTRY_PRIORITY_LOWEST)
Expand Down
16 changes: 16 additions & 0 deletions src/mccode_antlr/translators/c_initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def nexus_lines():


def cogen_initialize(source, component_declared_parameters, ok_to_skip):
from packaging.version import Version
from mccode_antlr.reader.registry import mccode_registry_version
version = mccode_registry_version()

lines = ["/* *****************************************************************************",
f"* instrument '{source.name}' and components INITIALISE",
"***************************************************************************** */",
Expand All @@ -244,6 +248,18 @@ def cogen_initialize(source, component_declared_parameters, ok_to_skip):
lines.extend([
f'int init(void) {{ /* called by mccode_main for {source.name}:INITIALIZE */',
' DEBUG_INSTR();',
])
if version > Version("3.5.40"):
# McCode https://github.com/mccode-dev/McCode/pull/2227#issuecomment-3613096573
# changed `mccode_main.c` and the code-generator to move the random number
# seeding before "INITIALIZE" time.
# The most-recent release at that time was 3.5.40, so this change is only
# compatible with releases after that.
lines.extend([
'// Initialise rng',
' srandom(_hash(mcseed-1));',
])
lines.extend([
'',
' /* code_main/parseoptions/readparams sets instrument parameters value */',
f' stracpy(instrument->_name, "{source.name}", {len(source.name)+1});',
Expand Down
2 changes: 1 addition & 1 deletion src/mccode_antlr/translators/c_raytrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def cogen_raytrace(source, ok_to_skip):
f' // GROUP {group.name}: from {fn} [{group.first_id}] to {ln} [{1+lid}]',
# Skip over when SCATTERED in the group:
f' if (SCATTERED) _particle->_index = {1+lid}; '
'// when SCATTERED in GROUP: reach exit of GROUP after {ln}',
f'// when SCATTERED in GROUP: reach exit of GROUP after {ln}',
#
])
if index == lid:
Expand Down