-
-
Notifications
You must be signed in to change notification settings - Fork 41
NodeBlock Docs CI #2196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
NodeBlock Docs CI #2196
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm all for more CI!
Is https://github.com/FCP-INDI/cpac-docs the future replacement for the main C-PAC docs repo (https://github.com/FCP-INDI/fcp-indi.github.io) or is it a component repo like https://github.com/FCP-INDI/C-PAC_tutorials ? I can't tell from that repo / this PR if it's already in use or just in preparation for future use.
I think all this functionality is just in service of the documentation CI, not anything happening in C-PAC itself. In that case, I think we should move the scripts from scripts
to .github/scripts
and add
.github/scripts/config_extractor.py
.github/scripts/nodeblock_docs.py
to .dockerignore
after https://github.com/childmindresearch/C-PAC-test/blob/9c876c3a29a0ca97d4acfa7d6cfbd037f5ba9e44/.dockerignore#L7
OR these scripts and most of the workflow could live in https://github.com/FCP-INDI/cpac-docs and this repo could just add a workflow that triggers the workflow over there.
Not at all a blocker for merging this, but I think this would be easier to follow and maintain with more thorough type hints.
There's some wheel reinventing here that I'm not sure is necessary (but it might be? I'm also not sure it isn't necessary). I commented some suggestions, that, if accepted, I think several of the functions here could then be removed entirely.
scripts/config_extractor.py
Outdated
import sys | ||
|
||
sys.path.append(".") | ||
|
||
CPAC_DIR = pl.Path(".") | ||
CONFIG_DIR = pl.Path("configs") | ||
|
||
fetch_and_expand_all_cpac_configs(CPAC_DIR, CONFIG_DIR) | ||
|
||
with open("nodeblock_index.json") as f: | ||
nbs = json.load(f) | ||
|
||
configs = {} | ||
|
||
for config_path in CONFIG_DIR.glob("*.yml"): | ||
with open(config_path, "r", encoding="utf-8") as handle: | ||
config = yaml.safe_load(handle) | ||
|
||
configs[config_path.stem] = config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use existing infrastructure to load all the preconfigs
import sys | |
sys.path.append(".") | |
CPAC_DIR = pl.Path(".") | |
CONFIG_DIR = pl.Path("configs") | |
fetch_and_expand_all_cpac_configs(CPAC_DIR, CONFIG_DIR) | |
with open("nodeblock_index.json") as f: | |
nbs = json.load(f) | |
configs = {} | |
for config_path in CONFIG_DIR.glob("*.yml"): | |
with open(config_path, "r", encoding="utf-8") as handle: | |
config = yaml.safe_load(handle) | |
configs[config_path.stem] = config | |
from CPAC.pipeline import ALL_PIPELINE_CONFIGS | |
from CPAC.utils.configuration.configuration import Configuration, Preconfiguration | |
with open("nodeblock_index.json") as f: | |
nbs = json.load(f) | |
configs: dict[str, Configuration] = {config: Preconfiguration(config, skip_env_check=True) for config in ALL_PIPELINE_CONFIGS} |
and then we'll have a dict[str, Configuration]
instead of a dict[str, dict]
so we'll continue to have access to all the built-in methods
scripts/config_extractor.py
Outdated
|
||
configs_with_this_enabled = [] | ||
for config_name, config in configs.items(): | ||
if _any_true_in_config(config, paths): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading this right and paths
is what's in a switch
key in a NodeBlock, if we load the configurations as Configuration
s instead of dict
s, we could use the switch_is_on
method like
if _any_true_in_config(config, paths): | |
if all(config.switch_is_on(switch) for switch in paths): |
and then we don't need a lot of the new infrastructure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was about to add the changes I made over at the prototype repo, but using the config class directly is of course better.
Just for reference:
scripts/config_extractor.py
Outdated
def check_cpac_config( | ||
config: dict, | ||
) -> tuple[Literal[True], None] | tuple[Literal[False], Exception]: | ||
"""Checks if the specified file is a valid C-PAC config file""" | ||
from CPAC.utils.configuration.configuration import Configuration # noqa | ||
|
||
try: | ||
Configuration(config) | ||
except Exception as e: | ||
return False, e | ||
return True, None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't used anywhere?
def find_nodeblocks(root_dir): | ||
visitor = NodeBlockVisitor() | ||
root_path = Path(root_dir) | ||
|
||
# Walk through all Python files | ||
for python_file in root_path.rglob('*.py'): | ||
try: | ||
with open(python_file, 'r', encoding='utf-8') as f: | ||
source = f.read() | ||
visitor.current_source = source.splitlines() | ||
visitor.current_file = python_file.relative_to(root_path) | ||
tree = ast.parse(source) | ||
visitor.visit(tree) | ||
except Exception as e: | ||
print(f"Error processing {python_file}: {e}") | ||
|
||
return visitor.nodeblocks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is different enough that it's probably worth having both for now, but it's pretty similar to
C-PAC/CPAC/pipeline/resource_inventory.py
Lines 64 to 108 in b3521fc
def import_nodeblock_functions( | |
package_name: str, exclude: Optional[list[str]] = None | |
) -> list[NodeBlockFunction]: | |
""" | |
Import all functions with the @nodeblock decorator from all modules and submodules in a package. | |
Parameters | |
---------- | |
package_name | |
The name of the package to import from. | |
exclude | |
A list of module names to exclude from the import. | |
""" | |
if exclude is None: | |
exclude = [] | |
functions: list[NodeBlockFunction] = [] | |
package = importlib.import_module(package_name) | |
package_path = package.__path__[0] # Path to the package directory | |
for root, _, package_files in os.walk(package_path): | |
for file in package_files: | |
if file.endswith(".py") and file != "__init__.py": | |
# Get the module path | |
rel_path = os.path.relpath(os.path.join(root, file), package_path) | |
module_name = f"{package_name}.{rel_path[:-3].replace(os.sep, '.')}" | |
if module_name in exclude: | |
continue | |
# Import the module | |
try: | |
with patch.dict( | |
"sys.modules", {exclusion: None for exclusion in exclude} | |
): | |
module = importlib.import_module(module_name) | |
except (ImportError, TraitError, ValueError) as e: | |
UTLOGGER.debug(f"Failed to import {module_name}: {e}") | |
continue | |
# Extract nodeblock-decorated functions from the module | |
for _name, obj in inspect.getmembers( | |
module, predicate=lambda obj: isinstance(obj, NodeBlockFunction) | |
): | |
functions.append(obj) | |
return functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey Jon, thanks for your comments - they're super helpful in understanding how CI will work best in this context :)
In regard to
Is https://github.com/FCP-INDI/cpac-docs the future replacement for the main C-PAC docs repo (https://github.com/FCP-INDI/fcp-indi.github.io) or is it a component repo like https://github.com/FCP-INDI/C-PAC_tutorials ? I can't tell from that repo / this PR if it's already in use or just in preparation for future use.
Yes, I'm using https://github.com/FCP-INDI/cpac-docs as the future replacement for the main C-PAC docs repo. At the time of this PR, it didn't have any content - we were just using it as a place to store nodeblock_index.json
.
I opened an issue in that new repo here FCP-INDI/cpac-docs#12 to address the others. Looking forward to getting it all working!
.github/workflows/nodeblock_docs.yml
Outdated
# Copy the JSON file to the target repo and commit it | ||
- name: Commit documentation to target repo | ||
run: | | ||
cp source-repo/nodeblock_index.json target-repo/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cp source-repo/nodeblock_index.json target-repo/ | |
cp source-repo/nodeblock_index.json target-repo/src/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that FCP-INDI/cpac-docs#120 is merged, I think this change should address my suggestions re: FCP-INDI/cpac-docs#24
hi @shnizzedy, I applied the above suggestions - let me know if I missed something! Since I moved the scripts from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this for 2 months. Yeah, I think it looks good! Once it's merged into main, we can see how it works and patch any bugs directly then.
Description
Adds continuous integration functionality for C-PAC documentation. The
nodeblock_index.json
file is populated with updated nodeblock documentation, which will then be read into https://github.com/FCP-INDI/cpac-docs.Technical details
Once merged, the C-PAC repo will also need the CPAC_DOC_REPO_ACCESS secret added.
Checklist
Update index.md
).develop
branch of the repository.Developer Certificate of Origin
Developer Certificate of Origin